dashboard-transloadit.spec.ts 7.1 KB

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