dashboard-transloadit.spec.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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().then(() => {
  12. cy.wait(['@assemblies', '@resumable']).then(() => {
  13. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  14. })
  15. })
  16. })
  17. it('should close assembly polling when cancelled', () => {
  18. cy.intercept({
  19. method: 'GET',
  20. url: '/assemblies/*',
  21. }).as('assemblyPolling')
  22. cy.intercept(
  23. { method: 'DELETE', pathname: '/assemblies/*', times: 1 },
  24. { statusCode: 204, body: {} },
  25. ).as('delete')
  26. cy.window().then(({ uppy }) => {
  27. cy.get('@file-input').selectFile(
  28. ['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg', 'cypress/fixtures/images/car.jpg'],
  29. { force:true },
  30. ).then(() => {
  31. cy.get('.uppy-StatusBar-actionBtn--upload').click().then(() => {
  32. cy.wait(['@createAssemblies']).then(() => {
  33. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true)
  34. uppy.cancelAll()
  35. cy.wait(['@delete']).then(() => {
  36. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
  37. })
  38. })
  39. })
  40. })
  41. })
  42. })
  43. // Too flaky at the moment. Arguably, this is not the right place
  44. // as this is doing white box testing (testing internal state).
  45. // But E2e is more about black box testing, you don’t care about the internals, only the result.
  46. // May make more sense to turn this into a unit test.
  47. it.skip('should emit one assembly-cancelled event when cancelled', () => {
  48. const spy = cy.spy()
  49. cy.window().then(({ uppy }) => {
  50. uppy.on('transloadit:assembly-cancelled', spy)
  51. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  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.get('.uppy-StatusBar-actionBtn--upload').click().then(() => {
  68. cy.wait('@assemblyPolling').then(() => {
  69. cy.get('button[data-cy=cancel]').click().then(() => {
  70. cy.wait('@assemblyDeletion').then(() => {
  71. // Unfortunately, waiting on a network request somehow often results in a race condition.
  72. // We just want to know wether this is called or not, so waiting for 2 sec to be sure.
  73. // eslint-disable-next-line cypress/no-unnecessary-waiting
  74. cy.wait(2000).then(() => {
  75. expect(spy).to.be.calledOnce
  76. })
  77. })
  78. })
  79. })
  80. })
  81. })
  82. })
  83. it.skip('should close assembly polling when all files are removed', () => {
  84. const spy = cy.spy()
  85. cy.window().then(({ uppy }) => {
  86. uppy.on('transloadit:assembly-cancelled', spy)
  87. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  88. cy.intercept({
  89. method: 'GET',
  90. url: '/assemblies/*',
  91. }).as('assemblyPolling')
  92. cy.intercept(
  93. { method: 'PATCH', pathname: '/files/*', times: 1 },
  94. { statusCode: 204, body: {} },
  95. )
  96. cy.intercept(
  97. { method: 'DELETE', pathname: '/resumable/files/*', times: 2 },
  98. { statusCode: 204, body: {} },
  99. ).as('fileDeletion')
  100. cy.intercept(
  101. { method: 'DELETE', pathname: '/assemblies/*', times: 1 },
  102. ).as('assemblyDeletion')
  103. cy.get('.uppy-StatusBar-actionBtn--upload').click().then(() => {
  104. cy.wait('@assemblyPolling').then(() => {
  105. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true)
  106. const { files } = uppy.getState()
  107. uppy.removeFiles(Object.keys(files))
  108. cy.wait('@assemblyDeletion').then(() => {
  109. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
  110. expect(spy).to.be.calledOnce
  111. })
  112. })
  113. })
  114. })
  115. })
  116. it('should not create assembly when all individual files have been cancelled', () => {
  117. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  118. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  119. cy.window().then(({ uppy }) => {
  120. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).length).to.equal(0)
  121. const { files } = uppy.getState()
  122. uppy.removeFiles(Object.keys(files))
  123. cy.wait('@createAssemblies').then(() => {
  124. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
  125. })
  126. })
  127. })
  128. // Not working, the upstream changes have not landed yet.
  129. it.skip('should create assembly if there is still one file to upload', () => {
  130. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  131. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  132. cy.window().then(({ uppy }) => {
  133. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).length).to.equal(0)
  134. const { files } = uppy.getState()
  135. const [fileID] = Object.keys(files)
  136. uppy.removeFile(fileID)
  137. cy.wait('@createAssemblies').then(() => {
  138. cy.wait('@resumable')
  139. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  140. })
  141. })
  142. })
  143. // Not working, the upstream changes have not landed yet.
  144. it.skip('should complete upload if one gets cancelled mid-flight', () => {
  145. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  146. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  147. cy.wait('@createAssemblies')
  148. cy.wait('@resumable')
  149. cy.window().then(({ uppy }) => {
  150. const { files } = uppy.getState()
  151. const [fileID] = Object.keys(files)
  152. uppy.removeFile(fileID)
  153. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  154. })
  155. })
  156. it('should not emit error if upload is cancelled right away', () => {
  157. cy.get('@file-input').selectFile('cypress/fixtures/images/cat.jpg', { force:true })
  158. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  159. const handler = cy.spy()
  160. cy.window().then(({ uppy }) => {
  161. const { files } = uppy.getState()
  162. uppy.on('upload-error', handler)
  163. const [fileID] = Object.keys(files)
  164. uppy.removeFile(fileID)
  165. uppy.removeFile(fileID)
  166. cy.wait('@createAssemblies').then(() => expect(handler).not.to.be.called)
  167. })
  168. })
  169. })