Notes/apps/client/spec/setup.ts

59 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-02-28 19:11:12 +02:00
import { beforeAll, vi } from "vitest";
import $ from "jquery";
injectGlobals();
2025-02-28 19:11:12 +02:00
beforeAll(() => {
vi.mock("../services/ws.js", mockWebsocket);
vi.mock("../services/server.js", mockServer);
2025-02-28 19:11:12 +02:00
});
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: []
}
}
},
2025-02-28 22:02:41 +02:00
async post(url: string, data: object) {
if (url === "tree/load") {
2025-02-28 22:02:41 +02:00
throw new Error(`A module tried to load from the server the following notes: ${((data as any).noteIds || []).join(",")}\nThis is not supported, use Froca mocking instead and ensure the note exist in the mock.`)
}
2025-02-28 19:11:12 +02:00
}
}
};
}