-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupTests.ts
More file actions
151 lines (131 loc) · 4.06 KB
/
setupTests.ts
File metadata and controls
151 lines (131 loc) · 4.06 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import fs from "fs";
// vitest.setup.ts
import "vitest-canvas-mock";
import "@testing-library/jest-dom";
import { vi } from "vitest";
import polyfill from "./packages/excalidraw/polyfill";
import { yellow } from "./packages/excalidraw/tests/helpers/colorize";
import { testPolyfills } from "./packages/excalidraw/tests/helpers/polyfills";
// mock for pep.js not working with setPointerCapture()
HTMLElement.prototype.setPointerCapture = vi.fn();
Object.assign(globalThis, testPolyfills);
require("fake-indexeddb/auto");
// Mock localStorage for tests (not available in jsdom by default)
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: vi.fn((key: string) => store[key] ?? null),
setItem: vi.fn((key: string, value: string) => {
store[key] = String(value);
}),
removeItem: vi.fn((key: string) => {
delete store[key];
}),
clear: vi.fn(() => {
store = {};
}),
get length() {
return Object.keys(store).length;
},
key: vi.fn((index: number) => Object.keys(store)[index] ?? null),
};
})();
Object.defineProperty(window, "localStorage", {
value: localStorageMock,
writable: true,
});
// Mock sessionStorage as well (same interface)
Object.defineProperty(window, "sessionStorage", {
value: localStorageMock,
writable: true,
});
polyfill();
Object.defineProperty(window, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
Object.defineProperty(window, "FontFace", {
enumerable: true,
value: class {
private family: string;
private source: string;
private descriptors: any;
private status: string;
private unicodeRange: string;
constructor(family, source, descriptors) {
this.family = family;
this.source = source;
this.descriptors = descriptors;
this.status = "unloaded";
this.unicodeRange = "U+0000-00FF";
}
load() {
this.status = "loaded";
}
},
});
Object.defineProperty(document, "fonts", {
value: {
load: vi.fn().mockResolvedValue([]),
check: vi.fn().mockResolvedValue(true),
has: vi.fn().mockResolvedValue(true),
add: vi.fn(),
},
});
Object.defineProperty(window, "EXCALIDRAW_ASSET_PATH", {
value: `file://${__dirname}/`,
});
// mock the font fetch only, so that everything else, as font subsetting, can run inside of the (snapshot) tests
vi.mock(
"./packages/excalidraw/fonts/ExcalidrawFontFace",
async (importOriginal) => {
const mod = await importOriginal<
typeof import("./packages/excalidraw/fonts/ExcalidrawFontFace")
>();
const ExcalidrawFontFaceImpl = mod.ExcalidrawFontFace;
return {
...mod,
ExcalidrawFontFace: class extends ExcalidrawFontFaceImpl {
public async fetchFont(url: URL): Promise<ArrayBuffer> {
if (!url.toString().startsWith("file://")) {
return super.fetchFont(url);
}
// read local assets directly, without running a server
const content = await fs.promises.readFile(url);
return content.buffer;
}
},
};
},
);
// ReactDOM is located inside index.tsx file
// as a result, we need a place for it to render into
const element = document.createElement("div");
element.id = "root";
document.body.appendChild(element);
const _consoleError = console.error.bind(console);
console.error = (...args) => {
// the react's act() warning usually doesn't contain any useful stack trace
// so we're catching the log and re-logging the message with the test name,
// also stripping the actual component stack trace as it's not useful
if (args[0]?.includes?.("act(")) {
_consoleError(
yellow(
`<<< WARNING: test "${
expect.getState().currentTestName
}" does not wrap some state update in act() >>>`,
),
);
} else {
_consoleError(...args);
}
};