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: simplify plugin to always run a single assembly #5158

Merged
merged 26 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
69e6c26
Only create one assembly
Murderlon May 9, 2024
cd65aa2
TransloaditState.assemblies->assembly
Murderlon May 9, 2024
d8c7f07
Remove AssemblyOptions
Murderlon May 9, 2024
59e8c9c
Fix import
Murderlon May 13, 2024
728573a
rename assembly in state to assemblyResponse
Murderlon May 13, 2024
14db62b
make watcher and assemblies singular
Murderlon May 13, 2024
6002d69
Remove redundant uploadsAssemblies from state
Murderlon May 14, 2024
f3084ad
Merge branch '4.x' into transloadit-single-assembly
Murderlon May 14, 2024
2547da9
Merge branch '4.x' into transloadit-single-assembly
Murderlon May 27, 2024
383a520
Fix e2e test
Murderlon May 28, 2024
8b34b13
Remove test
Murderlon May 28, 2024
bdb40c2
test clean up
Murderlon May 29, 2024
f7f8c07
remove updateNumberOfFilesInAssembly
Murderlon May 29, 2024
c733d93
Remove AssemblyResponse from TransloaditState
Murderlon May 29, 2024
6f9fe8f
Apply feedback
Murderlon May 30, 2024
f4a9d0c
Make watcher private
Murderlon Jun 3, 2024
ce6ded7
Merge branch '4.x' into transloadit-single-assembly
Murderlon Jun 4, 2024
23911d0
Send along all metadata to Transloadit
Murderlon Jun 4, 2024
d32e0ab
Merge branch '4.x' into transloadit-single-assembly
Murderlon Jun 5, 2024
75e86ea
Improve tests
Murderlon Jun 5, 2024
2c4c0d9
Update getPlugin
Murderlon Jun 17, 2024
a939de8
Merge branch '4.x' into transloadit-single-assembly
Murderlon Jun 17, 2024
28e5e2a
Merge branch 'transloadit-single-assembly' of https://github.com/tran…
Murderlon Jun 17, 2024
023709d
Fix restoring files during upload
Murderlon Jun 17, 2024
d41063a
Fix test
Murderlon Jun 17, 2024
a6e74c8
Remove timeout
Murderlon Jun 17, 2024
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
235 changes: 56 additions & 179 deletions e2e/cypress/integration/dashboard-transloadit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,215 +1,86 @@
import Uppy from '@uppy/core'
import Transloadit from '@uppy/transloadit'

function getPlugin<M = any, B = any>(uppy: Uppy<M, B>) {
return uppy.getPlugin<Transloadit<M, B>>('Transloadit')!
}

describe('Dashboard with Transloadit', () => {
beforeEach(() => {
cy.visit('/dashboard-transloadit')
cy.get('.uppy-Dashboard-input:first').as('file-input')
cy.intercept('/assemblies').as('createAssemblies')
cy.intercept('/assemblies/*').as('assemblies')
cy.intercept('/resumable/*').as('resumable')
})

it('should upload cat image successfully', () => {
cy.get('@file-input').selectFile('cypress/fixtures/images/cat.jpg', {
force: true,
})

cy.get('.uppy-StatusBar-actionBtn--upload').click()
cy.wait(['@assemblies', '@resumable']).then(() => {
cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
})
})
it('should upload all files as a single assembly with UppyFile metadata in Upload-Metadata', () => {
cy.intercept({ path: '/assemblies', method: 'POST' }).as('createAssembly')

it('should close assembly polling when cancelled', () => {
cy.intercept({
method: 'GET',
url: '/assemblies/*',
}).as('assemblyPolling')
cy.intercept(
{ method: 'DELETE', pathname: '/assemblies/*', times: 1 },
{ statusCode: 204, body: {} },
).as('delete')
cy.get('@file-input').selectFile(
[
'cypress/fixtures/images/cat.jpg',
'cypress/fixtures/images/traffic.jpg',
],
{ force: true },
)

cy.window().then(({ uppy }) => {
cy.get('@file-input').selectFile(
[
'cypress/fixtures/images/cat.jpg',
'cypress/fixtures/images/traffic.jpg',
'cypress/fixtures/images/car.jpg',
],
{ force: true },
)
cy.get('.uppy-StatusBar-actionBtn--upload').click()

cy.wait(['@createAssemblies']).then(() => {
// eslint-disable-next-line
// @ts-ignore fix me
expect(
mifi marked this conversation as resolved.
Show resolved Hide resolved
Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every(
(a: any) => a.pollInterval,
),
).to.equal(true)
// Set metadata on all files
uppy.setMeta({ sharedMetaProperty: 'bar' })
const [file1, file2] = uppy.getFiles()
// Set unique metdata per file as before that's how we determined to create multiple assemblies
uppy.setFileMeta(file1.id, { one: 'one' })
uppy.setFileMeta(file2.id, { two: 'two' })

uppy.cancelAll()
cy.get('.uppy-StatusBar-actionBtn--upload').click()

cy.wait(['@delete']).then(() => {
// eslint-disable-next-line
// @ts-ignore fix me
expect(
Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some(
(a: any) => a.pollInterval,
),
).to.equal(false)
})
cy.intercept('POST', '/resumable/*', (req) => {
expect(req.headers['upload-metadata']).to.include('sharedMetaProperty')
req.continue()
})
})
})

// Too flaky at the moment. Arguably, this is not the right place
// as this is doing white box testing (testing internal state).
// But E2e is more about black box testing, you don’t care about the internals, only the result.
// May make more sense to turn this into a unit test.
it.skip('should emit one assembly-cancelled event when cancelled', () => {
const spy = cy.spy()

cy.window().then(({ uppy }) => {
// eslint-disable-next-line
// @ts-ignore fix me
uppy.on('transloadit:assembly-cancelled', spy)

cy.get('@file-input').selectFile(
[
'cypress/fixtures/images/cat.jpg',
'cypress/fixtures/images/traffic.jpg',
],
{ force: true },
)

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.get('.uppy-StatusBar-actionBtn--upload').click()
cy.wait('@assemblyPolling')
cy.get('button[data-cy=cancel]').click()
cy.wait('@assemblyDeletion')
// Unfortunately, waiting on a network request somehow often results in a race condition.
// We just want to know wether this is called or not, so waiting for 2 sec to be sure.
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(2000)
expect(spy).to.be.calledOnce
cy.wait(['@createAssembly']).then(() => {
cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
// should only create one assembly
cy.get('@createAssembly.all').should('have.length', 1)
})
})
})

it.skip('should close assembly polling when all files are removed', () => {
const spy = cy.spy()
it('should close assembly when cancelled', () => {
cy.intercept({ path: '/resumable/*', method: 'POST' }).as('tusCreate')
cy.intercept({ path: '/assemblies', method: 'POST' }).as('createAssemblies')
cy.intercept({ path: '/assemblies/*', method: 'DELETE' }).as('delete')
cy.intercept({ path: '/resumable/files/*', method: 'DELETE' }).as(
'tusDelete',
)

cy.window().then(({ uppy }) => {
// eslint-disable-next-line
// @ts-ignore fix me
uppy.on('transloadit:assembly-cancelled', spy)

cy.get('@file-input').selectFile(
[
'cypress/fixtures/images/cat.jpg',
'cypress/fixtures/images/traffic.jpg',
'cypress/fixtures/images/car.jpg',
],
{ force: true },
)

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.get('.uppy-StatusBar-actionBtn--upload').click()
cy.wait('@assemblyPolling')
// eslint-disable-next-line
// @ts-ignore fix me
expect(
Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every(
(a: any) => a.pollInterval,
),
).to.equal(true)

const { files } = uppy.getState()
// eslint-disable-next-line
// @ts-ignore fix me
uppy.removeFiles(Object.keys(files))

cy.wait('@assemblyDeletion').then(() => {
// eslint-disable-next-line
// @ts-ignore fix me
expect(
Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some(
(a: any) => a.pollInterval,
),
).to.equal(false)
expect(spy).to.be.calledOnce
})
})
})
cy.wait(['@createAssemblies', '@tusCreate']).then(() => {
const plugin = getPlugin(uppy)

it('should not create assembly when all individual files have been cancelled', () => {
cy.window().then(({ uppy }) => {
cy.get('@file-input').selectFile(
[
'cypress/fixtures/images/cat.jpg',
'cypress/fixtures/images/traffic.jpg',
],
{ force: true },
)
// eslint-disable-next-line
// @ts-ignore fix me
expect(
Object.values(uppy.getPlugin('Transloadit').activeAssemblies).length,
).to.equal(0)
expect(plugin.assembly.closed).to.be.false

cy.get('.uppy-StatusBar-actionBtn--upload').click()
uppy.cancelAll()

const { files } = uppy.getState()
// eslint-disable-next-line
// @ts-ignore fix me
uppy.removeFiles(Object.keys(files))

cy.wait('@createAssemblies').then(() => {
// eslint-disable-next-line
// @ts-ignore fix me
expect(
Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some(
(a: any) => a.pollInterval,
),
).to.equal(false)
cy.wait(['@delete', '@tusDelete']).then(() => {
expect(plugin.assembly.closed).to.be.true
})
})
})
})

it('should not emit error if upload is cancelled right away', () => {
cy.intercept({ path: '/assemblies', method: 'POST' }).as('createAssemblies')

cy.get('@file-input').selectFile('cypress/fixtures/images/cat.jpg', {
force: true,
})
Expand All @@ -228,7 +99,7 @@ describe('Dashboard with Transloadit', () => {
})
})

it('should not re-use erroneous tus keys', () => {
it.skip('should not re-use erroneous tus keys', () => {
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
function createAssemblyStatus({
message,
assembly_id,
Expand Down Expand Up @@ -444,6 +315,9 @@ describe('Dashboard with Transloadit', () => {
})

it('should complete on retry', () => {
cy.intercept('/assemblies/*').as('assemblies')
cy.intercept('/resumable/*').as('resumable')

cy.get('@file-input').selectFile(
[
'cypress/fixtures/images/cat.jpg',
Expand All @@ -470,9 +344,12 @@ describe('Dashboard with Transloadit', () => {
cy.wait(['@assemblies', '@resumable'])

cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
})
}).timeout(40000)
Murderlon marked this conversation as resolved.
Show resolved Hide resolved

it('should complete when resuming after pause', () => {
cy.intercept({ path: '/assemblies', method: 'POST' }).as('createAssemblies')
cy.intercept('/resumable/*').as('resumable')

cy.get('@file-input').selectFile(
[
'cypress/fixtures/images/cat.jpg',
Expand Down
2 changes: 1 addition & 1 deletion e2e/cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ import type { Uppy } from '@uppy/core'

declare global {
interface Window {
uppy: Uppy
uppy: Uppy<any, any>
}
}
Loading