Skip to content

Commit 96e23f1

Browse files
committed
Add unit tests
1 parent 669dddf commit 96e23f1

File tree

1 file changed

+127
-3
lines changed

1 file changed

+127
-3
lines changed

test/index.test.ts

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,137 @@
11
import { expect } from 'chai';
22
import { VideoUploader } from '../src/index';
3+
import mock from 'xhr-mock';
34

45

56
describe('Instanciation', () => {
6-
77
it('throws if required param is missing', () => {
88
// @ts-ignore
99
expect(() => new VideoUploader({
1010
uploadToken: "aa",
11-
})).to.throw('"file" is missing');
11+
})).to.throw("'file' is missing");
12+
13+
// @ts-ignore
14+
expect(() => new VideoUploader({
15+
accessToken: "aa",
16+
file: new File([""], "")
17+
})).to.throw("'videoId' is missing");
18+
});
19+
});
20+
21+
describe('Content-range', () => {
22+
beforeEach(() => mock.setup());
23+
afterEach(() => mock.teardown());
24+
25+
it('content-range headers are properly set', (done) => {
26+
const uploadToken = "the-upload-token";
27+
28+
const uploader = new VideoUploader({
29+
file: new File([new ArrayBuffer(200)], "filename"),
30+
uploadToken,
31+
chunkSize: 50,
32+
});
33+
34+
const expectedRanges = [
35+
'bytes 0-49/200',
36+
'bytes 50-99/200',
37+
'bytes 100-149/200',
38+
'bytes 150-199/200',
39+
];
40+
41+
mock.post(`https://ws.api.video/upload?token=${uploadToken}`, (req, res) => {
42+
expect(req.header("content-range")).to.be.eq(expectedRanges.shift());
43+
return res.status(201).body("{}");
44+
});
45+
46+
uploader.upload().then(() => {
47+
expect(expectedRanges).has.lengthOf(0);
48+
done();
49+
});
50+
});
51+
});
52+
53+
describe('Access token auth', () => {
54+
beforeEach(() => mock.setup());
55+
afterEach(() => mock.teardown());
56+
57+
it('upload retries', (done) => {
58+
const accessToken = "1234";
59+
const videoId = "9876";
60+
61+
const uploader = new VideoUploader({
62+
file: new File([new ArrayBuffer(200)], "filename"),
63+
accessToken,
64+
videoId,
65+
});
66+
67+
68+
mock.post(`https://ws.api.video/videos/${videoId}/source`, (req, res) => {
69+
expect(req.header("content-range")).to.be.eq("bytes 0-199/200");
70+
expect(req.header("authorization")).to.be.eq(`Bearer ${accessToken}`);
71+
return res.status(201).body("{}");
72+
});
73+
74+
uploader.upload().then(() => {
75+
done();
76+
});
77+
1278
});
13-
});
79+
});
80+
81+
describe('Errors & retries', () => {
82+
beforeEach(() => mock.setup());
83+
afterEach(() => mock.teardown());
84+
85+
it('upload retries', (done) => {
86+
87+
const uploadToken = "the-upload-token";
88+
89+
const uploader = new VideoUploader({
90+
file: new File([new ArrayBuffer(200)], "filename"),
91+
uploadToken,
92+
});
93+
94+
let postCounts = 0;
95+
mock.post(`https://ws.api.video/upload?token=${uploadToken}`, (req, res) => {
96+
postCounts++;
97+
if (postCounts === 3) {
98+
return res.status(201).body("{}");
99+
}
100+
return res.status(500).body('{"error": "oups"}');
101+
});
102+
103+
uploader.upload().then(() => {
104+
expect(postCounts).to.be.eq(3);
105+
done();
106+
});
107+
}).timeout(10000);
108+
109+
it('failing upload returns the status from the api', (done) => {
110+
111+
const uploadToken = "the-upload-token";
112+
113+
const uploader = new VideoUploader({
114+
file: new File([new ArrayBuffer(200)], "filename"),
115+
uploadToken,
116+
chunkSize: 50,
117+
retries: 3,
118+
});
119+
120+
let postCounts = 0;
121+
mock.post(`https://ws.api.video/upload?token=${uploadToken}`, (req, res) => {
122+
postCounts++;
123+
if (postCounts > 2) {
124+
return res.status(500).body('{"error": "oups"}');
125+
}
126+
return res.status(201).body("{}");
127+
});
128+
129+
uploader.upload().then(() => {
130+
throw new Error('should not succeed');
131+
}).catch((e) => {
132+
expect(e.status).to.be.eq(500);
133+
expect(e.message).to.be.eq('{"error": "oups"}');
134+
done();
135+
});
136+
}).timeout(10000);
137+
});

0 commit comments

Comments
 (0)