11import { expect , userEvent , within } from '@storybook/test' ;
22import { StoryContext } from '@storybook/react' ;
33
4+ // Helper function to wait for a short time
5+ const waitForSelection = async ( ms = 50 ) => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
6+
47// Test selecting a US state
58export const testUSStateSelection = async ( { canvasElement } : StoryContext ) => {
69 const canvas = within ( canvasElement ) ;
@@ -13,6 +16,9 @@ export const testUSStateSelection = async ({ canvasElement }: StoryContext) => {
1316 const californiaOption = await within ( document . body ) . findByRole ( 'option' , { name : 'California' } ) ;
1417 await userEvent . click ( californiaOption ) ;
1518
19+ // Wait for the selection to be applied
20+ await waitForSelection ( ) ;
21+
1622 // Verify the selection
1723 expect ( stateDropdown ) . toHaveTextContent ( 'California' ) ;
1824} ;
@@ -29,6 +35,9 @@ export const testCanadaProvinceSelection = async ({ canvasElement }: StoryContex
2935 const ontarioOption = await within ( document . body ) . findByRole ( 'option' , { name : 'Ontario' } ) ;
3036 await userEvent . click ( ontarioOption ) ;
3137
38+ // Wait for the selection to be applied
39+ await waitForSelection ( ) ;
40+
3241 // Verify the selection
3342 expect ( provinceDropdown ) . toHaveTextContent ( 'Ontario' ) ;
3443} ;
@@ -42,18 +51,21 @@ export const testFormSubmission = async ({ canvasElement }: StoryContext) => {
4251 await userEvent . click ( stateDropdown ) ;
4352 const californiaOption = await within ( document . body ) . findByRole ( 'option' , { name : 'California' } ) ;
4453 await userEvent . click ( californiaOption ) ;
54+ await waitForSelection ( ) ;
4555
4656 // Select a province
4757 const provinceDropdown = canvas . getByLabelText ( 'Canadian Province' ) ;
4858 await userEvent . click ( provinceDropdown ) ;
4959 const ontarioOption = await within ( document . body ) . findByRole ( 'option' , { name : 'Ontario' } ) ;
5060 await userEvent . click ( ontarioOption ) ;
61+ await waitForSelection ( ) ;
5162
5263 // Select a custom region
5364 const regionDropdown = canvas . getByLabelText ( 'Custom Region' ) ;
5465 await userEvent . click ( regionDropdown ) ;
5566 const customOption = await within ( document . body ) . findByRole ( 'option' , { name : 'New York' } ) ;
5667 await userEvent . click ( customOption ) ;
68+ await waitForSelection ( ) ;
5769
5870 // Submit the form
5971 const submitButton = canvas . getByRole ( 'button' , { name : 'Submit' } ) ;
0 commit comments