@@ -7,12 +7,15 @@ import { Dialog } from './index';
77
88afterEach ( ( ) => cleanup ( ) ) ;
99
10+ // Headless parts no longer emit `data-cl-slot` — slot identity is applied by the styled
11+ // (mosaic) layer. Tests locate the surface-only parts (backdrop, viewport, trigger) via
12+ // `data-testid` and everything else via its accessible role or text.
1013function renderDialog ( props : Partial < React . ComponentProps < typeof Dialog . Root > > = { } ) {
1114 return render (
1215 < Dialog . Root { ...props } >
13- < Dialog . Trigger > Open dialog</ Dialog . Trigger >
14- < Dialog . Backdrop />
15- < Dialog . Viewport >
16+ < Dialog . Trigger data-testid = 'dialog-trigger' > Open dialog</ Dialog . Trigger >
17+ < Dialog . Backdrop data-testid = 'dialog-backdrop' />
18+ < Dialog . Viewport data-testid = 'dialog-viewport' >
1619 < Dialog . Popup >
1720 < Dialog . Title > Dialog Title</ Dialog . Title >
1821 < Dialog . Description > Some dialog description</ Dialog . Description >
@@ -25,25 +28,6 @@ function renderDialog(props: Partial<React.ComponentProps<typeof Dialog.Root>> =
2528}
2629
2730describe ( 'Dialog' , ( ) => {
28- describe ( 'slot attributes' , ( ) => {
29- it ( 'renders trigger with data-cl-slot' , ( ) => {
30- renderDialog ( ) ;
31- const trigger = screen . getByRole ( 'button' , { name : 'Open dialog' } ) ;
32- expect ( trigger ) . toHaveAttribute ( 'data-cl-slot' , 'dialog-trigger' ) ;
33- } ) ;
34-
35- it ( 'renders all parts with correct slot attributes when open' , ( ) => {
36- renderDialog ( { defaultOpen : true } ) ;
37-
38- expect ( document . querySelector ( '[data-cl-slot="dialog-backdrop"]' ) ) . toBeInTheDocument ( ) ;
39- expect ( document . querySelector ( '[data-cl-slot="dialog-viewport"]' ) ) . toBeInTheDocument ( ) ;
40- expect ( document . querySelector ( '[data-cl-slot="dialog-popup"]' ) ) . toBeInTheDocument ( ) ;
41- expect ( document . querySelector ( '[data-cl-slot="dialog-title"]' ) ) . toBeInTheDocument ( ) ;
42- expect ( document . querySelector ( '[data-cl-slot="dialog-description"]' ) ) . toBeInTheDocument ( ) ;
43- expect ( document . querySelector ( '[data-cl-slot="dialog-close"]' ) ) . toBeInTheDocument ( ) ;
44- } ) ;
45- } ) ;
46-
4731 describe ( 'open/close' , ( ) => {
4832 it ( 'opens on trigger click' , async ( ) => {
4933 const user = userEvent . setup ( ) ;
@@ -53,7 +37,7 @@ describe('Dialog', () => {
5337 await user . click ( trigger ) ;
5438
5539 expect ( trigger ) . toHaveAttribute ( 'data-cl-open' , '' ) ;
56- expect ( document . querySelector ( '[data-cl-slot=" dialog-popup"] ') ) . toBeInTheDocument ( ) ;
40+ expect ( screen . getByRole ( ' dialog') ) . toBeInTheDocument ( ) ;
5741 } ) ;
5842
5943 it ( 'closes on Escape' , async ( ) => {
@@ -93,7 +77,7 @@ describe('Dialog', () => {
9377 it ( 'respects controlled open prop' , ( ) => {
9478 renderDialog ( { open : true } ) ;
9579
96- expect ( document . querySelector ( '[data-cl-slot=" dialog-popup"] ') ) . toBeInTheDocument ( ) ;
80+ expect ( screen . getByRole ( ' dialog') ) . toBeInTheDocument ( ) ;
9781 } ) ;
9882
9983 it ( 'does not open when controlled open is false' , async ( ) => {
@@ -102,29 +86,29 @@ describe('Dialog', () => {
10286
10387 await user . click ( screen . getByRole ( 'button' , { name : 'Open dialog' } ) ) ;
10488
105- expect ( document . querySelector ( '[data-cl-slot=" dialog-popup"] ') ) . not . toBeInTheDocument ( ) ;
89+ expect ( screen . queryByRole ( ' dialog') ) . not . toBeInTheDocument ( ) ;
10690 } ) ;
10791 } ) ;
10892
10993 describe ( 'ARIA attributes' , ( ) => {
11094 it ( 'popup has aria-labelledby linked to title' , ( ) => {
11195 renderDialog ( { defaultOpen : true } ) ;
11296
113- const title = document . querySelector ( '[data-cl-slot="dialog-title"] ') ;
114- const popup = document . querySelector ( '[data-cl-slot=" dialog-popup"] ') ;
97+ const title = screen . getByText ( 'Dialog Title ') ;
98+ const popup = screen . getByRole ( ' dialog') ;
11599
116100 expect ( title ) . toHaveAttribute ( 'id' ) ;
117- expect ( popup ) . toHaveAttribute ( 'aria-labelledby' , title ? .getAttribute ( 'id' ) ) ;
101+ expect ( popup ) . toHaveAttribute ( 'aria-labelledby' , title . getAttribute ( 'id' ) ) ;
118102 } ) ;
119103
120104 it ( 'popup has aria-describedby linked to description' , ( ) => {
121105 renderDialog ( { defaultOpen : true } ) ;
122106
123- const desc = document . querySelector ( '[data-cl-slot=" dialog- description"] ') ;
124- const popup = document . querySelector ( '[data-cl-slot=" dialog-popup"] ') ;
107+ const desc = screen . getByText ( 'Some dialog description') ;
108+ const popup = screen . getByRole ( ' dialog') ;
125109
126110 expect ( desc ) . toHaveAttribute ( 'id' ) ;
127- expect ( popup ) . toHaveAttribute ( 'aria-describedby' , desc ? .getAttribute ( 'id' ) ) ;
111+ expect ( popup ) . toHaveAttribute ( 'aria-describedby' , desc . getAttribute ( 'id' ) ) ;
128112 } ) ;
129113
130114 it ( 'popup has role=dialog' , ( ) => {
@@ -137,7 +121,7 @@ describe('Dialog', () => {
137121 describe ( 'animation lifecycle' , ( ) => {
138122 it ( 'backdrop is not rendered when closed' , ( ) => {
139123 renderDialog ( ) ;
140- expect ( document . querySelector ( '[data-cl-slot=" dialog-backdrop"] ') ) . not . toBeInTheDocument ( ) ;
124+ expect ( screen . queryByTestId ( ' dialog-backdrop') ) . not . toBeInTheDocument ( ) ;
141125 } ) ;
142126
143127 it ( 'applies data-cl-open on popup when open' , async ( ) => {
@@ -146,8 +130,7 @@ describe('Dialog', () => {
146130
147131 await user . click ( screen . getByRole ( 'button' , { name : 'Open dialog' } ) ) ;
148132
149- const popup = document . querySelector ( '[data-cl-slot="dialog-popup"]' ) ;
150- expect ( popup ) . toHaveAttribute ( 'data-cl-open' , '' ) ;
133+ expect ( screen . getByRole ( 'dialog' ) ) . toHaveAttribute ( 'data-cl-open' , '' ) ;
151134 } ) ;
152135
153136 it ( 'applies data-cl-open on backdrop when open' , async ( ) => {
@@ -156,8 +139,7 @@ describe('Dialog', () => {
156139
157140 await user . click ( screen . getByRole ( 'button' , { name : 'Open dialog' } ) ) ;
158141
159- const backdrop = document . querySelector ( '[data-cl-slot="dialog-backdrop"]' ) ;
160- expect ( backdrop ) . toHaveAttribute ( 'data-cl-open' , '' ) ;
142+ expect ( screen . getByTestId ( 'dialog-backdrop' ) ) . toHaveAttribute ( 'data-cl-open' , '' ) ;
161143 } ) ;
162144
163145 it ( 'applies data-cl-open on viewport when open' , async ( ) => {
@@ -166,13 +148,12 @@ describe('Dialog', () => {
166148
167149 await user . click ( screen . getByRole ( 'button' , { name : 'Open dialog' } ) ) ;
168150
169- const viewport = document . querySelector ( '[data-cl-slot="dialog-viewport"]' ) ;
170- expect ( viewport ) . toHaveAttribute ( 'data-cl-open' , '' ) ;
151+ expect ( screen . getByTestId ( 'dialog-viewport' ) ) . toHaveAttribute ( 'data-cl-open' , '' ) ;
171152 } ) ;
172153
173154 it ( 'viewport is not rendered when closed' , ( ) => {
174155 renderDialog ( ) ;
175- expect ( document . querySelector ( '[data-cl-slot=" dialog-viewport"] ') ) . not . toBeInTheDocument ( ) ;
156+ expect ( screen . queryByTestId ( ' dialog-viewport') ) . not . toBeInTheDocument ( ) ;
176157 } ) ;
177158 } ) ;
178159
@@ -204,12 +185,12 @@ describe('Dialog', () => {
204185 </ Dialog . Root > ,
205186 ) ;
206187
207- expect ( document . querySelector ( '[data-cl-slot=" dialog-popup"] ') ) . not . toBeInTheDocument ( ) ;
188+ expect ( screen . queryByRole ( ' dialog') ) . not . toBeInTheDocument ( ) ;
208189 expect ( screen . queryByText ( 'Popup content' ) ) . not . toBeInTheDocument ( ) ;
209190
210191 await user . click ( screen . getByRole ( 'button' , { name : 'Open' } ) ) ;
211192
212- expect ( document . querySelector ( '[data-cl-slot=" dialog-popup"] ') ) . toBeInTheDocument ( ) ;
193+ expect ( screen . getByRole ( ' dialog') ) . toBeInTheDocument ( ) ;
213194 expect ( screen . getByText ( 'Popup content' ) ) . toBeInTheDocument ( ) ;
214195 } ) ;
215196 } ) ;
@@ -223,8 +204,8 @@ describe('Dialog', () => {
223204
224205 it ( 'trigger has data-cl-open when dialog is visible' , ( ) => {
225206 renderDialog ( { defaultOpen : true } ) ;
226- // When modal is open, trigger's container gets aria-hidden, so use querySelector
227- const trigger = document . querySelector ( '[data-cl-slot=" dialog-trigger"] ') ;
207+ // When modal is open, the trigger's container gets aria-hidden, so query by test id.
208+ const trigger = screen . getByTestId ( ' dialog-trigger') ;
228209 expect ( trigger ) . toHaveAttribute ( 'data-cl-open' , '' ) ;
229210 } ) ;
230211 } ) ;
@@ -246,7 +227,7 @@ describe('Dialog', () => {
246227 < Dialog . Root modal = { false } >
247228 < Dialog . Trigger > Open dialog</ Dialog . Trigger >
248229 < Dialog . Backdrop />
249- < Dialog . Viewport >
230+ < Dialog . Viewport data-testid = 'dialog-viewport' >
250231 < Dialog . Popup >
251232 < Dialog . Title > Dialog Title</ Dialog . Title >
252233 < Dialog . Close > Close</ Dialog . Close >
@@ -258,9 +239,9 @@ describe('Dialog', () => {
258239
259240 await user . click ( screen . getByRole ( 'button' , { name : 'Open dialog' } ) ) ;
260241
261- const viewport = document . querySelector ( '[data-cl-slot=" dialog-viewport"] ') ;
242+ const viewport = screen . getByTestId ( ' dialog-viewport') ;
262243 expect ( viewport ) . toHaveStyle ( { pointerEvents : 'auto' } ) ;
263- expect ( viewport ? .parentElement ) . toHaveStyle ( { pointerEvents : 'none' } ) ;
244+ expect ( viewport . parentElement ) . toHaveStyle ( { pointerEvents : 'none' } ) ;
264245
265246 await user . click ( screen . getByRole ( 'button' , { name : 'Background button' } ) ) ;
266247 expect ( onBackgroundClick ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments