@@ -6,55 +6,66 @@ import { userEvent, within } from '@storybook/testing-library';
66export const testUSStateSelection = async ( { canvasElement } : StoryContext ) => {
77 const canvas = within ( canvasElement ) ;
88
9- // Find and click the US state dropdown
10- const stateDropdown = canvas . getByLabelText ( 'US State' ) ;
11- await userEvent . click ( stateDropdown ) ;
9+ // Wait for and click the US state trigger (combobox)
10+ const stateTrigger = await canvas . findByLabelText ( 'US State' ) ;
11+ await userEvent . click ( stateTrigger ) ;
1212
13- // Select a state (e.g., California)
14- const californiaOption = await canvas . findByText ( 'California' ) ;
13+ // Dropdown content is portaled; query from document.body
14+ const listbox = await within ( document . body ) . findByRole ( 'listbox' ) ;
15+ const californiaOption = within ( listbox ) . getByRole ( 'option' , { name : 'California' } ) ;
1516 await userEvent . click ( californiaOption ) ;
1617
17- // Verify the selection
18- expect ( stateDropdown ) . toHaveTextContent ( 'California' ) ;
18+ // Verify the trigger text updates
19+ await expect ( canvas . findByRole ( 'combobox' , { name : 'US State' } ) ) . resolves . toHaveTextContent ( 'California' ) ;
1920} ;
2021
2122// Test selecting a Canadian province
2223export const testCanadaProvinceSelection = async ( { canvasElement } : StoryContext ) => {
2324 const canvas = within ( canvasElement ) ;
2425
25- // Find and click the Canada province dropdown
26- const provinceDropdown = canvas . getByLabelText ( 'Canadian Province' ) ;
27- await userEvent . click ( provinceDropdown ) ;
26+ // Wait for and click the province trigger
27+ const provinceTrigger = await canvas . findByLabelText ( 'Canadian Province' ) ;
28+ await userEvent . click ( provinceTrigger ) ;
2829
29- // Select a province (e.g., Ontario)
30- const ontarioOption = await canvas . findByText ( 'Ontario' ) ;
30+ // Query in the portaled content
31+ const listbox = await within ( document . body ) . findByRole ( 'listbox' ) ;
32+ const ontarioOption = within ( listbox ) . getByRole ( 'option' , { name : 'Ontario' } ) ;
3133 await userEvent . click ( ontarioOption ) ;
3234
33- // Verify the selection
34- expect ( provinceDropdown ) . toHaveTextContent ( 'Ontario' ) ;
35+ // Verify the trigger text updates
36+ await expect ( canvas . findByRole ( 'combobox' , { name : 'Canadian Province' } ) ) . resolves . toHaveTextContent ( 'Ontario' ) ;
3537} ;
3638
3739// Test form submission
3840export const testFormSubmission = async ( { canvasElement } : StoryContext ) => {
3941 const canvas = within ( canvasElement ) ;
4042
4143 // Select a state
42- const stateDropdown = canvas . getByLabelText ( 'US State' ) ;
43- await userEvent . click ( stateDropdown ) ;
44- const californiaOption = await canvas . findByText ( 'California' ) ;
45- await userEvent . click ( californiaOption ) ;
44+ const stateTrigger = await canvas . findByLabelText ( 'US State' ) ;
45+ await userEvent . click ( stateTrigger ) ;
46+ {
47+ const listbox = await within ( document . body ) . findByRole ( 'listbox' ) ;
48+ const californiaOption = within ( listbox ) . getByRole ( 'option' , { name : 'California' } ) ;
49+ await userEvent . click ( californiaOption ) ;
50+ }
4651
4752 // Select a province
48- const provinceDropdown = canvas . getByLabelText ( 'Canadian Province' ) ;
49- await userEvent . click ( provinceDropdown ) ;
50- const ontarioOption = await canvas . findByText ( 'Ontario' ) ;
51- await userEvent . click ( ontarioOption ) ;
52-
53- // Select a custom region
54- const regionDropdown = canvas . getByLabelText ( 'Custom Region' ) ;
55- await userEvent . click ( regionDropdown ) ;
56- const customOption = await canvas . findByText ( 'New York' ) ;
57- await userEvent . click ( customOption ) ;
53+ const provinceTrigger = await canvas . findByLabelText ( 'Canadian Province' ) ;
54+ await userEvent . click ( provinceTrigger ) ;
55+ {
56+ const listbox = await within ( document . body ) . findByRole ( 'listbox' ) ;
57+ const ontarioOption = within ( listbox ) . getByRole ( 'option' , { name : 'Ontario' } ) ;
58+ await userEvent . click ( ontarioOption ) ;
59+ }
60+
61+ // Select a custom region (use an option that exists in the story's options)
62+ const regionTrigger = await canvas . findByLabelText ( 'Custom Region' ) ;
63+ await userEvent . click ( regionTrigger ) ;
64+ {
65+ const listbox = await within ( document . body ) . findByRole ( 'listbox' ) ;
66+ const customOption = within ( listbox ) . getByRole ( 'option' , { name : 'California' } ) ;
67+ await userEvent . click ( customOption ) ;
68+ }
5869
5970 // Submit the form
6071 const submitButton = canvas . getByRole ( 'button' , { name : 'Submit' } ) ;
0 commit comments