Skip to content

Commit

Permalink
@uppy/transloadit: send assembly-cancelled only once (#3937)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 authored Aug 2, 2022
1 parent a6d6f0b commit 376e3aa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
49 changes: 47 additions & 2 deletions e2e/cypress/integration/dashboard-transloadit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -30,18 +32,61 @@ 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()

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', () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/@uppy/transloadit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 376e3aa

Please sign in to comment.