uppy/packages/@uppy/webcam/src/Webcam.test.ts
Antoine du Hamel aefb32533c
@uppy/webcam: refactor to TypeScript (#4870)
Co-authored-by: Merlijn Vos <merlijn@soverin.net>
2024-03-11 15:30:03 +01:00

113 lines
3.3 KiB
TypeScript

/* eslint-disable @typescript-eslint/ban-ts-comment */
import { describe, expect, it } from 'vitest'
import Uppy from '@uppy/core'
import Webcam from './index.ts'
describe('Webcam', () => {
describe('_getMediaRecorderOptions', () => {
it('should not have a mimeType set if no preferences given', () => {
// @ts-ignore
globalThis.MediaRecorder = {
isTypeSupported: () => true,
}
const uppy = new Uppy<any, any>().use(Webcam)
expect(
(uppy.getPlugin('Webcam') as Webcam<any, any>).getMediaRecorderOptions()
.mimeType,
).not.toBeDefined()
})
it('should use preferredVideoMimeType', () => {
// @ts-ignore
globalThis.MediaRecorder = {
isTypeSupported: (ty) => ty === 'video/webm',
}
const uppy = new Uppy().use(Webcam, {
preferredVideoMimeType: 'video/webm',
})
expect(
(uppy.getPlugin('Webcam') as Webcam<any, any>).getMediaRecorderOptions()
.mimeType,
).toEqual('video/webm')
})
it('should not use preferredVideoMimeType if it is not supported', () => {
// @ts-ignore
globalThis.MediaRecorder = {
isTypeSupported: (ty) => ty === 'video/webm',
}
const uppy = new Uppy().use(Webcam, {
preferredVideoMimeType: 'video/mp4',
})
expect(
(uppy.getPlugin('Webcam') as Webcam<any, any>).getMediaRecorderOptions()
.mimeType,
).not.toBeDefined()
})
it('should pick type based on `allowedFileTypes`', () => {
// @ts-ignore
globalThis.MediaRecorder = {
isTypeSupported: () => true,
}
const uppy = new Uppy({
restrictions: { allowedFileTypes: ['video/mp4', 'video/webm'] },
}).use(Webcam)
expect(
(uppy.getPlugin('Webcam') as Webcam<any, any>).getMediaRecorderOptions()
.mimeType,
).toEqual('video/mp4')
})
it('should use first supported type from allowedFileTypes', () => {
// @ts-ignore
globalThis.MediaRecorder = {
isTypeSupported: (ty) => ty === 'video/webm',
}
const uppy = new Uppy({
restrictions: { allowedFileTypes: ['video/mp4', 'video/webm'] },
}).use(Webcam)
expect(
(uppy.getPlugin('Webcam') as Webcam<any, any>).getMediaRecorderOptions()
.mimeType,
).toEqual('video/webm')
})
it('should prefer preferredVideoMimeType over allowedFileTypes', () => {
// @ts-ignore
globalThis.MediaRecorder = {
isTypeSupported: () => true,
}
const uppy = new Uppy({
restrictions: { allowedFileTypes: ['video/mp4', 'video/webm'] },
}).use(Webcam, {
preferredVideoMimeType: 'video/webm',
})
expect(
(uppy.getPlugin('Webcam') as Webcam<any, any>).getMediaRecorderOptions()
.mimeType,
).toEqual('video/webm')
})
it('should not use allowedFileTypes if they are unsupported', () => {
// @ts-ignore
globalThis.MediaRecorder = {
isTypeSupported: () => false,
}
const uppy = new Uppy({
restrictions: { allowedFileTypes: ['video/mp4', 'video/webm'] },
}).use(Webcam)
expect(
(uppy.getPlugin('Webcam') as Webcam<any, any>).getMediaRecorderOptions()
.mimeType,
).toEqual(undefined)
})
})
})