Skip to content

Commit 1a27c1e

Browse files
committed
Don't use waitFor to check that the UI has updated
As the before and after state for the 'Fixture' story is the same the expectations will pass before the UI has updated as `waitFor` does not have to wait and retry. By awaiting the change to the team name we ensure the UI has actually been updated before testing it is correct.
1 parent 866003d commit 1a27c1e

1 file changed

Lines changed: 39 additions & 34 deletions

File tree

dotcom-rendering/src/components/FootballMatchHeader/FootballMatchHeader.stories.tsx

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Meta, StoryObj } from '@storybook/react-webpack5';
2-
import { expect, waitFor, within } from 'storybook/test';
2+
import { expect, within } from 'storybook/test';
33
import { SWRConfig } from 'swr';
44
import {
55
matchDayLive,
@@ -69,16 +69,21 @@ export const Fixture = {
6969
'https://api.nextgen.guardianapps.co.uk/football/api/match-header/2026/02/08/26247/48490.json',
7070
),
7171
},
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');
8287
});
8388
},
8489
} satisfies Story;
@@ -125,12 +130,15 @@ export const Live = {
125130
reportURL: undefined,
126131
}),
127132
},
128-
play: async ({ canvasElement }) => {
129-
const canvas = within(canvasElement);
133+
play: async ({ canvas, step }) => {
130134
void expect(canvas.getByLabelText('Score: 0')).toBeInTheDocument();
131135
void expect(canvas.getByLabelText('Score: 13')).toBeInTheDocument();
132136

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+
134142
void expect(canvas.getByLabelText('Score: 3')).toBeInTheDocument();
135143
void expect(canvas.getByLabelText('Score: 4')).toBeInTheDocument();
136144
});
@@ -160,30 +168,27 @@ export const Result = {
160168
}),
161169
},
162170

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');
175188
});
176189
},
177190
} satisfies Story;
178191

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-
187192
const getMockData = (data: FEFootballMatchHeader) =>
188193
new Promise((resolve) => {
189194
setTimeout(() => {

0 commit comments

Comments
 (0)