Skip to content

Commit f538695

Browse files
committed
Add unit tests for replacing file
1 parent 05e75d5 commit f538695

File tree

6 files changed

+103
-21
lines changed

6 files changed

+103
-21
lines changed

__mocks__/_pdf.buffer.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Loads a PDF file as a Buffer.
2+
3+
const fs = require('fs');
4+
5+
const file = fs.readFileSync('./__mocks__/_pdf.pdf');
6+
7+
const fileArrayBuffer = file.buffer;
8+
const fileBlob = new Blob([fileArrayBuffer], { type: 'application/pdf' });
9+
const fileFile = new File([fileArrayBuffer], { type: 'application/pdf' });
10+
const fileDataURI = `data:application/pdf;base64,${file.toString('base64')}`;
11+
12+
export {
13+
file,
14+
fileArrayBuffer,
15+
fileBlob,
16+
fileFile,
17+
fileDataURI,
18+
};

__mocks__/_pdf.pdf

524 KB
Binary file not shown.

__mocks__/_pdf2.buffer.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Loads a PDF file as a Buffer.
2+
3+
const fs = require('fs');
4+
5+
const file = fs.readFileSync('./__mocks__/_pdf2.pdf');
6+
7+
const fileArrayBuffer = file.buffer;
8+
const fileBlob = new Blob([fileArrayBuffer], { type: 'application/pdf' });
9+
const fileFile = new File([fileArrayBuffer], { type: 'application/pdf' });
10+
const fileDataURI = `data:application/pdf;base64,${file.toString('base64')}`;
11+
12+
export {
13+
file,
14+
fileArrayBuffer,
15+
fileBlob,
16+
fileFile,
17+
fileDataURI,
18+
};

__mocks__/_pdf2.pdf

41.4 KB
Binary file not shown.

src/__tests__/Document.jsx

+67-16
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ import { mount } from 'enzyme';
33

44
import { Document } from '../entry.noworker';
55

6-
import file from './_pdf.buffer';
6+
import {
7+
fileArrayBuffer,
8+
fileBlob,
9+
fileFile,
10+
fileDataURI,
11+
} from '../../__mocks__/_pdf.buffer';
712

8-
const OK = Symbol('OK');
13+
import {
14+
fileFile as fileFile2,
15+
} from '../../__mocks__/_pdf2.buffer';
916

10-
const fileArrayBuffer = file.buffer;
11-
const fileBlob = new Blob([fileArrayBuffer], { type: 'application/pdf' });
12-
const fileFile = new File([fileArrayBuffer], { type: 'application/pdf' });
13-
const fileDataURI = `data:application/pdf;base64,${file.toString('base64')}`;
17+
const OK = Symbol('OK');
1418

1519
/* eslint-disable comma-dangle */
1620

@@ -28,9 +32,27 @@ const makeAsyncCallback = (callbackValue) => {
2832
return { promise, func };
2933
};
3034

35+
const desiredLoadedPdf = {
36+
loadingTask: {},
37+
pdfInfo: {
38+
fingerprint: 'a62067476e69734bb8eb60122615dfbf',
39+
numPages: 4,
40+
},
41+
transport: {},
42+
};
43+
const desiredLoadedPdf2 = {
44+
loadingTask: {},
45+
pdfInfo: {
46+
fingerprint: '04d9eadd32916d728460daa283b37ff2',
47+
numPages: 5,
48+
},
49+
transport: {},
50+
};
51+
52+
3153
describe('Document', () => {
3254
describe('loading', () => {
33-
it('loads a file via data URI properly', async () => {
55+
it('loads a file and calls onSourceSuccess and onLoadSuccess callbacks via data URI properly', async () => {
3456
const { func: onSourceSuccess, promise: onSourceSuccessPromise } = makeAsyncCallback(OK);
3557
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
3658

@@ -44,10 +66,10 @@ describe('Document', () => {
4466

4567
expect.assertions(2);
4668
await expect(onSourceSuccessPromise).resolves.toBe(OK);
47-
await expect(onLoadSuccessPromise).resolves.toBeInstanceOf(Object);
69+
await expect(onLoadSuccessPromise).resolves.toMatchObject(desiredLoadedPdf);
4870
});
4971

50-
it('loads a file via data URI properly (param object)', async () => {
72+
it('loads a file and calls onSourceSuccess and onLoadSuccess callbacks via data URI properly (param object)', async () => {
5173
const { func: onSourceSuccess, promise: onSourceSuccessPromise } = makeAsyncCallback(OK);
5274
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
5375

@@ -61,10 +83,10 @@ describe('Document', () => {
6183

6284
expect.assertions(2);
6385
await expect(onSourceSuccessPromise).resolves.toBe(OK);
64-
await expect(onLoadSuccessPromise).resolves.toBeInstanceOf(Object);
86+
await expect(onLoadSuccessPromise).resolves.toMatchObject(desiredLoadedPdf);
6587
});
6688

67-
it('loads a file via ArrayBuffer properly', async () => {
89+
it('loads a file and calls onSourceSuccess and onLoadSuccess callbacks via ArrayBuffer properly', async () => {
6890
const { func: onSourceSuccess, promise: onSourceSuccessPromise } = makeAsyncCallback(OK);
6991
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
7092

@@ -78,10 +100,10 @@ describe('Document', () => {
78100

79101
expect.assertions(2);
80102
await expect(onSourceSuccessPromise).resolves.toBe(OK);
81-
await expect(onLoadSuccessPromise).resolves.toBeInstanceOf(Object);
103+
await expect(onLoadSuccessPromise).resolves.toMatchObject(desiredLoadedPdf);
82104
});
83105

84-
it('loads a file via Blob properly', async () => {
106+
it('loads a file and calls onSourceSuccess and onLoadSuccess callbacks via Blob properly', async () => {
85107
const { func: onSourceSuccess, promise: onSourceSuccessPromise } = makeAsyncCallback(OK);
86108
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
87109

@@ -95,10 +117,10 @@ describe('Document', () => {
95117

96118
expect.assertions(2);
97119
await expect(onSourceSuccessPromise).resolves.toBe(OK);
98-
await expect(onLoadSuccessPromise).resolves.toBeInstanceOf(Object);
120+
await expect(onLoadSuccessPromise).resolves.toMatchObject(desiredLoadedPdf);
99121
});
100122

101-
it('loads a file via File properly', async () => {
123+
it('loads a file and calls onSourceSuccess and onLoadSuccess callbacks via File properly', async () => {
102124
const { func: onSourceSuccess, promise: onSourceSuccessPromise } = makeAsyncCallback(OK);
103125
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
104126

@@ -112,7 +134,36 @@ describe('Document', () => {
112134

113135
expect.assertions(2);
114136
await expect(onSourceSuccessPromise).resolves.toBe(OK);
115-
await expect(onLoadSuccessPromise).resolves.toBeInstanceOf(Object);
137+
await expect(onLoadSuccessPromise).resolves.toMatchObject(desiredLoadedPdf);
138+
});
139+
140+
it('replaces a file properly', async () => {
141+
const { func: onSourceSuccess, promise: onSourceSuccessPromise } = makeAsyncCallback(OK);
142+
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
143+
144+
const mountedComponent = mount(
145+
<Document
146+
file={fileFile}
147+
onLoadSuccess={onLoadSuccess}
148+
onSourceSuccess={onSourceSuccess}
149+
/>
150+
);
151+
152+
expect.assertions(4);
153+
await expect(onSourceSuccessPromise).resolves.toBe(OK);
154+
await expect(onLoadSuccessPromise).resolves.toMatchObject(desiredLoadedPdf);
155+
156+
const { func: onSourceSuccess2, promise: onSourceSuccessPromise2 } = makeAsyncCallback(OK);
157+
const { func: onLoadSuccess2, promise: onLoadSuccessPromise2 } = makeAsyncCallback();
158+
159+
mountedComponent.setProps({
160+
file: fileFile2,
161+
onLoadSuccess: onLoadSuccess2,
162+
onSourceSuccess: onSourceSuccess2,
163+
});
164+
165+
await expect(onSourceSuccessPromise2).resolves.toBe(OK);
166+
await expect(onLoadSuccessPromise2).resolves.toMatchObject(desiredLoadedPdf2);
116167
});
117168
});
118169

src/__tests__/_pdf.buffer.js

-5
This file was deleted.

0 commit comments

Comments
 (0)