@@ -7,29 +7,32 @@ import DropdownCombobox, {colors} from '../../test/useCombobox.test'
77import DropdownMultipleSelect from '../../test/useMultipleSelect.test'
88import { ReactShadowRoot } from '../../test/react-shadow'
99
10-
1110function _queryAllByRoleDeep ( container , ...rest ) {
12- // eslint-disable-next-line testing-library/prefer-screen-queries
13- const result = queryAllByRole ( container , ...rest ) // replace here with different queryAll* variants.
14- for ( const element of container . querySelectorAll ( '*' ) ) {
15- if ( element . shadowRoot ) {
16- result . push ( ..._queryAllByRoleDeep ( element . shadowRoot , ...rest ) )
17- }
11+ // eslint-disable-next-line testing-library/prefer-screen-queries
12+ const result = queryAllByRole ( container , ...rest ) // replace here with different queryAll* variants.
13+ for ( const element of container . querySelectorAll ( '*' ) ) {
14+ if ( element . shadowRoot ) {
15+ result . push ( ..._queryAllByRoleDeep ( element . shadowRoot , ...rest ) )
1816 }
19-
20- return result
2117 }
22-
23- // eslint-disable-next-line no-unused-vars
24- const [ _queryByRoleDeep , _getAllByRoleDeep , _getByRoleDeep , _findAllByRoleDeep , _findByRoleDeep ] = buildQueries (
25- _queryAllByRoleDeep ,
26- ( _ , role ) => `Found multiple elements with the role ${ role } ` ,
27- ( _ , role ) => `Unable to find an element with the role ${ role } `
28- )
2918
30- const getAllByRoleDeep = _getAllByRoleDeep . bind ( null , document . body )
31- const getByRoleDeep = _getByRoleDeep . bind ( null , document . body )
19+ return result
20+ }
3221
22+ const [
23+ _queryByRoleDeep ,
24+ _getAllByRoleDeep ,
25+ _getByRoleDeep ,
26+ _findAllByRoleDeep ,
27+ _findByRoleDeep ,
28+ ] = buildQueries (
29+ _queryAllByRoleDeep ,
30+ ( _ , role ) => `Found multiple elements with the role ${ role } ` ,
31+ ( _ , role ) => `Unable to find an element with the role ${ role } ` ,
32+ )
33+
34+ const getAllByRoleDeep = _getAllByRoleDeep . bind ( null , document . body )
35+ const getByRoleDeep = _getByRoleDeep . bind ( null , document . body )
3336
3437const Wrapper = ( { children} ) => {
3538 return < ReactShadowRoot > { children } </ ReactShadowRoot >
@@ -65,37 +68,37 @@ test('DropdownSelect works correctly in shadow DOM', async () => {
6568
6669 // Verify shadow root exists
6770 expect ( container . shadowRoot ) . toBeDefined ( )
68-
71+
6972 // Get elements within the shadow root
7073 const toggleButton = getByRoleDeep ( 'combobox' )
7174 const menu = getByRoleDeep ( 'listbox' )
7275 expect ( toggleButton ) . toBeInTheDocument ( )
7376 expect ( menu ) . toBeInTheDocument ( )
74-
77+
7578 // Initially menu should be closed
7679 expect ( toggleButton ) . toHaveAttribute ( 'aria-expanded' , 'false' )
77-
80+
7881 // Open the dropdown
7982 await user . click ( toggleButton )
80-
83+
8184 // Menu should now be open
8285 expect ( toggleButton ) . toHaveAttribute ( 'aria-expanded' , 'true' )
83-
86+
8487 // Select an item
8588 const blackOption = getByRoleDeep ( 'option' , { name : 'Black' } )
8689 await user . click ( blackOption )
87-
90+
8891 // Menu should close and selected item should appear in button
8992 expect ( toggleButton ) . toHaveAttribute ( 'aria-expanded' , 'false' )
9093 expect ( toggleButton ) . toHaveTextContent ( 'Black' )
91-
94+
9295 // Open it again
9396 await user . click ( toggleButton )
9497 expect ( toggleButton ) . toHaveAttribute ( 'aria-expanded' , 'true' )
95-
98+
9699 // Click outside (this tests our targetWithinDownshift with composedPath)
97100 await user . click ( document . body )
98-
101+
99102 // Menu should close
100103 expect ( toggleButton ) . toHaveAttribute ( 'aria-expanded' , 'false' )
101104} )
@@ -106,65 +109,53 @@ test('DropdownCombobox works correctly in shadow DOM', async () => {
106109
107110 // Verify shadow root exists
108111 expect ( container . shadowRoot ) . toBeDefined ( )
109-
112+
110113 // Get elements within the shadow root
111114 const input = getByRoleDeep ( 'combobox' )
112115 const toggleButton = getByRoleDeep ( 'button' , { name : 'toggle menu' } )
113116 const clearButton = getByRoleDeep ( 'button' , { name : 'clear' } )
114117 const menu = getByRoleDeep ( 'listbox' )
115-
118+
116119 expect ( input ) . toBeInTheDocument ( )
117120 expect ( toggleButton ) . toBeInTheDocument ( )
118121 expect ( clearButton ) . toBeInTheDocument ( )
119122 expect ( menu ) . toBeInTheDocument ( )
120-
123+
121124 // Initially menu should be closed
122125 expect ( input ) . toHaveAttribute ( 'aria-expanded' , 'false' )
123126 expect ( toggleButton ) . toHaveAttribute ( 'aria-expanded' , 'false' )
124-
127+
125128 // Open the dropdown
126129 await user . click ( toggleButton )
127-
130+
128131 // Menu should now be open
129132 expect ( input ) . toHaveAttribute ( 'aria-expanded' , 'true' )
130-
133+
131134 // All colors should initially be visible
132135 const items = getAllByRoleDeep ( 'option' )
133136 expect ( items ) . toHaveLength ( colors . length )
134-
135- // // Type in the input to filter items
136- // await user.click(input)
137- // input.focus()
138- // expect(input).toHaveFocus()
139- // await user.type(input, 'bl')
140-
141- // // Only Black and Blue should be visible
142- // items = getAllByRoleDeep('option')
143- // await waitFor(() => expect(items).toHaveLength(2))
144- // expect(items[0]).toHaveTextContent('Black')
145- // expect(items[1]).toHaveTextContent('Blue')
146-
137+
147138 // Select an item
148139 await user . click ( items [ 0 ] )
149-
140+
150141 // Menu should close and input should have selected value
151142 expect ( input ) . toHaveAttribute ( 'aria-expanded' , 'false' )
152143 expect ( toggleButton ) . toHaveAttribute ( 'aria-expanded' , 'false' )
153144 expect ( input ) . toHaveValue ( 'Black' )
154-
145+
155146 // Clear the selection
156147 await user . click ( clearButton )
157-
148+
158149 // Input should be empty
159150 expect ( input ) . toHaveValue ( '' )
160-
151+
161152 // Open it again
162153 await user . click ( toggleButton )
163154 expect ( input ) . toHaveAttribute ( 'aria-expanded' , 'true' )
164-
155+
165156 // Click outside to close
166157 await user . click ( document . body )
167-
158+
168159 // Menu should close
169160 expect ( input ) . toHaveAttribute ( 'aria-expanded' , 'false' )
170161} )
0 commit comments