-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathPlayer.js
355 lines (322 loc) · 10.3 KB
/
Player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { test } from 'zora'
import sinon from 'sinon'
import { create } from 'react-test-renderer'
import React from 'react'
import Player from '../src/Player'
// Prevent from Node.Timeout to hang the process
const oldSetTimeout = globalThis.setTimeout
globalThis.setTimeout = (callback, delay) => oldSetTimeout(callback, delay).unref?.()
const setProps = (wrapper, props) => {
const current = wrapper.toTree()
wrapper.update(React.createElement(current.type, {
...current.props,
...props
}))
}
test('componentWillUnmount()', t => {
const wrapper = create(<Player />)
const fake = sinon.fake()
wrapper.getInstance().isReady = true
wrapper.getInstance().player = { stop: fake }
wrapper.unmount()
t.ok(fake.calledOnce)
})
test('getInternalPlayer()', t => {
const wrapper = create(<Player />)
wrapper.getInstance().player = { abc: 123 }
t.ok(wrapper.getInstance().getInternalPlayer('abc') === 123)
})
test('getInternalPlayer() - null', t => {
const wrapper = create(<Player />)
wrapper.getInstance().player = null
t.ok(wrapper.getInstance().getInternalPlayer() === null)
})
test('player.load()', async t => {
const wrapper = create(<Player url='file.mp4' />)
const instance = wrapper.getInstance()
const fake = sinon.fake()
instance.handlePlayerMount({ load: fake })
instance.isLoading = false
instance.startOnPlay = false
instance.onDurationCalled = true
setProps(wrapper, { url: 'another-file.mp4' })
t.ok(fake.calledTwice)
t.ok(fake.calledWith('file.mp4'))
t.ok(fake.calledWith('another-file.mp4'))
t.ok(instance.isLoading)
t.ok(instance.startOnPlay)
t.notOk(instance.onDurationCalled)
})
test('set loadOnReady', t => {
const stub = sinon.stub(console, 'warn')
const wrapper = create(<Player url='file.mp4' activePlayer={() => null} />)
const instance = wrapper.getInstance()
instance.handlePlayerMount({ load: () => {} })
instance.isLoading = true
setProps(wrapper, { url: 'another-file.mp4' })
t.ok(stub.calledOnce)
t.ok(instance.loadOnReady === 'another-file.mp4')
stub.restore()
})
test('player.play()', t => {
const wrapper = create(<Player />)
const load = sinon.fake()
const play = sinon.fake()
wrapper.getInstance().handlePlayerMount({ load, play })
setProps(wrapper, { playing: true })
t.ok(play.calledOnce)
})
test('player.pause()', t => {
const wrapper = create(<Player playing />)
const load = sinon.fake()
const pause = sinon.fake()
wrapper.getInstance().handlePlayerMount({ load, pause })
wrapper.getInstance().isPlaying = true
setProps(wrapper, { playing: false })
t.ok(pause.calledOnce)
})
test('player.setVolume()', t => {
const wrapper = create(<Player volume={0.5} />)
const load = sinon.fake()
const setVolume = sinon.fake()
wrapper.getInstance().handlePlayerMount({ load, setVolume })
setProps(wrapper, { volume: 0.4 })
t.ok(setVolume.calledOnce)
})
test('player.mute()', t => {
const wrapper = create(<Player muted={false} />)
const load = sinon.fake()
const mute = sinon.fake()
wrapper.getInstance().handlePlayerMount({ load, mute })
setProps(wrapper, { muted: true })
t.ok(mute.calledOnce)
})
test('player.unmute()', async t => {
const wrapper = create(<Player muted volume={0.8} />)
const load = sinon.fake()
const unmute = sinon.fake()
const setVolume = sinon.fake()
wrapper.getInstance().handlePlayerMount({ load, unmute, setVolume })
setProps(wrapper, { muted: false })
t.ok(unmute.calledOnce)
return new Promise(resolve => setTimeout(() => {
t.ok(setVolume.calledOnceWith(0.8))
resolve()
}))
})
test('player.setPlaybackRate()', t => {
const wrapper = create(<Player playbackRate={1} />)
const load = sinon.fake()
const setPlaybackRate = sinon.fake()
wrapper.getInstance().handlePlayerMount({ load, setPlaybackRate })
setProps(wrapper, { playbackRate: 0.5 })
t.ok(setPlaybackRate.calledOnce)
})
const COMMON_METHODS = ['getDuration', 'getCurrentTime', 'getSecondsLoaded']
for (const method of COMMON_METHODS) {
test(`${method}()`, t => {
const instance = create(<Player />).getInstance()
instance.player = { [method]: () => 123 }
instance.isReady = true
t.ok(instance[method]() === 123)
})
test(`${method}() - null`, t => {
const instance = create(<Player />).getInstance()
t.ok(instance[method]() === null)
})
}
test('progress()', t => {
const load = sinon.fake()
const onProgress = sinon.fake()
const instance = create(<Player url='file.mp4' onProgress={onProgress} />).getInstance()
instance.handlePlayerMount({
load,
getCurrentTime: sinon.fake.returns(10),
getSecondsLoaded: sinon.fake.returns(20),
getDuration: sinon.fake.returns(40)
})
instance.isReady = true
instance.progress()
instance.progress() // Call twice to ensure onProgress is not called again
t.ok(onProgress.calledOnceWith({
loaded: 0.5,
loadedSeconds: 20,
played: 0.25,
playedSeconds: 10
}))
})
test('progress() handlePlayerMount', t => {
const load = sinon.fake()
const onProgress = sinon.fake()
const instance = create(<Player url='file.mp4' onProgress={onProgress} />).getInstance()
instance.isReady = true
instance.handlePlayerMount({
load,
getCurrentTime: sinon.fake.returns(10),
getSecondsLoaded: sinon.fake.returns(20),
getDuration: sinon.fake.returns(40)
})
t.ok(onProgress.calledWith({
loaded: 0.5,
loadedSeconds: 20,
played: 0.25,
playedSeconds: 10
}))
})
test('seekTo() - seconds', t => {
const load = sinon.fake()
const seekTo = sinon.fake()
const instance = create(<Player />).getInstance()
instance.handlePlayerMount({ load, seekTo })
instance.isReady = true
instance.seekTo(10)
t.ok(seekTo.calledOnceWith(10))
})
test('seekTo() - fraction', t => {
const load = sinon.fake()
const seekTo = sinon.fake()
const instance = create(<Player />).getInstance()
instance.handlePlayerMount({
load,
seekTo,
getDuration: sinon.fake.returns(10)
})
instance.isReady = true
instance.seekTo(0.5)
t.ok(seekTo.calledOnceWith(5))
})
test('seekTo() - warning', t => {
const stub = sinon.stub(console, 'warn')
const load = sinon.fake()
const seekTo = sinon.fake()
const instance = create(<Player />).getInstance()
instance.handlePlayerMount({
load,
seekTo,
getDuration: sinon.fake.returns(null)
})
instance.isReady = true
instance.seekTo(0.5)
t.ok(seekTo.notCalled)
t.ok(stub.calledOnce)
stub.restore()
})
test('seekTo() - set seekOnPlay', t => {
const load = sinon.fake()
const seekTo = sinon.fake()
const instance = create(<Player />).getInstance()
instance.handlePlayerMount({ load, seekTo })
instance.isReady = false
instance.seekTo(10)
t.ok(seekTo.notCalled)
t.ok(instance.seekOnPlay === 10)
})
test('onReady()', t => {
const onReady = sinon.fake()
const load = sinon.fake()
const setVolume = sinon.fake()
const play = sinon.fake()
const instance = create(<Player onReady={onReady} playing volume={1} />).getInstance()
instance.handlePlayerMount({ load, setVolume, play })
instance.handleDurationCheck = sinon.fake()
instance.isReady = true
instance.handleReady()
t.ok(setVolume.calledOnceWith(1))
t.ok(play.calledOnce)
})
test('loadOnReady', t => {
const load = sinon.fake()
const play = sinon.fake()
const instance = create(<Player />).getInstance()
instance.handlePlayerMount({ load, play })
instance.handleDurationCheck = sinon.fake()
instance.loadOnReady = 'file.mp4'
instance.handleReady()
t.ok(load.calledWith('file.mp4'))
t.ok(play.notCalled)
})
test('onPlay()', t => {
const onPlay = sinon.fake()
const instance = create(<Player onPlay={onPlay} />).getInstance()
instance.handlePlayerMount({ load: () => {} })
instance.handleDurationCheck = sinon.fake()
instance.handlePlay()
t.ok(onPlay.calledOnce)
t.ok(instance.isPlaying)
t.notOk(instance.isLoading)
})
test('onStart()', t => {
const onStart = sinon.fake()
const instance = create(<Player onStart={onStart} />).getInstance()
instance.handlePlayerMount({ load: () => {} })
instance.handleDurationCheck = sinon.fake()
instance.startOnPlay = true
instance.handlePlay()
t.ok(onStart.calledOnce)
t.notOk(instance.startOnPlay)
})
test('seekOnPlay', t => {
const seekTo = sinon.stub(Player.prototype, 'seekTo')
const instance = create(<Player />).getInstance()
instance.handlePlayerMount({ load: () => {} })
instance.handleDurationCheck = sinon.fake()
instance.seekOnPlay = 10
instance.handlePlay()
t.ok(seekTo.calledOnceWith(10))
t.ok(instance.seekOnPlay === null)
seekTo.restore()
})
test('onPause()', t => {
const onPause = sinon.fake()
const instance = create(<Player onPause={onPause} />).getInstance()
instance.isLoading = false
instance.handlePause()
t.ok(onPause.calledOnce)
t.notOk(instance.isPlaying)
})
test('onPause() - isLoading', t => {
const onPause = sinon.fake()
const instance = create(<Player onPause={onPause} />).getInstance()
instance.isLoading = true
instance.handlePause()
t.ok(onPause.notCalled)
})
test('onEnded()', t => {
const activePlayer = () => null
const onEnded = sinon.fake()
const instance = create(<Player activePlayer={activePlayer} onEnded={onEnded} />).getInstance()
instance.isPlaying = true
instance.handleEnded()
t.ok(onEnded.calledOnce)
t.notOk(instance.isPlaying)
})
test('loopOnEnded', t => {
const activePlayer = () => null
activePlayer.loopOnEnded = true
const seekTo = sinon.stub(Player.prototype, 'seekTo')
const instance = create(<Player loop activePlayer={activePlayer} />).getInstance()
instance.handlePlayerMount({ load: () => {} })
instance.isPlaying = true
instance.handleEnded()
t.ok(seekTo.calledOnceWith(0))
t.ok(instance.isPlaying)
seekTo.restore()
})
test('handleDurationCheck', t => {
const onDuration = sinon.fake()
const instance = create(<Player onDuration={onDuration} />).getInstance()
instance.getDuration = sinon.fake.returns(10)
instance.handleDurationCheck()
instance.handleDurationCheck() // Call twice to ensure onDuration is not called again
t.ok(onDuration.calledOnceWith(10))
t.ok(instance.onDurationCalled)
})
test('durationCheckTimeout', t => {
const onDuration = sinon.fake()
const instance = create(<Player onDuration={onDuration} />).getInstance()
instance.getDuration = sinon.fake.returns(null)
instance.durationCheckTimeout = null
instance.handleDurationCheck()
t.ok(onDuration.notCalled)
t.truthy(instance.durationCheckTimeout)
})