index.test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const Uppy = require('@uppy/core')
  2. const Webcam = require('./index')
  3. describe('Webcam', () => {
  4. describe('_getMediaRecorderOptions', () => {
  5. it('should not have a mimeType set if no preferences given', () => {
  6. global.MediaRecorder = {
  7. isTypeSupported: () => true,
  8. }
  9. const uppy = new Uppy().use(Webcam)
  10. expect(
  11. uppy.getPlugin('Webcam').getMediaRecorderOptions().mimeType,
  12. ).not.toBeDefined()
  13. })
  14. it('should use preferredVideoMimeType', () => {
  15. global.MediaRecorder = {
  16. isTypeSupported: (ty) => ty === 'video/webm',
  17. }
  18. const uppy = new Uppy().use(Webcam, { preferredVideoMimeType: 'video/webm' })
  19. expect(
  20. uppy.getPlugin('Webcam').getMediaRecorderOptions().mimeType,
  21. ).toEqual('video/webm')
  22. })
  23. it('should not use preferredVideoMimeType if it is not supported', () => {
  24. global.MediaRecorder = {
  25. isTypeSupported: (ty) => ty === 'video/webm',
  26. }
  27. const uppy = new Uppy().use(Webcam, { preferredVideoMimeType: 'video/mp4' })
  28. expect(
  29. uppy.getPlugin('Webcam').getMediaRecorderOptions().mimeType,
  30. ).not.toBeDefined()
  31. })
  32. it('should pick type based on `allowedFileTypes`', () => {
  33. global.MediaRecorder = {
  34. isTypeSupported: () => true,
  35. }
  36. const uppy = new Uppy({
  37. restrictions: { allowedFileTypes: ['video/mp4', 'video/webm'] },
  38. }).use(Webcam)
  39. expect(
  40. uppy.getPlugin('Webcam').getMediaRecorderOptions().mimeType,
  41. ).toEqual('video/mp4')
  42. })
  43. it('should use first supported type from allowedFileTypes', () => {
  44. global.MediaRecorder = {
  45. isTypeSupported: (ty) => ty === 'video/webm',
  46. }
  47. const uppy = new Uppy({
  48. restrictions: { allowedFileTypes: ['video/mp4', 'video/webm'] },
  49. }).use(Webcam)
  50. expect(
  51. uppy.getPlugin('Webcam').getMediaRecorderOptions().mimeType,
  52. ).toEqual('video/webm')
  53. })
  54. it('should prefer preferredVideoMimeType over allowedFileTypes', () => {
  55. global.MediaRecorder = {
  56. isTypeSupported: () => true,
  57. }
  58. const uppy = new Uppy({
  59. restrictions: { allowedFileTypes: ['video/mp4', 'video/webm'] },
  60. })
  61. .use(Webcam, { preferredVideoMimeType: 'video/webm' })
  62. expect(
  63. uppy.getPlugin('Webcam').getMediaRecorderOptions().mimeType,
  64. ).toEqual('video/webm')
  65. })
  66. it('should not use allowedFileTypes if they are unsupported', () => {
  67. global.MediaRecorder = {
  68. isTypeSupported: () => false,
  69. }
  70. const uppy = new Uppy({
  71. restrictions: { allowedFileTypes: ['video/mp4', 'video/webm'] },
  72. }).use(Webcam)
  73. expect(
  74. uppy.getPlugin('Webcam').getMediaRecorderOptions().mimeType,
  75. ).toEqual(undefined)
  76. })
  77. })
  78. })