forked from muxinc/elements
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuploader.test.js
281 lines (218 loc) · 10.8 KB
/
uploader.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { server } from './utils/server';
import { fixture, assert, oneEvent, aTimeout, waitUntil } from '@open-wc/testing';
import '../src/index.ts';
describe('<mux-uploader>', () => {
let file = new File(['foo'], 'foo.mp4', {
type: 'video/mp4',
});
it('initiates as expected', async function () {
const uploader = await fixture(`<mux-uploader
endpoint="https://my-authenticated-url/storage?your-url-params"
></mux-uploader>`);
const drop = uploader.shadowRoot.querySelector('mux-uploader-drop');
assert.equal(
uploader.getAttribute('endpoint'),
'https://my-authenticated-url/storage?your-url-params',
'endpoint matches'
);
assert.isNotNull(drop, 'mux-uploader-drop is not null');
});
it('removes dropzone with no-drop param', async function () {
const uploader = await fixture(`<mux-uploader no-drop></mux-uploader>`);
const drop = uploader.shadowRoot.querySelector('mux-uploader-drop');
assert.isNull(drop, 'mux-uploader-drop is null');
});
it('should toggle the no-drop attribute when setting nodrop', async () => {
const uploader = await fixture(`<mux-uploader></mux-uploader>`);
uploader.noDrop = true;
assert.equal(uploader.hasAttribute('no-drop'), true, 'no-drop attr is set');
let drop = uploader.shadowRoot.querySelector('mux-uploader-drop');
assert.isNull(drop, 'mux-uploader-drop is null');
uploader.noDrop = false;
drop = uploader.shadowRoot.querySelector('mux-uploader-drop');
assert.equal(uploader.hasAttribute('no-drop'), false, 'no-drop attr is removed');
assert.isNotNull(drop, 'mux-uploader-drop is not null');
});
it('removes progress with no-progress param', async function () {
const uploader = await fixture(`<mux-uploader no-progress></mux-uploader>`);
const progress = uploader.shadowRoot.querySelector('mux-uploader-progress');
assert.isNull(progress, 'mux-uploader-progress is null');
});
it('should toggle the no-progress attribute when setting noprogress', async () => {
const uploader = await fixture(`<mux-uploader></mux-uploader>`);
uploader.noProgress = true;
assert.equal(uploader.hasAttribute('no-progress'), true, 'no-progress attr is set');
let progress = uploader.shadowRoot.querySelector('mux-uploader-progress');
assert.isNull(progress, 'mux-uploader-progress is null');
uploader.noProgress = false;
progress = uploader.shadowRoot.querySelector('mux-uploader-progress');
assert.equal(uploader.hasAttribute('no-progress'), false, 'no-progress attr is removed');
assert.isNotNull(progress, 'mux-uploader-progress is not null');
});
it('removes retry with no-retry param', async function () {
const uploader = await fixture(`<mux-uploader no-retry></mux-uploader>`);
const retry = uploader.shadowRoot.querySelector('mux-uploader-retry');
assert.isNull(retry, 'mux-uploader-retry is null');
});
it('should toggle the no-retry attribute when setting noretry', async () => {
const uploader = await fixture(`<mux-uploader></mux-uploader>`);
uploader.noRetry = true;
assert.equal(uploader.hasAttribute('no-retry'), true, 'no-retry attr is set');
let retry = uploader.shadowRoot.querySelector('mux-uploader-retry');
assert.isNull(retry, 'mux-uploader-retry is null');
uploader.noRetry = false;
retry = uploader.shadowRoot.querySelector('mux-uploader-retry');
assert.equal(uploader.hasAttribute('no-retry'), false, 'no-retry attr is removed');
assert.isNotNull(retry, 'mux-uploader-retry is not null');
});
it('removes status with no-status param', async function () {
const uploader = await fixture(`<mux-uploader no-status></mux-uploader>`);
const status = uploader.shadowRoot.querySelector('mux-uploader-status');
assert.isNull(status, 'mux-uploader-status is null');
});
it('should toggle the no-status attribute when setting nostatus', async () => {
const uploader = await fixture(`<mux-uploader></mux-uploader>`);
uploader.noStatus = true;
assert.equal(uploader.hasAttribute('no-status'), true, 'no-status attr is set');
let status = uploader.shadowRoot.querySelector('mux-uploader-status');
assert.isNull(status, 'mux-uploader-status is null');
uploader.noStatus = false;
status = uploader.shadowRoot.querySelector('mux-uploader-status');
assert.equal(uploader.hasAttribute('no-status'), false, 'no-status attr is removed');
assert.isNotNull(status, 'mux-uploader-status is not null');
});
it('does not init without endpoint', async function () {
const uploader = await fixture(`<mux-uploader></mux-uploader>`);
setTimeout(() => {
uploader.dispatchEvent(
new CustomEvent('file-ready', {
composed: true,
bubbles: true,
detail: file,
})
);
});
const { detail } = await oneEvent(uploader, 'uploaderror');
assert.equal(detail.message, 'No url or endpoint specified -- cannot handleUpload', 'error message matches');
assert.exists(uploader.getAttribute('upload-error'), 'upload error is true');
});
it('completes a mock upload', async function () {
server.respondWith('PUT', 'https://mock-upload-endpoint.com', [
200,
{ 'Content-Type': 'application/json' },
'{success: true}',
]);
const uploader = await fixture(`<mux-uploader
endpoint="https://mock-upload-endpoint.com"
></mux-uploader>`);
setTimeout(() => {
uploader.dispatchEvent(
new CustomEvent('file-ready', {
composed: true,
bubbles: true,
detail: file,
})
);
});
const { detail } = await oneEvent(uploader, 'uploadstart');
assert.equal(detail.chunkSize, 30720, 'chunk size matches');
assert.equal(detail.file, file, 'file matches');
assert.exists(uploader.getAttribute('upload-in-progress'), 'upload in progress is true');
await aTimeout(100);
server.respond();
assert.equal(server.lastRequest.url, 'https://mock-upload-endpoint.com', 'request url matches');
await oneEvent(uploader, 'success');
assert.equal(uploader.hasAttribute('upload-in-progress'), false, 'upload-in-progress attr is not set');
assert.equal(uploader.hasAttribute('upload-complete'), true, 'upload-complete attr is set');
});
it('completes a mock upload with an asynchronous endpoint function', async function () {
const endpointUrl = 'https://mock-upload-endpoint-2.com';
server.respondWith('PUT', endpointUrl, [200, { 'Content-Type': 'application/json' }, '{success: true}']);
const uploader = await fixture(`<mux-uploader></mux-uploader>`);
const endpoint = () => Promise.resolve(endpointUrl);
uploader.endpoint = endpoint;
assert.equal(uploader.endpoint, endpoint);
Promise.resolve(
uploader.dispatchEvent(
new CustomEvent('file-ready', {
composed: true,
bubbles: true,
detail: file,
})
)
);
// It should be in progress and not errored
await waitUntil(() => uploader.hasAttribute('upload-in-progress'), 'upload should be in progress');
await waitUntil(() => !uploader.hasAttribute('upload-error'), 'upload should not error');
await aTimeout(100);
server.respond();
assert.equal(server.lastRequest.url, endpointUrl, 'request url matches');
// NOTE: This didn't work bc the <mux-uploader>'s "upload-in-progress"/"upload-complete" attrs aren't actually updated, only <mux-uploader-progress>
// await waitUntil(() => uploader.hasAttribute('upload-complete') && !uploader.hasAttribute('upload-in-progress'));
const progressEl = uploader.shadowRoot.querySelector('mux-uploader-progress');
// Since we have a chain of awaits, instead of checking for a success event that may have been dispatched between awaits,
// we're instead just waiting for expected state changes.
await waitUntil(
() => progressEl.hasAttribute('upload-complete') && !progressEl.hasAttribute('upload-in-progress'),
'upload should eventually complete'
);
});
it('should not reset state based on updating the endpoint (since it will not impact the current upload)', async function () {
const endpointUrl = 'https://mock-upload-endpoint-2.com';
server.respondWith('PUT', endpointUrl, [200, { 'Content-Type': 'application/json' }, '{success: true}']);
const uploader = await fixture(`<mux-uploader></mux-uploader>`);
const endpoint = () => Promise.resolve(endpointUrl);
uploader.endpoint = endpoint;
assert.equal(uploader.endpoint, endpoint);
Promise.resolve(
uploader.dispatchEvent(
new CustomEvent('file-ready', {
composed: true,
bubbles: true,
detail: file,
})
)
);
// It should be in progress and not errored
await waitUntil(() => uploader.hasAttribute('upload-in-progress'), 'upload should be in progress');
await waitUntil(() => !uploader.hasAttribute('upload-error'), 'upload should not error');
const endpoint2 = () => Promise.resolve(endpointUrl);
uploader.endpoint = endpoint2;
// It should *still* be in progress and not errored
await waitUntil(() => uploader.hasAttribute('upload-in-progress'), 'upload should be in progress');
await waitUntil(() => !uploader.hasAttribute('upload-error'), 'upload should not error');
});
it('should set and get maxFileSize attribute correctly', async () => {
const uploader = await fixture(`<mux-uploader></mux-uploader>`);
uploader.maxFileSize = 1000000;
assert.equal(uploader.maxFileSize, 1000000, 'maxFileSize matches');
uploader.removeAttribute('max-file-size');
assert.equal(uploader.maxFileSize, undefined);
});
it('throws an error when uploading a file larger than maxFileSize', async function () {
const uploader = await fixture(`<mux-uploader></mux-uploader>`);
uploader.endpoint = 'https://example.com/upload';
uploader.maxFileSize = 1000; // Set max file size to 1KB
// Create a fake file larger than the maxFileSize
const testFile = new File([new ArrayBuffer(1024001)], 'testfile.mp4', { type: 'video/mp4' });
setTimeout(() => {
uploader.dispatchEvent(
new CustomEvent('file-ready', {
composed: true,
bubbles: true,
detail: testFile,
})
);
});
const { detail } = await oneEvent(uploader, 'uploaderror');
assert.equal(detail.message, 'file size exceeds maximum (1024001 > 1024000)', 'error message matches');
assert.exists(uploader.getAttribute('upload-error'), 'upload error is true');
});
it('should set and get chunkSize property correctly', async () => {
const uploader = await fixture(`<mux-uploader></mux-uploader>`);
uploader.chunkSize = 1024;
assert.equal(uploader.chunkSize, 1024, 'chunkSize matches');
uploader.chunkSize = undefined;
assert.equal(uploader.chunkSize, undefined, 'chunkSize matches');
});
});