refactor(test): global initialisation

This commit is contained in:
Elian Doran
2025-02-28 19:11:12 +02:00
parent 7a34a2f59c
commit f646e0f724
3 changed files with 53 additions and 46 deletions

View File

@@ -0,0 +1,51 @@
import { beforeAll, vi } from "vitest";
import $ from "jquery";
beforeAll(() => {
injectGlobals();
vi.mock("./services/ws.js", mockWebsocket);
vi.mock("./services/server.js", mockServer);
});
function injectGlobals() {
const uncheckedWindow = window as any;
uncheckedWindow.$ = $;
uncheckedWindow.WebSocket = () => {};
uncheckedWindow.glob = {
isMainWindow: true
};
}
function mockWebsocket() {
return {
default: {
subscribeToMessages(callback: (message: unknown) => void) {
// Do nothing.
}
}
}
}
function mockServer() {
return {
default: {
async get(url: string) {
if (url === "options") {
return {};
}
if (url === "keyboard-actions") {
return [];
}
if (url === "tree") {
return {
branches: [],
notes: [],
attributes: []
}
}
}
}
};
}