Skip to content

Commit

Permalink
test(upload): add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cycleccc committed Aug 19, 2024
1 parent 1a60626 commit 2768f18
Showing 1 changed file with 101 additions and 1 deletion.
102 changes: 101 additions & 1 deletion packages/core/__tests__/upload/uploader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,44 @@ describe('uploader', () => {
})
})

test('it should invoke error callback if file be uploaded error', () => {
nock(server)
.defaultReplyHeaders({
'access-control-allow-method': 'POST',
'access-control-allow-origin': '*',
})
.options('/')
.reply(200, {})
.post('/')
.reply(200, {})

const fn = jest.fn()
console.error = fn

let uppy = createUploader({
server,
fieldName: 'file1',
metaWithUrl: false,
onSuccess: () => {
throw new Error('test onSuccess error')
},
onFailed: (file, res) => {},
onError: (file, err, res) => {},
})

// reference https://github.com/transloadit/uppy/blob/main/packages/%40uppy/xhr-upload/src/index.test.js
uppy.addFile({
source: 'jest',
name: 'foo.jpg',
type: 'image/jpeg',
data: new Blob([Buffer.alloc(8192)]),
})

return uppy.upload().catch(err => {
expect(fn).toBeCalled()
})
})

test('it should invoke onProgress callback if file be uploaded successfully', () => {
nock(server)
.defaultReplyHeaders({
Expand Down Expand Up @@ -190,9 +228,48 @@ describe('uploader', () => {
})
})

test('it should invoke error callback if file size over max size', () => {
test('it should invoke console.error method if file be uploaded failed and pass onError option throw error', () => {
nock(server)
.defaultReplyHeaders({
'access-control-allow-method': 'POST',
'access-control-allow-origin': '*',
})
.options('/')
.reply(200, {})
.post('/')
.reply(400, {})

const fn = jest.fn()
console.error = fn
const uppy = createUploader({
server,
fieldName: 'file1',
metaWithUrl: false,
onSuccess: () => {},
onFailed: (file, res) => {},
onError: () => {
throw new Error('test onError error')
},
} as any)

// reference https://github.com/transloadit/uppy/blob/main/packages/%40uppy/xhr-upload/src/index.test.js
uppy.addFile({
source: 'jest',
name: 'foo.jpg',
type: 'image/jpeg',
data: new Blob([Buffer.alloc(8192)]),
})

return uppy.upload().catch(err => {
expect(fn).toBeCalled()
})
})

test('it should invoke error callback if file size over max size', () => {
const fn = jest.fn()
const consoleFn = jest.fn()
console.error = consoleFn
let uppy = createUploader({
server,
fieldName: 'file1',
metaWithUrl: false,
Expand All @@ -212,5 +289,28 @@ describe('uploader', () => {
} catch (err) {
expect(fn).toBeCalled()
}

uppy = createUploader({
server,
fieldName: 'file1',
metaWithUrl: false,
onSuccess: () => {},
onFailed: (file, res) => {},
onError: () => {
throw new Error('test onError error')
},
maxFileSize: 5,
})

try {
uppy.addFile({
source: 'jest',
name: 'foo.jpg',
type: 'image/jpeg',
data: new Blob([Buffer.alloc(8192)]),
})
} catch (err) {
expect(consoleFn).toBeCalled()
}
})
})

0 comments on commit 2768f18

Please sign in to comment.