Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/transloadit: send assembly-cancelled only once #3937

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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