dashboard-transloadit.spec.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. describe('Dashboard with Transloadit', () => {
  2. beforeEach(() => {
  3. cy.visit('/dashboard-transloadit')
  4. cy.get('.uppy-Dashboard-input:first').as('file-input')
  5. cy.intercept('/assemblies').as('createAssemblies')
  6. cy.intercept('/assemblies/*').as('assemblies')
  7. cy.intercept('/resumable/*').as('resumable')
  8. })
  9. it('should upload cat image successfully', () => {
  10. cy.get('@file-input').selectFile('cypress/fixtures/images/cat.jpg', { force:true })
  11. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  12. cy.wait('@assemblies')
  13. cy.wait('@resumable')
  14. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  15. })
  16. it('should close assembly polling when cancelled', () => {
  17. const spy = cy.spy()
  18. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  19. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  20. cy.intercept({
  21. method: 'GET',
  22. url: '/assemblies/*',
  23. }).as('assemblyPolling')
  24. cy.intercept(
  25. { method: 'PATCH', pathname: '/files/*', times: 1 },
  26. { statusCode: 204, body: {} },
  27. )
  28. cy.intercept(
  29. { method: 'DELETE', pathname: '/resumable/files/*', times: 2 },
  30. { statusCode: 204, body: {} },
  31. ).as('fileDeletion')
  32. cy.intercept(
  33. { method: 'DELETE', pathname: '/assemblies/*', times: 1 },
  34. ).as('assemblyDeletion')
  35. cy.wait('@assemblyPolling')
  36. cy.window().then(({ uppy }) => {
  37. uppy.on('transloadit:assembly-cancelled', spy)
  38. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true)
  39. })
  40. cy.get('button[data-cy=cancel]').click()
  41. cy.window().then(({ uppy }) => {
  42. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
  43. })
  44. cy.wait('@assemblyDeletion').then(() => {
  45. expect(spy).to.be.calledOnce
  46. })
  47. })
  48. it('should close assembly polling when all files are removed', () => {
  49. const spy = cy.stub()
  50. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  51. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  52. cy.intercept({
  53. method: 'GET',
  54. url: '/assemblies/*',
  55. }).as('assemblyPolling')
  56. cy.intercept(
  57. { method: 'PATCH', pathname: '/files/*', times: 1 },
  58. { statusCode: 204, body: {} },
  59. )
  60. cy.intercept(
  61. { method: 'DELETE', pathname: '/resumable/files/*', times: 2 },
  62. { statusCode: 204, body: {} },
  63. ).as('fileDeletion')
  64. cy.intercept(
  65. { method: 'DELETE', pathname: '/assemblies/*', times: 1 },
  66. ).as('assemblyDeletion')
  67. cy.wait('@assemblyPolling')
  68. cy.window().then(({ uppy }) => {
  69. uppy.on('transloadit:assembly-cancelled', spy)
  70. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true)
  71. const { files } = uppy.getState()
  72. uppy.removeFiles(Object.keys(files))
  73. cy.wait('@assemblyDeletion').then(() => {
  74. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
  75. expect(spy).to.be.calledOnce
  76. })
  77. })
  78. })
  79. it('should not create assembly when all individual files have been cancelled', () => {
  80. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force: true })
  81. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  82. cy.window().then(({ uppy }) => {
  83. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).length).to.equal(0)
  84. const { files } = uppy.getState()
  85. uppy.removeFiles(Object.keys(files))
  86. cy.wait('@createAssemblies').then(() => {
  87. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
  88. })
  89. })
  90. })
  91. // Not working, the upstream changes have not landed yet.
  92. it.skip('should create assembly if there is still one file to upload', () => {
  93. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  94. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  95. cy.window().then(({ uppy }) => {
  96. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).length).to.equal(0)
  97. const { files } = uppy.getState()
  98. const [fileID] = Object.keys(files)
  99. uppy.removeFile(fileID)
  100. cy.wait('@createAssemblies').then(() => {
  101. cy.wait('@resumable')
  102. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  103. })
  104. })
  105. })
  106. // Not working, the upstream changes have not landed yet.
  107. it.skip('should complete upload if one gets cancelled mid-flight', () => {
  108. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  109. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  110. cy.wait('@createAssemblies')
  111. cy.wait('@resumable')
  112. cy.window().then(({ uppy }) => {
  113. const { files } = uppy.getState()
  114. const [fileID] = Object.keys(files)
  115. uppy.removeFile(fileID)
  116. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  117. })
  118. })
  119. it('should not emit error if upload is cancelled right away', () => {
  120. cy.get('@file-input').selectFile('cypress/fixtures/images/cat.jpg', { force:true })
  121. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  122. const handler = cy.spy()
  123. cy.window().then(({ uppy }) => {
  124. const { files } = uppy.getState()
  125. uppy.on('upload-error', handler)
  126. const [fileID] = Object.keys(files)
  127. uppy.removeFile(fileID)
  128. uppy.removeFile(fileID)
  129. cy.wait('@createAssemblies').then(() => expect(handler).not.to.be.called)
  130. })
  131. })
  132. })