@@ -4,7 +4,7 @@ import { initCore } from './initCore.js';
44import { SvelteURL } from "svelte/reactivity" ;
55import { location } from "./Location.js" ;
66import { defaultTraceOptions , traceOptions } from "./trace.svelte.js" ;
7- import { defaultRoutingOptions , routingOptions } from "./options.js" ;
7+ import { defaultRoutingOptions , resetRoutingOptions , routingOptions } from "./options.js" ;
88import { logger } from "./Logger.js" ;
99
1010const initialUrl = 'http://example.com/' ;
@@ -19,13 +19,15 @@ const locationMock: Location = {
1919 on : vi . fn ( ) ,
2020 go : vi . fn ( ) ,
2121 navigate : vi . fn ( ) ,
22+ get path ( ) { return this . url . pathname ; } ,
2223} ;
2324
2425describe ( 'initCore' , ( ) => {
2526 let cleanup : ( ( ) => void ) | undefined ;
2627 afterEach ( ( ) => {
2728 vi . resetAllMocks ( ) ;
2829 cleanup ?.( ) ;
30+ resetRoutingOptions ( ) ;
2931 } ) ;
3032 test ( "Should initialize with all the expected default values." , ( ) => {
3133 // Act.
@@ -53,20 +55,10 @@ describe('initCore', () => {
5355 error : ( ) => { }
5456 } ;
5557
56- // Capture initial state
57- const initialLoggerIsOffLogger = logger !== globalThis . console ;
58- const initialRoutingOptions = {
59- hashMode : routingOptions . hashMode ,
60- defaultHash : routingOptions . defaultHash
61- } ;
62- const initialTraceOptions = {
63- routerHierarchy : traceOptions . routerHierarchy
64- } ;
65-
66- // Act - Initialize with custom options
58+ // Act - Initialize with custom options (use valid combo)
6759 cleanup = initCore ( locationMock , {
6860 hashMode : 'multi' ,
69- defaultHash : true ,
61+ defaultHash : 'customHash' ,
7062 logger : customLogger ,
7163 trace : {
7264 routerHierarchy : true
@@ -76,19 +68,19 @@ describe('initCore', () => {
7668 // Assert - Check that options were applied
7769 expect ( logger ) . toBe ( customLogger ) ;
7870 expect ( routingOptions . hashMode ) . toBe ( 'multi' ) ;
79- expect ( routingOptions . defaultHash ) . toBe ( true ) ;
71+ expect ( routingOptions . defaultHash ) . toBe ( 'customHash' ) ;
8072 expect ( traceOptions . routerHierarchy ) . toBe ( true ) ;
8173 expect ( location ) . toBeDefined ( ) ;
8274
8375 // Act - Cleanup
8476 cleanup ( ) ;
8577 cleanup = undefined ;
8678
87- // Assert - Check that everything was rolled back
88- expect ( logger !== globalThis . console ) . toBe ( initialLoggerIsOffLogger ) ; // Back to offLogger
89- expect ( routingOptions . hashMode ) . toBe ( initialRoutingOptions . hashMode ) ;
90- expect ( routingOptions . defaultHash ) . toBe ( initialRoutingOptions . defaultHash ) ;
91- expect ( traceOptions . routerHierarchy ) . toBe ( initialTraceOptions . routerHierarchy ) ;
79+ // Assert - Check that everything was rolled back to library defaults
80+ expect ( logger ) . not . toBe ( customLogger ) ; // Should revert to offLogger
81+ expect ( routingOptions . hashMode ) . toBe ( defaultRoutingOptions . hashMode ) ;
82+ expect ( routingOptions . defaultHash ) . toBe ( defaultRoutingOptions . defaultHash ) ;
83+ expect ( traceOptions . routerHierarchy ) . toBe ( defaultTraceOptions . routerHierarchy ) ;
9284 expect ( location ) . toBeNull ( ) ;
9385 } ) ;
9486 test ( "Should throw an error when called a second time without proper prior cleanup." , ( ) => {
@@ -103,53 +95,57 @@ describe('initCore', () => {
10395 } ) ;
10496 describe ( 'cleanup' , ( ) => {
10597 test ( "Should rollback everything to defaults." , async ( ) => {
106- // Arrange.
107- const uninitializedLogger = logger ;
98+ // Arrange - Initialize with custom options
10899 cleanup = initCore ( locationMock , {
109100 hashMode : 'multi' ,
110- defaultHash : true ,
101+ defaultHash : 'abc' ,
111102 trace : {
112103 routerHierarchy : ! defaultTraceOptions . routerHierarchy
113104 }
114105 } ) ;
115- // Verify options were applied
106+ // Verify options were applied (no type conversion occurs)
116107 expect ( routingOptions . hashMode ) . toBe ( 'multi' ) ;
117- expect ( routingOptions . defaultHash ) . toBe ( true ) ;
118- expect ( logger ) . not . toBe ( uninitializedLogger ) ;
108+ expect ( routingOptions . defaultHash ) . toBe ( 'abc' ) ;
109+ expect ( logger ) . toBe ( globalThis . console ) ; // Default logger when none specified
119110 expect ( traceOptions . routerHierarchy ) . toBe ( ! defaultTraceOptions . routerHierarchy ) ;
120111
121112 // Act - Cleanup
122113 cleanup ( ) ;
123114 cleanup = undefined ;
124115
125- // Assert - Check that routing options were reset to defaults
126- expect ( routingOptions . hashMode ) . toBe ( 'single' ) ;
127- expect ( routingOptions . defaultHash ) . toBe ( false ) ;
128- expect ( logger ) . toBe ( uninitializedLogger ) ;
116+ // Assert - Check that all options were reset to library defaults
117+ expect ( routingOptions . hashMode ) . toBe ( defaultRoutingOptions . hashMode ) ;
118+ expect ( routingOptions . defaultHash ) . toBe ( defaultRoutingOptions . defaultHash ) ;
119+ expect ( logger ) . not . toBe ( globalThis . console ) ; // Reverts to offLogger (uninitialized state)
129120 expect ( traceOptions ) . toEqual ( defaultTraceOptions ) ;
130121 } ) ;
131122 test ( "Should handle multiple init/cleanup cycles properly." , async ( ) => {
132- // Arrange.
133- const uninitializedLogger = logger ;
123+ // Capture initial logger state for comparison
124+ const initialLogger = logger ;
125+
126+ // First cycle
134127 cleanup = initCore ( locationMock , {
135128 logger : { debug : ( ) => { } , log : ( ) => { } , warn : ( ) => { } , error : ( ) => { } }
136129 } ) ;
137- expect ( logger ) . not . toBe ( uninitializedLogger ) ;
130+ expect ( logger ) . not . toBe ( initialLogger ) ;
138131 expect ( location ) . toBeDefined ( ) ;
132+
139133 cleanup ( ) ;
140134 cleanup = undefined ;
141- expect ( logger ) . toBe ( uninitializedLogger ) ;
135+ expect ( logger ) . toBe ( initialLogger ) ; // Back to initial state
142136 expect ( location ) . toBeNull ( ) ;
137+
138+ // Second cycle
143139 cleanup = initCore ( locationMock , { hashMode : 'multi' } ) ;
144140 expect ( routingOptions . hashMode ) . toBe ( 'multi' ) ;
145141 expect ( location ) . toBeDefined ( ) ;
146142
147- // Act.
143+ // Act - Final cleanup
148144 cleanup ( ) ;
149145 cleanup = undefined ;
150146
151- // Assert.
152- expect ( routingOptions . hashMode ) . toBe ( 'single' ) ;
147+ // Assert - Should revert to library defaults
148+ expect ( routingOptions . hashMode ) . toBe ( defaultRoutingOptions . hashMode ) ;
153149 expect ( location ) . toBeNull ( ) ;
154150 } ) ;
155151 } ) ;
0 commit comments