diff --git a/e2e/cypress/integration/dashboard-transloadit.spec.ts b/e2e/cypress/integration/dashboard-transloadit.spec.ts index 5ece6bbcc8..9e08920941 100644 --- a/e2e/cypress/integration/dashboard-transloadit.spec.ts +++ b/e2e/cypress/integration/dashboard-transloadit.spec.ts @@ -18,6 +18,8 @@ describe('Dashboard with Transloadit', () => { }) it('should close assembly polling when cancelled', () => { + const spy = cy.spy() + cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true }) cy.get('.uppy-StatusBar-actionBtn--upload').click() @@ -30,11 +32,15 @@ describe('Dashboard with Transloadit', () => { { statusCode: 204, body: {} }, ) cy.intercept( - { method: 'DELETE', pathname: '/resumable/files/*', times: 1 }, + { method: 'DELETE', pathname: '/resumable/files/*', times: 2 }, { statusCode: 204, body: {} }, - ) + ).as('fileDeletion') + cy.intercept( + { method: 'DELETE', pathname: '/assemblies/*', times: 1 }, + ).as('assemblyDeletion') cy.wait('@assemblyPolling') cy.window().then(({ uppy }) => { + uppy.on('transloadit:assembly-cancelled', spy) expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true) }) cy.get('button[data-cy=cancel]').click() @@ -42,6 +48,45 @@ describe('Dashboard with Transloadit', () => { cy.window().then(({ uppy }) => { expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false) }) + cy.wait('@assemblyDeletion').then(() => { + expect(spy).to.be.calledOnce + }) + }) + + it('should close assembly polling when all files are removed', () => { + const spy = cy.stub() + + cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true }) + cy.get('.uppy-StatusBar-actionBtn--upload').click() + + cy.intercept({ + method: 'GET', + url: '/assemblies/*', + }).as('assemblyPolling') + cy.intercept( + { method: 'PATCH', pathname: '/files/*', times: 1 }, + { statusCode: 204, body: {} }, + ) + cy.intercept( + { method: 'DELETE', pathname: '/resumable/files/*', times: 2 }, + { statusCode: 204, body: {} }, + ).as('fileDeletion') + cy.intercept( + { method: 'DELETE', pathname: '/assemblies/*', times: 1 }, + ).as('assemblyDeletion') + cy.wait('@assemblyPolling') + cy.window().then(({ uppy }) => { + uppy.on('transloadit:assembly-cancelled', spy) + expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true) + + const { files } = uppy.getState() + uppy.removeFiles(Object.keys(files)) + + cy.wait('@assemblyDeletion').then(() => { + expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false) + expect(spy).to.be.calledOnce + }) + }) }) it('should not create assembly when all individual files have been cancelled', () => { diff --git a/packages/@uppy/transloadit/src/index.js b/packages/@uppy/transloadit/src/index.js index 22a664493b..7eeeac7b28 100644 --- a/packages/@uppy/transloadit/src/index.js +++ b/packages/@uppy/transloadit/src/index.js @@ -238,14 +238,13 @@ export default class Transloadit extends BasePlugin { const fileRemovedHandler = (fileRemoved, reason) => { if (reason === 'cancel-all') { assembly.close() - this.client.cancelAssembly(newAssembly).catch(() => { /* ignore potential errors */ }) this.uppy.off(fileRemovedHandler) } else if (fileRemoved.id in updatedFiles) { delete updatedFiles[fileRemoved.id] const nbOfRemainingFiles = Object.keys(updatedFiles).length if (nbOfRemainingFiles === 0) { assembly.close() - this.client.cancelAssembly(newAssembly).catch(() => { /* ignore potential errors */ }) + this.#cancelAssembly(newAssembly).catch(() => { /* ignore potential errors */ }) this.uppy.off(fileRemovedHandler) } else { this.client.updateNumberOfFilesInAssembly(newAssembly, nbOfRemainingFiles)