forked from wojtekmaj/react-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-utils.js
50 lines (44 loc) · 1.25 KB
/
test-utils.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
/* eslint-env jest */
const fs = require('fs');
export const makeAsyncCallback = (callbackValue) => {
let promiseResolve;
const promise = new Promise((resolve) => {
promiseResolve = resolve;
});
const func = jest.fn(
callbackValue
? () => promiseResolve(callbackValue)
: (...args) => promiseResolve(args.length === 1 ? args[0] : args),
);
return { promise, func };
};
export const loadPDF = (path) => {
const raw = fs.readFileSync(path);
const arrayBuffer = raw.buffer;
return {
raw,
arrayBuffer,
get blob() {
return new Blob([arrayBuffer], { type: 'application/pdf' });
},
get data() {
return new Uint8Array(raw);
},
get dataURI() {
return `data:application/pdf;base64,${raw.toString('base64')}`;
},
get file() {
return new File([arrayBuffer], { type: 'application/pdf' });
},
};
};
export const muteConsole = () => {
jest.spyOn(global.console, 'log').mockImplementation(() => {});
jest.spyOn(global.console, 'error').mockImplementation(() => {});
jest.spyOn(global.console, 'warn').mockImplementation(() => {});
};
export const restoreConsole = () => {
global.console.log.mockRestore();
global.console.error.mockRestore();
global.console.warn.mockRestore();
};