getBlobDuration.test.js
1.35 KB
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
import getBlobDuration from '../src/getBlobDuration'
let dummyVideoEl, mockBlob
beforeEach(() => {
dummyVideoEl = jest.fn()
dummyVideoEl.addEventListener = jest.fn((eventName, handler) => {
expect(eventName).toBe('loadedmetadata')
handler()
})
document.createElement = jest.fn(elType => {
expect(elType).toBe('video')
return dummyVideoEl
})
window.URL.createObjectURL = jest.fn()
mockBlob = jest.fn()
})
it('should handle ordinary duration retrieval', async () => {
dummyVideoEl.duration = 23741
const duration = await getBlobDuration(mockBlob)
expect(duration).toBe(23741)
expect(window.URL.createObjectURL).toHaveBeenCalledWith(mockBlob)
})
it('should handle ordinary duration retrieval', async () => {
dummyVideoEl.duration = 12345
const duration = await getBlobDuration('fake-blob-url')
expect(duration).toBe(12345)
expect(window.URL.createObjectURL).not.toHaveBeenCalled()
expect(dummyVideoEl.src).toBe('fake-blob-url')
})
it('should execute Chrome bugfix duration retrieval as needed', async () => {
dummyVideoEl.duration = Infinity
// noinspection ES6MissingAwait
const durationP = getBlobDuration(mockBlob)
await new Promise(async resolve => {
dummyVideoEl.duration = 98543
dummyVideoEl.ontimeupdate()
const duration = await durationP
expect(duration).toBe(98543)
resolve()
})
})