Skip to content

Commit

Permalink
Refactor tests to async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Sep 23, 2021
1 parent 428b4fc commit fb2971c
Showing 1 changed file with 49 additions and 47 deletions.
96 changes: 49 additions & 47 deletions packages/@uppy/aws-s3-multipart/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ describe('AwsS3Multipart', () => {
awsS3Multipart = core.getPlugin('AwsS3Multipart')
})

it('Calls the prepareUploadParts function totalChunks / limit times', (done) => {
it('Calls the prepareUploadParts function totalChunks / limit times', async () => {
const scope = nock(
'https://bucket.s3.us-east-2.amazonaws.com'
).defaultReplyHeaders({
'access-control-allow-method': 'PUT',
'access-control-allow-origin': '*',
'access-control-expose-headers': 'ETag',
})
// 6MB file will give us 2 chunks, so there will be 2 PUT and 2 OPTIONS
// calls to the presigned URL from 1 prepareUploadParts calls
const fileSize = 5 * MB + 1 * MB

scope
.options((uri) => uri.includes('test/upload/multitest.dat'))
.reply(200, '')
Expand All @@ -87,9 +91,6 @@ describe('AwsS3Multipart', () => {
.put((uri) => uri.includes('test/upload/multitest.dat'))
.reply(200, '', { ETag: 'test2' })

// 6MB file will give us 2 chunks, so there will be 2 PUT and 2 OPTIONS
// calls to the presigned URL from 1 prepareUploadParts calls
const fileSize = 5 * MB + 1 * MB
core.addFile({
source: 'jest',
name: 'multitest.dat',
Expand All @@ -98,23 +99,31 @@ describe('AwsS3Multipart', () => {
type: 'application/octet-stream',
}),
})
core.upload().then(() => {
expect(
awsS3Multipart.opts.prepareUploadParts.mock.calls.length
).toEqual(1)
scope.done()
done()
})

await core.upload()

expect(
awsS3Multipart.opts.prepareUploadParts.mock.calls.length
).toEqual(1)

scope.done()
})

it('Calls prepareUploadParts with a Math.ceil(limit / 2) minimum, instead of one at a time for the remaining chunks after the first limit batch', (done) => {
it('Calls prepareUploadParts with a Math.ceil(limit / 2) minimum, instead of one at a time for the remaining chunks after the first limit batch', async () => {
const scope = nock(
'https://bucket.s3.us-east-2.amazonaws.com'
).defaultReplyHeaders({
'access-control-allow-method': 'PUT',
'access-control-allow-origin': '*',
'access-control-expose-headers': 'ETag',
})
// 50MB file will give us 10 chunks, so there will be 10 PUT and 10 OPTIONS
// calls to the presigned URL from 3 prepareUploadParts calls
//
// The first prepareUploadParts call will be for 5 parts, the second
// will be for 3 parts, the third will be for 2 parts.
const fileSize = 50 * MB

scope
.options((uri) => uri.includes('test/upload/multitest.dat'))
.reply(200, '')
Expand All @@ -123,12 +132,6 @@ describe('AwsS3Multipart', () => {
.reply(200, '', { ETag: 'test' })
scope.persist()

// 50MB file will give us 10 chunks, so there will be 10 PUT and 10 OPTIONS
// calls to the presigned URL from 3 prepareUploadParts calls
//
// The first prepareUploadParts call will be for 5 parts, the second
// will be for 3 parts, the third will be for 2 parts.
const fileSize = 50 * MB
core.addFile({
source: 'jest',
name: 'multitest.dat',
Expand All @@ -137,28 +140,28 @@ describe('AwsS3Multipart', () => {
type: 'application/octet-stream',
}),
})
core.upload().then(() => {
expect(
awsS3Multipart.opts.prepareUploadParts.mock.calls.length
).toEqual(3)
expect(awsS3Multipart.opts.prepareUploadParts.mock.calls[0][1].partNumbers).toEqual([1, 2, 3, 4, 5])
expect(awsS3Multipart.opts.prepareUploadParts.mock.calls[1][1].partNumbers).toEqual([6, 7, 8])
expect(awsS3Multipart.opts.prepareUploadParts.mock.calls[2][1].partNumbers).toEqual([9, 10])
const completeCall = awsS3Multipart.opts.completeMultipartUpload.mock.calls[0][1]
expect(completeCall.parts).toEqual([
{ ETag: 'test', PartNumber: 1 },
{ ETag: 'test', PartNumber: 2 },
{ ETag: 'test', PartNumber: 3 },
{ ETag: 'test', PartNumber: 4 },
{ ETag: 'test', PartNumber: 5 },
{ ETag: 'test', PartNumber: 6 },
{ ETag: 'test', PartNumber: 7 },
{ ETag: 'test', PartNumber: 8 },
{ ETag: 'test', PartNumber: 9 },
{ ETag: 'test', PartNumber: 10 },
])
done()
})

await core.upload()

expect(awsS3Multipart.opts.prepareUploadParts.mock.calls.length).toEqual(3)
expect(awsS3Multipart.opts.prepareUploadParts.mock.calls[0][1].partNumbers).toEqual([1, 2, 3, 4, 5])
expect(awsS3Multipart.opts.prepareUploadParts.mock.calls[1][1].partNumbers).toEqual([6, 7, 8])
expect(awsS3Multipart.opts.prepareUploadParts.mock.calls[2][1].partNumbers).toEqual([9, 10])

const completeCall = awsS3Multipart.opts.completeMultipartUpload.mock.calls[0][1]

expect(completeCall.parts).toEqual([
{ ETag: 'test', PartNumber: 1 },
{ ETag: 'test', PartNumber: 2 },
{ ETag: 'test', PartNumber: 3 },
{ ETag: 'test', PartNumber: 4 },
{ ETag: 'test', PartNumber: 5 },
{ ETag: 'test', PartNumber: 6 },
{ ETag: 'test', PartNumber: 7 },
{ ETag: 'test', PartNumber: 8 },
{ ETag: 'test', PartNumber: 9 },
{ ETag: 'test', PartNumber: 10 },
])
})
})

Expand Down Expand Up @@ -197,8 +200,9 @@ describe('AwsS3Multipart', () => {
awsS3Multipart = core.getPlugin('AwsS3Multipart')
})

it('retries prepareUploadParts when it fails once', (done) => {
it('retries prepareUploadParts when it fails once', async () => {
const fileSize = 5 * MB + 1 * MB

core.addFile({
source: 'jest',
name: 'multitest.dat',
Expand All @@ -207,12 +211,10 @@ describe('AwsS3Multipart', () => {
type: 'application/octet-stream',
}),
})
core.upload().then(() => {
expect(
awsS3Multipart.opts.prepareUploadParts.mock.calls.length
).toEqual(2)
done()
})

await core.upload()

expect(awsS3Multipart.opts.prepareUploadParts.mock.calls.length).toEqual(2)
})
})
})

0 comments on commit fb2971c

Please sign in to comment.