|
1 | 1 | import type { Meta, StoryObj } from '@storybook/react-webpack5'; |
2 | | -import { expect, waitFor, within } from 'storybook/test'; |
| 2 | +import { expect, within } from 'storybook/test'; |
3 | 3 | import { SWRConfig } from 'swr'; |
4 | 4 | import { |
5 | 5 | matchDayLive, |
@@ -69,16 +69,21 @@ export const Fixture = { |
69 | 69 | 'https://api.nextgen.guardianapps.co.uk/football/api/match-header/2026/02/08/26247/48490.json', |
70 | 70 | ), |
71 | 71 | }, |
72 | | - play: async ({ canvasElement }) => { |
73 | | - const list = await getListElement(canvasElement); |
74 | | - let items = within(list).getAllByRole('listitem'); |
75 | | - void expect(items.length).toBe(1); |
76 | | - void expect(items[0]).toHaveTextContent('Match info'); |
77 | | - |
78 | | - await waitFor(() => { |
79 | | - items = within(list).getAllByRole('listitem'); |
80 | | - void expect(items.length).toBe(1); |
81 | | - void expect(items[0]).toHaveTextContent('Match info'); |
| 72 | + play: async ({ canvas, step }) => { |
| 73 | + const nav = canvas.getByRole('navigation'); |
| 74 | + const initialTabs = within(nav).getAllByRole('listitem'); |
| 75 | + |
| 76 | + void expect(initialTabs.length).toBe(1); |
| 77 | + void expect(initialTabs[0]).toHaveTextContent('Match info'); |
| 78 | + |
| 79 | + await step('Fetch updated match header data', async () => { |
| 80 | + // Wait for 'Home Team' to appear which indicates match header data |
| 81 | + // has been fetched and the UI updated |
| 82 | + await canvas.findByText('Home Team'); |
| 83 | + |
| 84 | + const updatedTabs = within(nav).getAllByRole('listitem'); |
| 85 | + void expect(updatedTabs.length).toBe(1); |
| 86 | + void expect(updatedTabs[0]).toHaveTextContent('Match info'); |
82 | 87 | }); |
83 | 88 | }, |
84 | 89 | } satisfies Story; |
@@ -125,12 +130,15 @@ export const Live = { |
125 | 130 | reportURL: undefined, |
126 | 131 | }), |
127 | 132 | }, |
128 | | - play: async ({ canvasElement }) => { |
129 | | - const canvas = within(canvasElement); |
| 133 | + play: async ({ canvas, step }) => { |
130 | 134 | void expect(canvas.getByLabelText('Score: 0')).toBeInTheDocument(); |
131 | 135 | void expect(canvas.getByLabelText('Score: 13')).toBeInTheDocument(); |
132 | 136 |
|
133 | | - await waitFor(() => { |
| 137 | + await step('Fetch updated match header data', async () => { |
| 138 | + // Wait for 'Home Team' to appear which indicates match header data |
| 139 | + // has been fetched and the UI updated |
| 140 | + await canvas.findByText('Home Team'); |
| 141 | + |
134 | 142 | void expect(canvas.getByLabelText('Score: 3')).toBeInTheDocument(); |
135 | 143 | void expect(canvas.getByLabelText('Score: 4')).toBeInTheDocument(); |
136 | 144 | }); |
@@ -160,30 +168,27 @@ export const Result = { |
160 | 168 | }), |
161 | 169 | }, |
162 | 170 |
|
163 | | - play: async ({ canvasElement }) => { |
164 | | - const list = await getListElement(canvasElement); |
165 | | - let items = within(list).getAllByRole('listitem'); |
166 | | - void expect(items.length).toBe(1); |
167 | | - void expect(items[0]).toHaveTextContent('Match info'); |
168 | | - |
169 | | - await waitFor(() => { |
170 | | - items = within(list).getAllByRole('listitem'); |
171 | | - void expect(items.length).toBe(3); |
172 | | - void expect(items[0]).toHaveTextContent('Match report'); |
173 | | - void expect(items[1]).toHaveTextContent('Live feed'); |
174 | | - void expect(items[2]).toHaveTextContent('Match info'); |
| 171 | + play: async ({ canvas, step }) => { |
| 172 | + const nav = canvas.getByRole('navigation'); |
| 173 | + const initialTabs = within(nav).getAllByRole('listitem'); |
| 174 | + |
| 175 | + void expect(initialTabs.length).toBe(1); |
| 176 | + void expect(initialTabs[0]).toHaveTextContent('Match info'); |
| 177 | + |
| 178 | + await step('Fetch updated match header data', async () => { |
| 179 | + // Wait for 'Home Team' to appear which indicates match header data |
| 180 | + // has been fetched and the UI updated |
| 181 | + await canvas.findByText('Home Team'); |
| 182 | + |
| 183 | + const updatedTabs = within(nav).getAllByRole('listitem'); |
| 184 | + void expect(updatedTabs.length).toBe(3); |
| 185 | + void expect(updatedTabs[0]).toHaveTextContent('Match report'); |
| 186 | + void expect(updatedTabs[1]).toHaveTextContent('Live feed'); |
| 187 | + void expect(updatedTabs[2]).toHaveTextContent('Match info'); |
175 | 188 | }); |
176 | 189 | }, |
177 | 190 | } satisfies Story; |
178 | 191 |
|
179 | | -const getListElement = async (canvasElement: HTMLElement) => { |
180 | | - const canvas = within(canvasElement); |
181 | | - const nav = await canvas.findByRole('navigation'); |
182 | | - const navQueries = within(nav); |
183 | | - // Get the list element that is within a nav element |
184 | | - return await navQueries.findByRole('list'); |
185 | | -}; |
186 | | - |
187 | 192 | const getMockData = (data: FEFootballMatchHeader) => |
188 | 193 | new Promise((resolve) => { |
189 | 194 | setTimeout(() => { |
|
0 commit comments