Skip to content

Commit f28becd

Browse files
committed
Add more tests
1 parent 120020f commit f28becd

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

stories/html.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ export const SwapNodesBetweenDifferentLocations = () => {
253253
<span>{ index }</span>
254254
</InPortal>
255255
))}
256-
{order.map((position) => (
257-
<OutPortal node={nodes[position]} key={position} />
256+
{order.map((position, index) => (
257+
<OutPortal node={nodes[position]} key={index} />
258258
))}
259259
</div>
260260
);

tests/html.test.tsx

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,71 @@
11
import { expect, test } from 'vitest';
2-
import { render, screen } from '@testing-library/react';
2+
import { render } from '@testing-library/react';
33
import { composeStory } from '@storybook/react';
4+
import type { RenderResult } from '@testing-library/react';
45
import * as stories from '../stories/html.stories';
56

6-
const RenderThingsInDifferentPlaces = composeStory(
7-
stories.RenderThingsInDifferentPlaces,
8-
stories.default
9-
);
7+
const allStoryNames = Object.keys(stories).filter(key => key !== 'default');
108

11-
test('RenderThingsInDifferentPlaces', async () => {
12-
const { container } = render(<RenderThingsInDifferentPlaces />);
9+
const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
1310

14-
expect(container.innerHTML).toContain(
15-
'<div>Portal renders here:<div>Hi!</div></div>'
16-
);
11+
const storyTests: Record<string, (result: RenderResult) => void | Promise<void>> = {
12+
'RenderThingsInDifferentPlaces': ({ container }) => {
13+
expect(container.innerHTML).toContain(
14+
'<div>Portal renders here:<div>Hi!</div></div>'
15+
);
16+
},
17+
'ExampleFromREADME': ({ container }) => {
18+
expect(container.innerHTML).toContain('expensive!');
19+
expect(container.innerHTML).toContain('A:');
20+
},
21+
'PortalContainerElementAsSpanInParagraph': ({ container }) => {
22+
expect(container.innerHTML).toContain(
23+
'<p>Portal renders here:<span>Hi!</span></p>'
24+
);
25+
},
26+
'SwapNodesBetweenDifferentLocations': async ({ container, getByText }) => {
27+
const html = container.innerHTML;
28+
const initialText = container.textContent || '';
29+
30+
expect(html).toContain('<span>0</span>');
31+
expect(html).toContain('<span>1</span>');
32+
expect(html).toContain('<span>2</span>');
33+
expect(html).toContain('<span>3</span>');
34+
expect(html).toContain('<span>4</span>');
35+
36+
const spans = container.querySelectorAll('span');
37+
const order = Array.from(spans).map(span => span.textContent);
38+
expect(order).toEqual(['0', '1', '2', '3', '4']);
39+
40+
const button = getByText('Click to reverse the order');
41+
button.click();
42+
await wait(10);
43+
44+
const spansAfter = container.querySelectorAll('span');
45+
const orderAfter = Array.from(spansAfter).map(span => span.textContent);
46+
expect(orderAfter).toEqual(['4', '3', '2', '1', '0']);
47+
},
48+
};
49+
50+
// Skipped for now, until we have full test coverage of the stories:
51+
test.skip('all stories have tests', () => {
52+
const testedStories = Object.keys(storyTests);
53+
const untestedStories = allStoryNames.filter(name => !testedStories.includes(name));
54+
55+
if (untestedStories.length > 0) {
56+
throw new Error(
57+
`The following stories do not have tests:\n${untestedStories.map(s => ` - ${s}`).join('\n')}`
58+
);
59+
}
1760
});
61+
62+
Object.entries(storyTests).forEach(([storyName, testFn]) => {
63+
test(storyName, async () => {
64+
const Story = composeStory(
65+
(stories as any)[storyName],
66+
stories.default
67+
);
68+
const result = render(<Story />);
69+
await testFn(result);
70+
});
71+
});

vite.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export default defineConfig({
1212
provider: 'playwright',
1313
screenshotFailures: false,
1414
instances: [{
15-
browser: 'chromium'
15+
browser: 'chromium',
16+
launch: {
17+
args: ['--autoplay-policy=no-user-gesture-required']
18+
}
1619
}]
1720
},
1821
setupFiles: ['.storybook/vitest.setup.ts']

0 commit comments

Comments
 (0)