@@ -8,6 +8,36 @@ const allStoryNames = Object.keys(stories).filter(key => key !== 'default');
88
99const  wait  =  ( ms : number )  =>  new  Promise ( resolve  =>  setTimeout ( resolve ,  ms ) ) ; 
1010
11+ const  waitForVideoToLoad  =  ( video : HTMLVideoElement ) : Promise < void >  =>  { 
12+   return  new  Promise < void > ( ( resolve ,  reject )  =>  { 
13+     const  timeout  =  setTimeout ( ( )  =>  reject ( new  Error ( 'Video load timeout after 30s' ) ) ,  30000 ) ; 
14+ 
15+     const  cleanup  =  ( )  =>  clearTimeout ( timeout ) ; 
16+ 
17+     if  ( video . readyState  >=  2 )  { 
18+       cleanup ( ) ; 
19+       resolve ( ) ; 
20+       return ; 
21+     } 
22+ 
23+     const  onSuccess  =  ( )  =>  { 
24+       cleanup ( ) ; 
25+       resolve ( ) ; 
26+     } ; 
27+ 
28+     const  onError  =  ( e : Event )  =>  { 
29+       cleanup ( ) ; 
30+       reject ( new  Error ( `Video failed to load: ${ ( e . target  as  HTMLVideoElement ) ?. error ?. message  ||  'unknown error' }  ` ) ) ; 
31+     } ; 
32+ 
33+     video . addEventListener ( 'loadeddata' ,  onSuccess ,  {  once : true  } ) ; 
34+     video . addEventListener ( 'canplay' ,  onSuccess ,  {  once : true  } ) ; 
35+     video . addEventListener ( 'error' ,  onError ,  {  once : true  } ) ; 
36+ 
37+     video . load ( ) ; 
38+   } ) ; 
39+ } ; 
40+ 
1141const  storyTests : Record < string ,  ( result : RenderResult )  =>  void   |  Promise < void > >  =  { 
1242  'RenderThingsInDifferentPlaces' : ( {  container } )  =>  { 
1343    expect ( container . innerHTML ) . toContain ( 
@@ -184,10 +214,32 @@ const storyTests: Record<string, (result: RenderResult) => void | Promise<void>>
184214    expect ( valueCounts [ '3' ]  +  valueCounts [ 'nothing' ] ) . toBeGreaterThanOrEqual ( 2 ) ; 
185215    expect ( seenValues . size ) . toBeGreaterThanOrEqual ( 2 ) ; 
186216  } , 
217+   'PersistDOMWhilstMoving' : async  ( {  container,  getAllByText } )  =>  { 
218+     const  video  =  container . querySelector ( 'video' )  as  HTMLVideoElement ; 
219+     expect ( video ) . not . toBeNull ( ) ; 
220+     expect ( video . src ) . toContain ( 'giphy.mp4' ) ; 
221+ 
222+     await  waitForVideoToLoad ( video ) ; 
223+     await  video . play ( ) ; 
224+     await  wait ( 200 ) ; 
225+ 
226+     expect ( video . paused ) . toBe ( false ) ; 
227+     const  playbackPosition  =  video . currentTime ; 
228+     expect ( playbackPosition ) . toBeGreaterThan ( 0 ) ; 
229+ 
230+     const  moveButtons  =  getAllByText ( 'Click to move the OutPortal' ) ; 
231+     moveButtons [ 0 ] . click ( ) ; 
232+     await  wait ( 50 ) ; 
233+ 
234+     const  videoAfterMove  =  container . querySelector ( 'video' )  as  HTMLVideoElement ; 
235+     expect ( videoAfterMove ) . toBe ( video ) ; 
236+     expect ( videoAfterMove . paused ) . toBe ( false ) ; 
237+     expect ( videoAfterMove . currentTime ) . toBeGreaterThanOrEqual ( playbackPosition ) ; 
238+     expect ( videoAfterMove . currentTime ) . toBeGreaterThan ( playbackPosition ) ; 
239+   } , 
187240} ; 
188241
189- // Skipped for now, until we have full test coverage of the stories: 
190- test . skip ( 'all stories have tests' ,  ( )  =>  { 
242+ test ( 'all stories have tests' ,  ( )  =>  { 
191243  const  testedStories  =  Object . keys ( storyTests ) ; 
192244  const  untestedStories  =  allStoryNames . filter ( name  =>  ! testedStories . includes ( name ) ) ; 
193245
0 commit comments