@@ -5,14 +5,29 @@ import HLSJS from 'hls.js'
55const simplePlaybackMock = new HlsjsPlayback ( { src : 'http://clappr.io/video.m3u8' } )
66
77describe ( 'HlsjsPlayback' , ( ) => {
8- test ( 'have a getter called template ' , ( ) => {
8+ test ( 'have a getter called defaultOptions ' , ( ) => {
99 expect ( Object . getOwnPropertyDescriptor ( Object . getPrototypeOf ( simplePlaybackMock ) , 'defaultOptions' ) . get ) . toBeTruthy ( )
1010 } )
1111
1212 test ( 'defaultOptions getter returns all the default options values into one object' , ( ) => {
1313 expect ( simplePlaybackMock . defaultOptions ) . toEqual ( { preload : true } )
1414 } )
1515
16+ test ( 'have a getter called customListeners' , ( ) => {
17+ expect ( Object . getOwnPropertyDescriptor ( Object . getPrototypeOf ( simplePlaybackMock ) , 'customListeners' ) . get ) . toBeTruthy ( )
18+ } )
19+
20+ test ( 'customListeners getter returns all configured custom listeners for each hls.js event' , ( ) => {
21+ const cb = ( ) => { }
22+ const playback = new HlsjsPlayback ( {
23+ src : 'http://clappr.io/foo.m3u8' ,
24+ hlsPlayback : {
25+ customListeners : [ { eventName : 'hlsMediaAttaching' , callback : cb } ]
26+ }
27+ } )
28+ expect ( playback . customListeners ) . toEqual ( playback . options . hlsPlayback . customListeners )
29+ } )
30+
1631 test ( 'should be able to identify it can play resources independently of the file extension case' , ( ) => {
1732 jest . spyOn ( HLSJS , 'isSupported' ) . mockImplementation ( ( ) => true )
1833 expect ( HlsjsPlayback . canPlay ( '/relative/video.m3u8' ) ) . toBeTruthy ( )
@@ -171,6 +186,14 @@ describe('HlsjsPlayback', () => {
171186
172187 expect ( playback . _manifestParsed ) . toBeTruthy ( )
173188 } )
189+
190+ test ( 'calls bindCustomListeners method' , ( ) => {
191+ const playback = new HlsjsPlayback ( { src : 'http://clappr.io/foo.m3u8' } )
192+ jest . spyOn ( playback , 'bindCustomListeners' )
193+ playback . _setup ( )
194+
195+ expect ( playback . bindCustomListeners ) . toHaveBeenCalledTimes ( 1 )
196+ } )
174197 } )
175198
176199 describe ( '_ready method' , ( ) => {
@@ -238,4 +261,77 @@ describe('HlsjsPlayback', () => {
238261 expect ( playback . _hls . loadSource ) . toHaveBeenCalledTimes ( 1 )
239262 } )
240263 } )
264+
265+ describe ( 'bindCustomListeners method' , ( ) => {
266+ test ( 'creates listeners for each item configured on customListeners array' , ( ) => {
267+ const cb = jest . fn ( )
268+ const playback = new HlsjsPlayback ( {
269+ src : 'http://clappr.io/foo.m3u8' ,
270+ hlsPlayback : {
271+ customListeners : [ { eventName : HLSJS . Events . MEDIA_ATTACHING , callback : cb } ]
272+ }
273+ } )
274+ playback . _setup ( )
275+
276+ expect ( cb ) . toHaveBeenCalledTimes ( 1 )
277+
278+ playback . _hls . trigger ( HLSJS . Events . MEDIA_ATTACHING )
279+
280+ expect ( cb ) . toHaveBeenCalledTimes ( 2 )
281+ } )
282+
283+ test ( 'don\'t add one listener without a valid configuration' , ( ) => {
284+ const cb = jest . fn ( )
285+ const playback = new HlsjsPlayback ( { src : 'http://clappr.io/foo.m3u8' } )
286+ playback . _setup ( )
287+
288+ expect ( cb ) . not . toHaveBeenCalled ( )
289+
290+ playback . options . hlsPlayback = { }
291+
292+ expect ( cb ) . not . toHaveBeenCalled ( )
293+
294+ playback . options . hlsPlayback . customListeners = [ ]
295+
296+ expect ( cb ) . not . toHaveBeenCalled ( )
297+
298+ playback . options . hlsPlayback . customListeners . push ( [ { eventName : 'invalid_name' , callback : cb } ] )
299+
300+ expect ( cb ) . not . toHaveBeenCalled ( )
301+ } )
302+
303+ test ( 'adds a listener for one time when the customListeners array item is configured with the "once" param' , ( ) => {
304+ const cb = jest . fn ( )
305+ const playback = new HlsjsPlayback ( {
306+ src : 'http://clappr.io/foo.m3u8' ,
307+ hlsPlayback : {
308+ customListeners : [ { eventName : HLSJS . Events . MEDIA_ATTACHING , callback : cb , once : true } ]
309+ }
310+ } )
311+ playback . _setup ( )
312+
313+ expect ( cb ) . toHaveBeenCalledTimes ( 1 )
314+
315+ playback . _hls . trigger ( HLSJS . Events . MEDIA_ATTACHING )
316+
317+ expect ( cb ) . toHaveBeenCalledTimes ( 1 )
318+ } )
319+ } )
320+
321+ describe ( 'unbindCustomListeners method' , ( ) => {
322+ test ( 'remove listeners for each item configured on customListeners array' , ( ) => {
323+ const cb = jest . fn ( )
324+ const playback = new HlsjsPlayback ( {
325+ src : 'http://clappr.io/foo.m3u8' ,
326+ hlsPlayback : {
327+ customListeners : [ { eventName : 'hlsFragLoaded' , callback : cb } ]
328+ }
329+ } )
330+ playback . _setup ( )
331+ playback . unbindCustomListeners ( )
332+ playback . _hls . trigger ( HLSJS . Events . FRAG_LOADED )
333+
334+ expect ( cb ) . not . toHaveBeenCalled ( )
335+ } )
336+ } )
241337} )
0 commit comments