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-
74// Test selecting a US state
85export const testUSStateSelection = async ( { canvasElement } : StoryContext ) => {
96 const canvas = within ( canvasElement ) ;
@@ -16,11 +13,8 @@ export const testUSStateSelection = async ({ canvasElement }: StoryContext) => {
1613 const californiaOption = await within ( document . body ) . findByRole ( 'option' , { name : 'California' } ) ;
1714 await userEvent . click ( californiaOption ) ;
1815
19- // Wait for the selection to be applied
20- await waitForSelection ( ) ;
21-
22- // Verify the selection
23- expect ( stateDropdown ) . toHaveTextContent ( 'California' ) ;
16+ // Wait for the popover to close and the selection to be applied
17+ await expect ( stateDropdown ) . toHaveTextContent ( 'California' ) ;
2418} ;
2519
2620// Test selecting a Canadian province
@@ -35,11 +29,8 @@ export const testCanadaProvinceSelection = async ({ canvasElement }: StoryContex
3529 const ontarioOption = await within ( document . body ) . findByRole ( 'option' , { name : 'Ontario' } ) ;
3630 await userEvent . click ( ontarioOption ) ;
3731
38- // Wait for the selection to be applied
39- await waitForSelection ( ) ;
40-
41- // Verify the selection
42- expect ( provinceDropdown ) . toHaveTextContent ( 'Ontario' ) ;
32+ // Wait for the popover to close and the selection to be applied
33+ await expect ( provinceDropdown ) . toHaveTextContent ( 'Ontario' ) ;
4334} ;
4435
4536// Test form submission
@@ -51,21 +42,21 @@ export const testFormSubmission = async ({ canvasElement }: StoryContext) => {
5142 await userEvent . click ( stateDropdown ) ;
5243 const californiaOption = await within ( document . body ) . findByRole ( 'option' , { name : 'California' } ) ;
5344 await userEvent . click ( californiaOption ) ;
54- await waitForSelection ( ) ;
45+ await expect ( stateDropdown ) . toHaveTextContent ( 'California' ) ;
5546
5647 // Select a province
5748 const provinceDropdown = canvas . getByLabelText ( 'Canadian Province' ) ;
5849 await userEvent . click ( provinceDropdown ) ;
5950 const ontarioOption = await within ( document . body ) . findByRole ( 'option' , { name : 'Ontario' } ) ;
6051 await userEvent . click ( ontarioOption ) ;
61- await waitForSelection ( ) ;
52+ await expect ( provinceDropdown ) . toHaveTextContent ( 'Ontario' ) ;
6253
6354 // Select a custom region
6455 const regionDropdown = canvas . getByLabelText ( 'Custom Region' ) ;
6556 await userEvent . click ( regionDropdown ) ;
6657 const customOption = await within ( document . body ) . findByRole ( 'option' , { name : 'New York' } ) ;
6758 await userEvent . click ( customOption ) ;
68- await waitForSelection ( ) ;
59+ await expect ( regionDropdown ) . toHaveTextContent ( 'New York' ) ;
6960
7061 // Submit the form
7162 const submitButton = canvas . getByRole ( 'button' , { name : 'Submit' } ) ;
0 commit comments