11import { 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- } ) ) ;
2+ import { initHighchartsAccessibility } from "@/main.js" ;
273
4+ // Mock the simple-analytics-vue
285vi . mock ( "simple-analytics-vue" , async ( ) => {
296 return {
307 default : {
@@ -33,6 +10,7 @@ vi.mock("simple-analytics-vue", async () => {
3310 } ;
3411} ) ;
3512
13+ // Mock the vue-gtag
3614vi . mock ( "vue-gtag" , async ( ) => {
3715 return {
3816 default : {
@@ -45,99 +23,45 @@ vi.mock("@/App.vue", () => ({
4523 default : { template : "<div>Mock App</div>" } ,
4624} ) ) ;
4725
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-
5526describe ( "main.js logic" , ( ) => {
5627 beforeEach ( ( ) => {
5728 vi . clearAllMocks ( ) ;
5829 } ) ;
5930
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 ) ;
31+ describe ( "Highcharts Initialization" , ( ) => {
32+ it ( "initializes when accessibility module is a direct function (CommonJS style)" , ( ) => {
33+ // Setup
34+ const mockInitFn = vi . fn ( ) ;
35+ const mockHighcharts = { name : "Highcharts Mock" } ;
6436
6537 // 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 ( ) ;
38+ initHighchartsAccessibility ( mockInitFn , mockHighcharts ) ;
8739
8840 // Assert
89- expect ( store . commit ) . toHaveBeenCalledWith (
90- "introspection/setMaintenanceMode" ,
91- ) ;
92- // Should NOT redirect to 500
93- expect ( router . replace ) . not . toHaveBeenCalled ( ) ;
41+ expect ( mockInitFn ) . toHaveBeenCalledWith ( mockHighcharts ) ;
9442 } ) ;
9543
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 ) ;
44+ it ( "initializes when accessibility module is a default export (ESM style)" , ( ) => {
45+ // Setup
46+ const mockInitObj = { default : vi . fn ( ) } ;
47+ const mockHighcharts = { name : "Highcharts Mock" } ;
10048
10149 // Execute
102- await bootstrapApp ( ) ;
50+ initHighchartsAccessibility ( mockInitObj , mockHighcharts ) ;
10351
10452 // 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- } ) ;
53+ expect ( mockInitObj . default ) . toHaveBeenCalledWith ( mockHighcharts ) ;
12154 } ) ;
12255
123- describe ( "cleanString" , ( ) => {
124- it ( "replaces underscores with spaces" , ( ) => {
125- expect ( globalFilters . cleanString ( "hello_world" ) ) . toContain (
126- "Hello world" ,
127- ) ;
128- } ) ;
56+ it ( "does nothing if accessibility module is invalid" , ( ) => {
57+ // Safety check (optional, but good for coverage)
58+ const mockInitObj = { } ; // No function, no default
59+ const mockHighcharts = { } ;
12960
130- it ( "capitalizes the first letter" , ( ) => {
131- expect ( globalFilters . cleanString ( "test_string" ) ) . toMatch ( / ^ T e s t / ) ;
132- } ) ;
61+ initHighchartsAccessibility ( mockInitObj , mockHighcharts ) ;
13362
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- } ) ;
63+ // Should not crash, and nothing called
64+ expect ( true ) . toBe ( true ) ;
14165 } ) ;
14266 } ) ;
14367} ) ;
0 commit comments