@@ -2,7 +2,7 @@ import { render } from '@testing-library/react';
22
33import { Sound } from '../Sound' ;
44import { AudioSource } from '../types' ;
5- import { setupAudioContextMock } from './test-utils' ;
5+ import { resetMediaSpies , setupAudioContextMock } from './test-utils' ;
66
77const httpSource : AudioSource = { url : '/a.mp3' , protocol : 'http' } ;
88
@@ -20,4 +20,34 @@ describe('Sound', () => {
2020
2121 restore ( ) ;
2222 } ) ;
23+
24+ it ( 'calls play after loading a new source while status is playing' , ( ) => {
25+ const { restore } = setupAudioContextMock ( ) ;
26+ const sourceA : AudioSource = { url : '/a.mp3' , protocol : 'http' } ;
27+ const sourceB : AudioSource = { url : '/b.mp3' , protocol : 'http' } ;
28+
29+ const { rerender } = render ( < Sound src = { sourceA } status = "playing" /> ) ;
30+
31+ const { playMock } = resetMediaSpies ( ) ;
32+
33+ rerender ( < Sound src = { sourceB } status = "playing" /> ) ;
34+
35+ expect ( playMock ) . toHaveBeenCalled ( ) ;
36+ restore ( ) ;
37+ } ) ;
38+
39+ it ( 'does not call play after loading a new source while status is paused' , ( ) => {
40+ const { restore } = setupAudioContextMock ( ) ;
41+ const sourceA : AudioSource = { url : '/a.mp3' , protocol : 'http' } ;
42+ const sourceB : AudioSource = { url : '/b.mp3' , protocol : 'http' } ;
43+
44+ const { rerender } = render ( < Sound src = { sourceA } status = "paused" /> ) ;
45+
46+ const { playMock } = resetMediaSpies ( ) ;
47+
48+ rerender ( < Sound src = { sourceB } status = "paused" /> ) ;
49+
50+ expect ( playMock ) . not . toHaveBeenCalled ( ) ;
51+ restore ( ) ;
52+ } ) ;
2353} ) ;
0 commit comments