Skip to content

Commit

Permalink
Reject promise if the video throws an error (#9)
Browse files Browse the repository at this point in the history
Reject promise if the video throws an error

Co-authored-by: Ezekiel Victor <[email protected]>
  • Loading branch information
Stopa and evictor authored Apr 2, 2021
1 parent cabf157 commit 69d3db9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/getBlobDuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
export default async function getBlobDuration(blob) {
const tempVideoEl = document.createElement('video')

const durationP = new Promise(resolve =>
const durationP = new Promise((resolve, reject) => {
tempVideoEl.addEventListener('loadedmetadata', () => {
// Chrome bug: https://bugs.chromium.org/p/chromium/issues/detail?id=642012
if(tempVideoEl.duration === Infinity) {
Expand All @@ -20,8 +20,9 @@ export default async function getBlobDuration(blob) {
// Normal behavior
else
resolve(tempVideoEl.duration)
}),
)
})
tempVideoEl.onerror = (event) => reject(event.target.error)
})

tempVideoEl.src = typeof blob === 'string' || blob instanceof String
? blob
Expand Down
14 changes: 14 additions & 0 deletions test/getBlobDuration.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import getBlobDuration from '../src/getBlobDuration'

let dummyVideoEl, mockBlob
const dummyErrorEventObject = {
target: {
error: 'Dummy error message'
}
}

beforeEach(() => {
dummyVideoEl = jest.fn()
Expand Down Expand Up @@ -52,3 +57,12 @@ it('should execute Chrome bugfix duration retrieval as needed', async () => {
resolve()
})
})

it('should reject with the error object if an error occurs', async () => {
dummyVideoEl.duration = Infinity

// noinspection ES6MissingAwait
const durationP = getBlobDuration(mockBlob)
dummyVideoEl.onerror(dummyErrorEventObject)
await expect(durationP).rejects.toMatch(dummyErrorEventObject.target.error)
})

0 comments on commit 69d3db9

Please sign in to comment.