@@ -38,6 +38,16 @@ const waitForVideoToLoad = (video: HTMLVideoElement): Promise<void> => {
3838 } ) ;
3939} ;
4040
41+ const getSpanOrder = ( container : HTMLElement ) : string [ ] => {
42+ const spans = container . querySelectorAll ( 'span' ) ;
43+ return Array . from ( spans ) . map ( span => span . textContent ) ;
44+ } ;
45+
46+ const findButtonByText = ( container : HTMLElement , text : string ) : HTMLButtonElement | undefined => {
47+ const buttons = container . querySelectorAll ( 'button' ) ;
48+ return Array . from ( buttons ) . find ( btn => btn . textContent === text ) as HTMLButtonElement | undefined ;
49+ } ;
50+
4151const storyTests : Record < string , ( result : RenderResult ) => void | Promise < void > > = {
4252 'RenderThingsInDifferentPlaces' : ( { container } ) => {
4353 expect ( container . innerHTML ) . toContain (
@@ -54,17 +64,13 @@ const storyTests: Record<string, (result: RenderResult) => void | Promise<void>>
5464 ) ;
5565 } ,
5666 'SwapNodesBetweenDifferentLocations' : async ( { container, getByText } ) => {
57- const spans = container . querySelectorAll ( 'span' ) ;
58- const order = Array . from ( spans ) . map ( span => span . textContent ) ;
59- expect ( order ) . toEqual ( [ '0' , '1' , '2' , '3' , '4' ] ) ;
67+ expect ( getSpanOrder ( container ) ) . toEqual ( [ '0' , '1' , '2' , '3' , '4' ] ) ;
6068
6169 const button = getByText ( 'Click to reverse the order' ) ;
6270 button . click ( ) ;
6371 await wait ( 10 ) ;
6472
65- const spansAfter = container . querySelectorAll ( 'span' ) ;
66- const orderAfter = Array . from ( spansAfter ) . map ( span => span . textContent ) ;
67- expect ( orderAfter ) . toEqual ( [ '4' , '3' , '2' , '1' , '0' ] ) ;
73+ expect ( getSpanOrder ( container ) ) . toEqual ( [ '4' , '3' , '2' , '1' , '0' ] ) ;
6874 } ,
6975 'CanPassAttributesOptionToCreateHtmlPortalNode' : async ( { container, getByText } ) => {
7076 expect ( container . querySelector ( '#div-id-1' ) ) . toBeNull ( ) ;
@@ -158,8 +164,7 @@ const storyTests: Record<string, (result: RenderResult) => void | Promise<void>>
158164 'CanSwitchBetweenPortalsSafely' : async ( { container, getByText } ) => {
159165 expect ( container . textContent ) . toContain ( 'Count is 0' ) ;
160166
161- const incrementButtons = container . querySelectorAll ( 'button' ) ;
162- const incrementButton = Array . from ( incrementButtons ) . find ( btn => btn . textContent === '+1' ) ;
167+ const incrementButton = findButtonByText ( container , '+1' ) ;
163168 expect ( incrementButton ) . not . toBeNull ( ) ;
164169
165170 incrementButton ! . click ( ) ;
@@ -173,9 +178,7 @@ const storyTests: Record<string, (result: RenderResult) => void | Promise<void>>
173178
174179 expect ( container . textContent ) . toContain ( 'Count is 0' ) ;
175180
176- const incrementButtonAfterSwap = Array . from ( container . querySelectorAll ( 'button' ) ) . find (
177- btn => btn . textContent === '+1'
178- ) ;
181+ const incrementButtonAfterSwap = findButtonByText ( container , '+1' ) ;
179182 incrementButtonAfterSwap ! . click ( ) ;
180183 await wait ( 10 ) ;
181184 incrementButtonAfterSwap ! . click ( ) ;
0 commit comments