mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 02:52:27 +08:00
refactor(test): global initialisation
This commit is contained in:
parent
7a34a2f59c
commit
f646e0f724
51
src/public/app/test-setup.ts
Normal file
51
src/public/app/test-setup.ts
Normal 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: []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -2,6 +2,7 @@ import { defineConfig } from "vitest/config";
|
|||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
test: {
|
test: {
|
||||||
|
setupFiles: ["./test-setup.ts"],
|
||||||
environment: "happy-dom",
|
environment: "happy-dom",
|
||||||
coverage: {
|
coverage: {
|
||||||
reporter: [ "text", "html" ]
|
reporter: [ "text", "html" ]
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
import $ from "jquery";
|
import { describe, it } from "vitest";
|
||||||
(window as any).$ = $;
|
|
||||||
|
|
||||||
import { beforeAll, describe, it, vi } from "vitest";
|
|
||||||
import utils from "../../services/utils.js";
|
import utils from "../../services/utils.js";
|
||||||
|
|
||||||
interface NoteDefinition {
|
interface NoteDefinition {
|
||||||
@ -54,48 +51,6 @@ async function buildNotes(notes: NoteDefinition[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("Building events", () => {
|
describe("Building events", () => {
|
||||||
|
|
||||||
beforeAll(async () => {
|
|
||||||
(window as any).WebSocket = () => {};
|
|
||||||
(window as any).glob = {
|
|
||||||
isMainWindow: true
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("../../services/ws.js", () => {
|
|
||||||
return {
|
|
||||||
default: {
|
|
||||||
subscribeToMessages(callback: (message: unknown) => void) {
|
|
||||||
// Do nothing.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
vi.mock("../../services/server.js", () => {
|
|
||||||
return {
|
|
||||||
default: {
|
|
||||||
async get(url: string) {
|
|
||||||
if (url === "options") {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (url === "keyboard-actions") {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (url === "tree") {
|
|
||||||
return {
|
|
||||||
branches: [],
|
|
||||||
notes: [],
|
|
||||||
attributes: []
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("supports start date", async () => {
|
it("supports start date", async () => {
|
||||||
const noteIds = await buildNotes([
|
const noteIds = await buildNotes([
|
||||||
{ title: "A", "#startDate": "2025-05-05" }
|
{ title: "A", "#startDate": "2025-05-05" }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user