|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { bootstrapApp, globalFilters } from "@/main.js"; // Adjust path |
| 3 | +import store from "@/store"; |
| 4 | +import router from "@/router"; |
| 5 | + |
| 6 | +// --- 1. MOCK DEPENDENCIES --- |
| 7 | +// We mock the modules that 'main.js' imports internally. |
| 8 | + |
| 9 | +// Mock the Store |
| 10 | +vi.mock("@/store", () => ({ |
| 11 | + default: { |
| 12 | + dispatch: vi.fn(), |
| 13 | + commit: vi.fn(), |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +// Mock the Router |
| 18 | +vi.mock("@/router", () => ({ |
| 19 | + default: { |
| 20 | + replace: vi.fn(), |
| 21 | + beforeEach: vi.fn(), |
| 22 | + afterEach: vi.fn(), |
| 23 | + }, |
| 24 | + beforeEach: vi.fn(), // Exported named functions from router |
| 25 | + afterEach: vi.fn(), |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock("simple-analytics-vue", async () => { |
| 29 | + return { |
| 30 | + default: { |
| 31 | + install: vi.fn(), // Mock the Vue plugin install method |
| 32 | + }, |
| 33 | + }; |
| 34 | +}); |
| 35 | + |
| 36 | +vi.mock("vue-gtag", async () => { |
| 37 | + return { |
| 38 | + default: { |
| 39 | + install: vi.fn(), // Mock the Vue plugin install method |
| 40 | + }, |
| 41 | + }; |
| 42 | +}); |
| 43 | + |
| 44 | +vi.mock("@/App.vue", () => ({ |
| 45 | + default: { template: "<div>Mock App</div>" }, |
| 46 | +})); |
| 47 | + |
| 48 | +// Mock External CSS/Plugins to prevent import errors during testing |
| 49 | +// vi.mock("tsparticles", () => ({ loadFull: vi.fn() })); |
| 50 | +// vi.mock("@tsparticles/vue3", () => ({ default: {} })); |
| 51 | +// vi.mock("vue-code-highlight/themes/prism-twilight.css", () => ({})); |
| 52 | +// vi.mock("vue-code-highlight/themes/window.css", () => ({})); |
| 53 | +// Add other css mocks if Vitest complains about parsing CSS |
| 54 | + |
| 55 | +describe("main.js logic", () => { |
| 56 | + beforeEach(() => { |
| 57 | + vi.clearAllMocks(); |
| 58 | + }); |
| 59 | + |
| 60 | + describe("bootstrapApp()", () => { |
| 61 | + it("dispatches the 4 required initialization actions on success", async () => { |
| 62 | + // Setup: Mock dispatch to resolve successfully |
| 63 | + store.dispatch.mockResolvedValue(true); |
| 64 | + |
| 65 | + // Execute |
| 66 | + await bootstrapApp(); |
| 67 | + |
| 68 | + // Assert: Check all 4 calls |
| 69 | + expect(store.dispatch).toHaveBeenCalledTimes(4); |
| 70 | + expect(store.dispatch).toHaveBeenCalledWith("users/login"); |
| 71 | + expect(store.dispatch).toHaveBeenCalledWith( |
| 72 | + "introspection/fetchParameters", |
| 73 | + ); |
| 74 | + expect(store.dispatch).toHaveBeenCalledWith( |
| 75 | + "searchFilters/assembleFilters", |
| 76 | + ); |
| 77 | + expect(store.dispatch).toHaveBeenCalledWith("messages/setMessages"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("commits maintenance mode if API returns 503 Service Unavailable", async () => { |
| 81 | + // Setup: specific 503 error structure |
| 82 | + const error503 = { response: { status: 503 } }; |
| 83 | + store.dispatch.mockRejectedValueOnce(error503); |
| 84 | + |
| 85 | + // Execute |
| 86 | + await bootstrapApp(); |
| 87 | + |
| 88 | + // Assert |
| 89 | + expect(store.commit).toHaveBeenCalledWith( |
| 90 | + "introspection/setMaintenanceMode", |
| 91 | + ); |
| 92 | + // Should NOT redirect to 500 |
| 93 | + expect(router.replace).not.toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("redirects to /error/500 for generic errors", async () => { |
| 97 | + // Setup: Generic error |
| 98 | + const errorGeneric = new Error("Something exploded"); |
| 99 | + store.dispatch.mockRejectedValueOnce(errorGeneric); |
| 100 | + |
| 101 | + // Execute |
| 102 | + await bootstrapApp(); |
| 103 | + |
| 104 | + // Assert |
| 105 | + expect(store.commit).not.toHaveBeenCalledWith( |
| 106 | + "introspection/setMaintenanceMode", |
| 107 | + ); |
| 108 | + expect(router.replace).toHaveBeenCalledWith("/error/500"); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("Global Filters", () => { |
| 113 | + describe("capitalize", () => { |
| 114 | + it("capitalizes the first letter of a string", () => { |
| 115 | + expect(globalFilters.capitalize("hello")).toBe("Hello"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("returns empty string if input is null/undefined", () => { |
| 119 | + expect(globalFilters.capitalize(null)).toBe(""); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe("cleanString", () => { |
| 124 | + it("replaces underscores with spaces", () => { |
| 125 | + expect(globalFilters.cleanString("hello_world")).toContain( |
| 126 | + "Hello world", |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + it("capitalizes the first letter", () => { |
| 131 | + expect(globalFilters.cleanString("test_string")).toMatch(/^Test/); |
| 132 | + }); |
| 133 | + |
| 134 | + // Note: Based on your current regex logic `replace(/([A-Z])/g, "$1")`, |
| 135 | + // "camelCase" remains "camelCase". If you intend to split it, update logic in main.js. |
| 136 | + it("handles camelCase based on current logic", () => { |
| 137 | + const result = globalFilters.cleanString("camelCase"); |
| 138 | + // Logic check: "camelCase" -> "CamelCase" (capitalizes first char, keeps rest) |
| 139 | + expect(result).toBe("CamelCase"); |
| 140 | + }); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments