index.test.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { describe, expect, it } from '@jest/globals'
  2. import Core from '@uppy/core'
  3. import Transloadit from './index.js'
  4. import 'whatwg-fetch'
  5. describe('Transloadit', () => {
  6. it('Throws errors if options are missing', () => {
  7. const uppy = new Core()
  8. expect(() => {
  9. uppy.use(Transloadit, { params: {} })
  10. }).toThrowError(/The `params\.auth\.key` option is required/)
  11. })
  12. it('Accepts a JSON string as `params` for signature authentication', () => {
  13. const uppy = new Core()
  14. expect(() => {
  15. uppy.use(Transloadit, {
  16. params: 'not json',
  17. })
  18. }).toThrowError(/The `params` option is a malformed JSON string/)
  19. expect(() => {
  20. uppy.use(Transloadit, {
  21. params: '{"template_id":"some template id string"}',
  22. })
  23. }).toThrowError(/The `params\.auth\.key` option is required/)
  24. expect(() => {
  25. uppy.use(Transloadit, {
  26. params: '{"auth":{"key":"some auth key string"},"template_id":"some template id string"}',
  27. })
  28. }).not.toThrowError(/The `params\.auth\.key` option is required/)
  29. })
  30. it('Does not leave lingering progress if getAssemblyOptions fails', () => {
  31. const error = new Error('expected failure')
  32. const uppy = new Core()
  33. uppy.use(Transloadit, {
  34. getAssemblyOptions () {
  35. return Promise.reject(error)
  36. },
  37. })
  38. uppy.addFile({
  39. source: 'jest',
  40. name: 'abc',
  41. data: new Uint8Array(100),
  42. })
  43. return uppy.upload().then(() => {
  44. throw new Error('Should not have succeeded')
  45. }).catch((err) => {
  46. const fileID = Object.keys(uppy.getState().files)[0]
  47. expect(err).toBe(error)
  48. expect(uppy.getFile(fileID).progress.uploadStarted).toBe(null)
  49. })
  50. })
  51. it('Does not leave lingering progress if creating assembly fails', () => {
  52. const uppy = new Core()
  53. uppy.use(Transloadit, {
  54. params: {
  55. auth: { key: 'some auth key string' },
  56. template_id: 'some template id string',
  57. },
  58. })
  59. uppy.getPlugin('Transloadit').client.createAssembly = () => Promise.reject(new Error('VIDEO_ENCODE_VALIDATION'))
  60. uppy.addFile({
  61. source: 'jest',
  62. name: 'abc',
  63. data: new Uint8Array(100),
  64. })
  65. return uppy.upload().then(() => {
  66. throw new Error('Should not have succeeded')
  67. }, (err) => {
  68. const fileID = Object.keys(uppy.getState().files)[0]
  69. expect(err.message).toBe('Transloadit: Could not create Assembly: VIDEO_ENCODE_VALIDATION')
  70. expect(uppy.getFile(fileID).progress.uploadStarted).toBe(null)
  71. })
  72. })
  73. })