-
-
Notifications
You must be signed in to change notification settings - Fork 895
/
test-utils.ts
76 lines (67 loc) · 2.06 KB
/
test-utils.ts
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
import fs from 'node:fs';
import { vi } from 'vitest';
function makeAsyncCallbackWithoutValue<T extends unknown[]>() {
let promiseResolve: (args: T) => void;
const promise = new Promise<T>((resolve) => {
promiseResolve = resolve;
});
type Func = (...args: T) => void;
const func: Func = vi.fn((...args: T) => promiseResolve(args));
return { func, promise };
}
function makeAsyncCallbackWithValue<T>(value: T) {
let promiseResolve: (arg: T) => void;
const promise = new Promise<T>((resolve) => {
promiseResolve = resolve;
});
const func = vi.fn(() => promiseResolve(value));
return { func, promise };
}
export function makeAsyncCallback<T extends unknown[]>(): ReturnType<
typeof makeAsyncCallbackWithoutValue<T>
>;
export function makeAsyncCallback<T>(value?: T): ReturnType<typeof makeAsyncCallbackWithValue<T>>;
export function makeAsyncCallback<T>(value?: T) {
if (value === undefined) {
return makeAsyncCallbackWithoutValue();
}
return makeAsyncCallbackWithValue<T>(value);
}
export function loadPDF(path: string) {
const raw = fs.readFileSync(path);
const arrayBuffer = raw.buffer;
return {
raw,
get arrayBuffer() {
return new Uint8Array(raw).buffer;
},
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], 'test.pdf', { type: 'application/pdf' });
},
};
}
export function muteConsole() {
vi.spyOn(globalThis.console, 'log').mockImplementation(() => {
// Intentionally empty
});
vi.spyOn(globalThis.console, 'error').mockImplementation(() => {
// Intentionally empty
});
vi.spyOn(globalThis.console, 'warn').mockImplementation(() => {
// Intentionally empty
});
}
export function restoreConsole() {
vi.mocked(globalThis.console.log).mockRestore();
vi.mocked(globalThis.console.error).mockRestore();
vi.mocked(globalThis.console.warn).mockRestore();
}