|
| 1 | +import type { StoryContext } from '@storybook/react'; |
1 | 2 | import { expect } from '@storybook/test'; |
2 | 3 | import { userEvent, within } from '@storybook/testing-library'; |
3 | | -import { StoryContext } from '@storybook/react'; |
4 | 4 |
|
5 | 5 | // Test selecting a US state |
6 | 6 | export const testUSStateSelection = async ({ canvasElement }: StoryContext) => { |
7 | 7 | const canvas = within(canvasElement); |
8 | | - |
| 8 | + |
9 | 9 | // Find and click the US state dropdown |
10 | 10 | const stateDropdown = canvas.getByLabelText('US State'); |
11 | 11 | await userEvent.click(stateDropdown); |
12 | | - |
| 12 | + |
13 | 13 | // Select a state (e.g., California) |
14 | 14 | const californiaOption = await canvas.findByText('California'); |
15 | 15 | await userEvent.click(californiaOption); |
16 | | - |
| 16 | + |
17 | 17 | // Verify the selection |
18 | 18 | expect(stateDropdown).toHaveTextContent('California'); |
19 | 19 | }; |
20 | 20 |
|
21 | 21 | // Test selecting a Canadian province |
22 | 22 | export const testCanadaProvinceSelection = async ({ canvasElement }: StoryContext) => { |
23 | 23 | const canvas = within(canvasElement); |
24 | | - |
| 24 | + |
25 | 25 | // Find and click the Canada province dropdown |
26 | 26 | const provinceDropdown = canvas.getByLabelText('Canadian Province'); |
27 | 27 | await userEvent.click(provinceDropdown); |
28 | | - |
| 28 | + |
29 | 29 | // Select a province (e.g., Ontario) |
30 | 30 | const ontarioOption = await canvas.findByText('Ontario'); |
31 | 31 | await userEvent.click(ontarioOption); |
32 | | - |
| 32 | + |
33 | 33 | // Verify the selection |
34 | 34 | expect(provinceDropdown).toHaveTextContent('Ontario'); |
35 | 35 | }; |
36 | 36 |
|
37 | 37 | // Test form submission |
38 | 38 | export const testFormSubmission = async ({ canvasElement }: StoryContext) => { |
39 | 39 | const canvas = within(canvasElement); |
40 | | - |
| 40 | + |
41 | 41 | // Select a state |
42 | 42 | const stateDropdown = canvas.getByLabelText('US State'); |
43 | 43 | await userEvent.click(stateDropdown); |
44 | 44 | const californiaOption = await canvas.findByText('California'); |
45 | 45 | await userEvent.click(californiaOption); |
46 | | - |
| 46 | + |
47 | 47 | // Select a province |
48 | 48 | const provinceDropdown = canvas.getByLabelText('Canadian Province'); |
49 | 49 | await userEvent.click(provinceDropdown); |
50 | 50 | const ontarioOption = await canvas.findByText('Ontario'); |
51 | 51 | await userEvent.click(ontarioOption); |
52 | | - |
| 52 | + |
53 | 53 | // Select a custom region |
54 | 54 | const regionDropdown = canvas.getByLabelText('Custom Region'); |
55 | 55 | await userEvent.click(regionDropdown); |
56 | 56 | const customOption = await canvas.findByText('New York'); |
57 | 57 | await userEvent.click(customOption); |
58 | | - |
| 58 | + |
59 | 59 | // Submit the form |
60 | 60 | const submitButton = canvas.getByRole('button', { name: 'Submit' }); |
61 | 61 | await userEvent.click(submitButton); |
62 | | - |
| 62 | + |
63 | 63 | // Verify the submission (mock response would be shown) |
64 | 64 | await expect(canvas.findByText('Selected regions:')).resolves.toBeInTheDocument(); |
65 | 65 | }; |
66 | 66 |
|
67 | 67 | // Test validation errors |
68 | 68 | export const testValidationErrors = async ({ canvasElement }: StoryContext) => { |
69 | 69 | const canvas = within(canvasElement); |
70 | | - |
| 70 | + |
71 | 71 | // Submit the form without selecting anything |
72 | 72 | const submitButton = canvas.getByRole('button', { name: 'Submit' }); |
73 | 73 | await userEvent.click(submitButton); |
74 | | - |
| 74 | + |
75 | 75 | // Verify error messages |
76 | 76 | await expect(canvas.findByText('Please select a state')).resolves.toBeInTheDocument(); |
77 | 77 | await expect(canvas.findByText('Please select a province')).resolves.toBeInTheDocument(); |
78 | 78 | await expect(canvas.findByText('Please select a region')).resolves.toBeInTheDocument(); |
79 | 79 | }; |
80 | | - |
0 commit comments