/
index.test.js
113 lines (102 loc) · 3.12 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { jest, describe, it, expect } from '@jest/globals'
import nock from 'nock'
import Core from '@uppy/core'
import XHRUpload from './index.js'
describe('XHRUpload', () => {
describe('getResponseData', () => {
it('has the XHRUpload options as its `this`', () => {
nock('https://fake-endpoint.uppy.io')
.defaultReplyHeaders({
'access-control-allow-method': 'POST',
'access-control-allow-origin': '*',
})
.options('/').reply(200, {})
.post('/').reply(200, {})
const core = new Core()
const getResponseData = jest.fn(function getResponseData () {
expect(this.some).toEqual('option')
return {}
})
core.use(XHRUpload, {
id: 'XHRUpload',
endpoint: 'https://fake-endpoint.uppy.io',
some: 'option',
getResponseData,
})
core.addFile({
name: 'test.jpg',
data: new Blob([new Uint8Array(8192)]),
})
return core.upload().then(() => {
expect(getResponseData).toHaveBeenCalled()
})
})
})
describe('validateStatus', () => {
it('emit upload error under status code 200', () => {
nock('https://fake-endpoint.uppy.io')
.defaultReplyHeaders({
'access-control-allow-method': 'POST',
'access-control-allow-origin': '*',
})
.options('/').reply(200, {})
.post('/').reply(200, {
code: 40000,
message: 'custom upload error',
})
const core = new Core()
const validateStatus = jest.fn((status, responseText) => {
return JSON.parse(responseText).code !== 40000
})
core.use(XHRUpload, {
id: 'XHRUpload',
endpoint: 'https://fake-endpoint.uppy.io',
some: 'option',
validateStatus,
getResponseError (responseText) {
return JSON.parse(responseText).message
},
})
core.addFile({
name: 'test.jpg',
data: new Blob([new Uint8Array(8192)]),
})
return core.upload().then(result => {
expect(validateStatus).toHaveBeenCalled()
expect(result.failed.length).toBeGreaterThan(0)
result.failed.forEach(file => {
expect(file.error).toEqual('custom upload error')
})
})
})
})
describe('headers', () => {
it('can be a function', async () => {
const scope = nock('https://fake-endpoint.uppy.io')
.defaultReplyHeaders({
'access-control-allow-method': 'POST',
'access-control-allow-origin': '*',
'access-control-allow-headers': 'x-sample-header',
})
scope.options('/')
.reply(200, {})
scope.post('/')
.matchHeader('x-sample-header', 'test.jpg')
.reply(200, {})
const core = new Core()
core.use(XHRUpload, {
id: 'XHRUpload',
endpoint: 'https://fake-endpoint.uppy.io',
headers: (file) => ({
'x-sample-header': file.name,
}),
})
core.addFile({
name: 'test.jpg',
data: new Blob([new Uint8Array(8192)]),
})
await core.upload()
expect(scope.isDone()).toBe(true)
})
})
})