Skip to content

Commit cd2c4e2

Browse files
Merge pull request #13222 from timvandermeij/unit-test-async
Convert done callbacks to async/await in the smaller unit test files
2 parents eddb908 + c1e9f60 commit cd2c4e2

7 files changed

+133
-204
lines changed

test/unit/custom_spec.js

+26-38
Original file line numberDiff line numberDiff line change
@@ -35,50 +35,41 @@ describe("custom canvas rendering", function () {
3535
let loadingTask;
3636
let page;
3737

38-
beforeAll(function (done) {
38+
beforeAll(async function () {
3939
CanvasFactory = new DefaultCanvasFactory();
4040

4141
loadingTask = getDocument(transparentGetDocumentParams);
42-
loadingTask.promise
43-
.then(function (doc) {
44-
return doc.getPage(1);
45-
})
46-
.then(function (data) {
47-
page = data;
48-
done();
49-
})
50-
.catch(done.fail);
42+
const doc = await loadingTask.promise;
43+
const data = await doc.getPage(1);
44+
page = data;
5145
});
5246

53-
afterAll(function (done) {
47+
afterAll(async function () {
5448
CanvasFactory = null;
5549
page = null;
56-
loadingTask.destroy().then(done);
50+
await loadingTask.destroy();
5751
});
5852

59-
it("renders to canvas with a default white background", function (done) {
53+
it("renders to canvas with a default white background", async function () {
6054
const viewport = page.getViewport({ scale: 1 });
6155
const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
6256

6357
const renderTask = page.render({
6458
canvasContext: canvasAndCtx.context,
6559
viewport,
6660
});
67-
renderTask.promise
68-
.then(function () {
69-
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
70-
r: 255,
71-
g: 255,
72-
b: 255,
73-
a: 255,
74-
});
75-
CanvasFactory.destroy(canvasAndCtx);
76-
done();
77-
})
78-
.catch(done.fail);
61+
await renderTask.promise;
62+
63+
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
64+
r: 255,
65+
g: 255,
66+
b: 255,
67+
a: 255,
68+
});
69+
CanvasFactory.destroy(canvasAndCtx);
7970
});
8071

81-
it("renders to canvas with a custom background", function (done) {
72+
it("renders to canvas with a custom background", async function () {
8273
const viewport = page.getViewport({ scale: 1 });
8374
const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
8475

@@ -87,18 +78,15 @@ describe("custom canvas rendering", function () {
8778
viewport,
8879
background: "rgba(255,0,0,1.0)",
8980
});
90-
renderTask.promise
91-
.then(function () {
92-
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
93-
r: 255,
94-
g: 0,
95-
b: 0,
96-
a: 255,
97-
});
98-
CanvasFactory.destroy(canvasAndCtx);
99-
done();
100-
})
101-
.catch(done.fail);
81+
await renderTask.promise;
82+
83+
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
84+
r: 255,
85+
g: 0,
86+
b: 0,
87+
a: 255,
88+
});
89+
CanvasFactory.destroy(canvasAndCtx);
10290
});
10391
});
10492

test/unit/fetch_stream_spec.js

+14-22
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("fetch_stream", function () {
2020
const pdfUrl = new URL("../pdfs/tracemonkey.pdf", window.location).href;
2121
const pdfLength = 1016315;
2222

23-
it("read with streaming", function (done) {
23+
it("read with streaming", async function () {
2424
const stream = new PDFFetchStream({
2525
url: pdfUrl,
2626
disableStream: false,
@@ -47,18 +47,14 @@ describe("fetch_stream", function () {
4747
});
4848
};
4949

50-
const readPromise = Promise.all([read(), promise]);
51-
readPromise
52-
.then(function () {
53-
expect(len).toEqual(pdfLength);
54-
expect(isStreamingSupported).toEqual(true);
55-
expect(isRangeSupported).toEqual(false);
56-
done();
57-
})
58-
.catch(done.fail);
50+
await Promise.all([read(), promise]);
51+
52+
expect(len).toEqual(pdfLength);
53+
expect(isStreamingSupported).toEqual(true);
54+
expect(isRangeSupported).toEqual(false);
5955
});
6056

61-
it("read ranges with streaming", function (done) {
57+
it("read ranges with streaming", async function () {
6258
const rangeSize = 32768;
6359
const stream = new PDFFetchStream({
6460
url: pdfUrl,
@@ -98,20 +94,16 @@ describe("fetch_stream", function () {
9894
});
9995
};
10096

101-
const readPromise = Promise.all([
97+
await Promise.all([
10298
read(rangeReader1, result1),
10399
read(rangeReader2, result2),
104100
promise,
105101
]);
106-
readPromise
107-
.then(function () {
108-
expect(isStreamingSupported).toEqual(true);
109-
expect(isRangeSupported).toEqual(true);
110-
expect(fullReaderCancelled).toEqual(true);
111-
expect(result1.value).toEqual(rangeSize);
112-
expect(result2.value).toEqual(tailSize);
113-
done();
114-
})
115-
.catch(done.fail);
102+
103+
expect(isStreamingSupported).toEqual(true);
104+
expect(isRangeSupported).toEqual(true);
105+
expect(fullReaderCancelled).toEqual(true);
106+
expect(result1.value).toEqual(rangeSize);
107+
expect(result2.value).toEqual(tailSize);
116108
});
117109
});

test/unit/network_spec.js

+14-28
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("network", function () {
2020
const pdf1 = new URL("../pdfs/tracemonkey.pdf", window.location).href;
2121
const pdf1Length = 1016315;
2222

23-
it("read without stream and range", function (done) {
23+
it("read without stream and range", async function () {
2424
const stream = new PDFNetworkStream({
2525
url: pdf1,
2626
rangeChunkSize: 65536,
@@ -49,22 +49,15 @@ describe("network", function () {
4949
});
5050
};
5151

52-
const readPromise = Promise.all([read(), promise]);
53-
54-
readPromise
55-
.then(function (page) {
56-
expect(len).toEqual(pdf1Length);
57-
expect(count).toEqual(1);
58-
expect(isStreamingSupported).toEqual(false);
59-
expect(isRangeSupported).toEqual(false);
60-
done();
61-
})
62-
.catch(function (reason) {
63-
done.fail(reason);
64-
});
52+
await Promise.all([read(), promise]);
53+
54+
expect(len).toEqual(pdf1Length);
55+
expect(count).toEqual(1);
56+
expect(isStreamingSupported).toEqual(false);
57+
expect(isRangeSupported).toEqual(false);
6558
});
6659

67-
it("read custom ranges", function (done) {
60+
it("read custom ranges", async function () {
6861
// We don't test on browsers that don't support range request, so
6962
// requiring this test to pass.
7063
const rangeSize = 32768;
@@ -111,23 +104,16 @@ describe("network", function () {
111104
});
112105
};
113106

114-
const readPromises = Promise.all([
107+
await Promise.all([
115108
read(range1Reader, result1),
116109
read(range2Reader, result2),
117110
promise,
118111
]);
119112

120-
readPromises
121-
.then(function () {
122-
expect(result1.value).toEqual(rangeSize);
123-
expect(result2.value).toEqual(tailSize);
124-
expect(isStreamingSupported).toEqual(false);
125-
expect(isRangeSupported).toEqual(true);
126-
expect(fullReaderCancelled).toEqual(true);
127-
done();
128-
})
129-
.catch(function (reason) {
130-
done.fail(reason);
131-
});
113+
expect(result1.value).toEqual(rangeSize);
114+
expect(result2.value).toEqual(tailSize);
115+
expect(isStreamingSupported).toEqual(false);
116+
expect(isRangeSupported).toEqual(true);
117+
expect(fullReaderCancelled).toEqual(true);
132118
});
133119
});

test/unit/node_stream_spec.js

+22-35
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { AbortException } from "../../src/shared/util.js";
1818
import { isNodeJS } from "../../src/shared/is_node.js";
1919
import { PDFNodeStream } from "../../src/display/node_stream.js";
2020

21-
// Ensure that these test only runs in Node.js environments.
21+
// Ensure that these tests only run in Node.js environments.
2222
if (!isNodeJS) {
2323
throw new Error(
2424
'The "node_stream" unit-tests can only be run in Node.js environments.'
@@ -84,7 +84,7 @@ describe("node_stream", function () {
8484
server.close();
8585
});
8686

87-
it("read both http(s) and filesystem pdf files", function (done) {
87+
it("read both http(s) and filesystem pdf files", async function () {
8888
const stream1 = new PDFNodeStream({
8989
url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
9090
rangeChunkSize: 65536,
@@ -135,23 +135,17 @@ describe("node_stream", function () {
135135
});
136136
};
137137

138-
const readPromise = Promise.all([read1(), read2(), promise1, promise2]);
139-
readPromise
140-
.then(result => {
141-
expect(isStreamingSupported1).toEqual(false);
142-
expect(isRangeSupported1).toEqual(false);
143-
expect(isStreamingSupported2).toEqual(false);
144-
expect(isRangeSupported2).toEqual(false);
145-
expect(len1).toEqual(pdfLength);
146-
expect(len1).toEqual(len2);
147-
done();
148-
})
149-
.catch(reason => {
150-
done.fail(reason);
151-
});
138+
await Promise.all([read1(), read2(), promise1, promise2]);
139+
140+
expect(isStreamingSupported1).toEqual(false);
141+
expect(isRangeSupported1).toEqual(false);
142+
expect(isStreamingSupported2).toEqual(false);
143+
expect(isRangeSupported2).toEqual(false);
144+
expect(len1).toEqual(pdfLength);
145+
expect(len1).toEqual(len2);
152146
});
153147

154-
it("read custom ranges for both http(s) and filesystem urls", function (done) {
148+
it("read custom ranges for both http(s) and filesystem urls", async function () {
155149
const rangeSize = 32768;
156150
const stream1 = new PDFNodeStream({
157151
url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
@@ -225,7 +219,7 @@ describe("node_stream", function () {
225219
});
226220
};
227221

228-
const readPromises = Promise.all([
222+
await Promise.all([
229223
read(range11Reader, result11),
230224
read(range12Reader, result12),
231225
read(range21Reader, result21),
@@ -234,22 +228,15 @@ describe("node_stream", function () {
234228
promise2,
235229
]);
236230

237-
readPromises
238-
.then(function () {
239-
expect(result11.value).toEqual(rangeSize);
240-
expect(result12.value).toEqual(tailSize);
241-
expect(result21.value).toEqual(rangeSize);
242-
expect(result22.value).toEqual(tailSize);
243-
expect(isStreamingSupported1).toEqual(false);
244-
expect(isRangeSupported1).toEqual(true);
245-
expect(fullReaderCancelled1).toEqual(true);
246-
expect(isStreamingSupported2).toEqual(false);
247-
expect(isRangeSupported2).toEqual(true);
248-
expect(fullReaderCancelled2).toEqual(true);
249-
done();
250-
})
251-
.catch(function (reason) {
252-
done.fail(reason);
253-
});
231+
expect(result11.value).toEqual(rangeSize);
232+
expect(result12.value).toEqual(tailSize);
233+
expect(result21.value).toEqual(rangeSize);
234+
expect(result22.value).toEqual(tailSize);
235+
expect(isStreamingSupported1).toEqual(false);
236+
expect(isRangeSupported1).toEqual(true);
237+
expect(fullReaderCancelled1).toEqual(true);
238+
expect(isStreamingSupported2).toEqual(false);
239+
expect(isRangeSupported2).toEqual(true);
240+
expect(fullReaderCancelled2).toEqual(true);
254241
});
255242
});

0 commit comments

Comments
 (0)