11import { expect , test } from 'vitest' ;
2- import { render , screen } from '@testing-library/react' ;
2+ import { render } from '@testing-library/react' ;
33import { composeStory } from '@storybook/react' ;
4+ import type { RenderResult } from '@testing-library/react' ;
45import * 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+ } ) ;
0 commit comments