|
1 | 1 | import React from 'react'; |
2 | 2 | import { axe } from 'vitest-axe'; |
3 | 3 | import type { RenderResult } from '@testing-library/react'; |
4 | | -import { render, fireEvent, cleanup } from '@testing-library/react'; |
| 4 | +import { render, fireEvent, cleanup, act } from '@testing-library/react'; |
5 | 5 | import * as Dialog from './dialog'; |
6 | 6 | import type { Mock, MockInstance } from 'vitest'; |
7 | 7 | import { describe, it, afterEach, beforeEach, vi, expect } from 'vitest'; |
@@ -139,3 +139,47 @@ describe('given a default Dialog', () => { |
139 | 139 | }); |
140 | 140 | }); |
141 | 141 | }); |
| 142 | + |
| 143 | +describe('given a Dialog rendered inside a Shadow DOM', () => { |
| 144 | + let shadowHost: HTMLElement; |
| 145 | + let shadowRoot: ShadowRoot; |
| 146 | + let consoleErrorMock: ReturnType<typeof vi.spyOn>; |
| 147 | + let consoleErrorMockFunction: ReturnType<typeof vi.fn>; |
| 148 | + |
| 149 | + beforeEach(() => { |
| 150 | + consoleErrorMockFunction = vi.fn(); |
| 151 | + consoleErrorMock = vi.spyOn(console, 'error').mockImplementation(consoleErrorMockFunction); |
| 152 | + |
| 153 | + // Create a shadow host and attach a shadow root |
| 154 | + shadowHost = document.createElement('div'); |
| 155 | + document.body.appendChild(shadowHost); |
| 156 | + shadowRoot = shadowHost.attachShadow({ mode: 'open' }); |
| 157 | + }); |
| 158 | + |
| 159 | + afterEach(() => { |
| 160 | + consoleErrorMock.mockRestore(); |
| 161 | + consoleErrorMockFunction.mockClear(); |
| 162 | + document.body.removeChild(shadowHost); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should not fire accessibility warning when DialogTitle is present inside Shadow DOM', async () => { |
| 166 | + const container = document.createElement('div'); |
| 167 | + shadowRoot.appendChild(container); |
| 168 | + |
| 169 | + render( |
| 170 | + <Dialog.Root open> |
| 171 | + <Dialog.Content> |
| 172 | + <Dialog.Title>Shadow Dialog Title</Dialog.Title> |
| 173 | + <Dialog.Description>Shadow Dialog Description</Dialog.Description> |
| 174 | + <Dialog.Close>Close</Dialog.Close> |
| 175 | + </Dialog.Content> |
| 176 | + </Dialog.Root>, |
| 177 | + { container } |
| 178 | + ); |
| 179 | + |
| 180 | + // Wait for effects to run |
| 181 | + await act(async () => {}); |
| 182 | + |
| 183 | + expect(consoleErrorMockFunction).not.toHaveBeenCalled(); |
| 184 | + }); |
| 185 | +}); |
0 commit comments