Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions src/lib/core/Logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
import { logger, setLogger } from "./Logger.js";
import { logger, resetLogger, setLogger } from "./Logger.js";
import type { ILogger } from "$lib/types.js";

describe("logger", () => {
test("Should default to globalThis.console", () => {
expect(logger).toBe(globalThis.console);
test("Should default to offLogger (not console)", () => {
expect(logger).not.toBe(globalThis.console);
expect(logger.debug).toBeDefined();
expect(logger.log).toBeDefined();
expect(logger.warn).toBeDefined();
expect(logger.error).toBeDefined();
});
});

Expand Down Expand Up @@ -158,4 +162,45 @@ describe("setLogger", () => {
expect(mockLogger.error).toHaveBeenCalledWith("error", { error: "object" });
});
});

describe("resetLogger", () => {
test("Should reset logger to offLogger (default uninitialized state).", () => {
// Arrange - Set logger to console
setLogger(true);
expect(logger).toBe(globalThis.console);

// Act
resetLogger();

// Assert - Logger should be back to offLogger
expect(logger).not.toBe(globalThis.console);
expect(logger.debug).toBeDefined();
expect(logger.log).toBeDefined();
expect(logger.warn).toBeDefined();
expect(logger.error).toBeDefined();
});

test("Should reset logger from custom logger to offLogger.", () => {
// Arrange - Set logger to custom logger
const customLogger = {
debug: vi.fn(),
log: vi.fn(),
warn: vi.fn(),
error: vi.fn()
};
setLogger(customLogger);
expect(logger).toBe(customLogger);

// Act
resetLogger();

// Assert - Logger should be back to offLogger
expect(logger).not.toBe(customLogger);
expect(logger).not.toBe(globalThis.console);
expect(logger.debug).toBeDefined();
expect(logger.log).toBeDefined();
expect(logger.warn).toBeDefined();
expect(logger.error).toBeDefined();
});
});
});
11 changes: 9 additions & 2 deletions src/lib/core/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ const offLogger: ILogger = {
error: noop
};

export let logger: ILogger = stockLogger;
export let logger: ILogger = offLogger;

export function setLogger(newLogger: boolean | ILogger) {
logger = newLogger === true ? stockLogger : (newLogger === false ? offLogger : newLogger);
};
}

/**
* Resets the logger to the default uninitialized state (offLogger).
*/
export function resetLogger(): void {
logger = offLogger;
}
22 changes: 21 additions & 1 deletion src/lib/core/options.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "vitest";
import { routingOptions } from "./options.js";
import { resetRoutingOptions, routingOptions } from "./options.js";

describe("options", () => {
test("The routing options' initial values are the expected ones.", () => {
Expand Down Expand Up @@ -94,4 +94,24 @@ describe("options", () => {
// Restore original value
routingOptions.implicitMode = originalValue;
});

test("Should reset all options to defaults when resetRoutingOptions is called.", () => {
// Arrange - Modify all options to non-default values
routingOptions.full = true;
routingOptions.hashMode = 'multi';
routingOptions.implicitMode = 'hash';

// Verify they were changed
expect(routingOptions.full).toBe(true);
expect(routingOptions.hashMode).toBe('multi');
expect(routingOptions.implicitMode).toBe('hash');

// Act
resetRoutingOptions();

// Assert - Verify all options are back to defaults
expect(routingOptions.full).toBe(false);
expect(routingOptions.hashMode).toBe('single');
expect(routingOptions.implicitMode).toBe('path');
});
});
16 changes: 14 additions & 2 deletions src/lib/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,22 @@ export type RoutingOptions = {
}

/**
* Global routing options.
* Default routing options used for rollback.
*/
export const routingOptions: Required<RoutingOptions> = {
const defaultRoutingOptions: Required<RoutingOptions> = {
full: false,
hashMode: 'single',
implicitMode: 'path'
};

/**
* Global routing options.
*/
export const routingOptions: Required<RoutingOptions> = structuredClone(defaultRoutingOptions);

/**
* Resets routing options to their default values.
*/
export function resetRoutingOptions(): void {
Object.assign(routingOptions, structuredClone(defaultRoutingOptions));
}
16 changes: 14 additions & 2 deletions src/lib/core/trace.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,24 @@ export function getAllChildRouters(parent: RouterEngine) {
}

/**
* Tracing options that can be set during library initialization.
* Default tracing options used for rollback.
*/
export const traceOptions: TraceOptions = {
const defaultTraceOptions: TraceOptions = {
routerHierarchy: false,
};

/**
* Tracing options that can be set during library initialization.
*/
export const traceOptions: TraceOptions = structuredClone(defaultTraceOptions);

export function setTraceOptions(options?: typeof traceOptions) {
Object.assign(traceOptions, options);
}

/**
* Resets tracing options to their default values.
*/
export function resetTraceOptions(): void {
Object.assign(traceOptions, structuredClone(defaultTraceOptions));
}
28 changes: 27 additions & 1 deletion src/lib/core/trace.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterAll, beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
import { getAllChildRouters, setTraceOptions, traceOptions } from "./trace.svelte.js";
import { getAllChildRouters, resetTraceOptions, setTraceOptions, traceOptions } from "./trace.svelte.js";
import { RouterEngine } from "./RouterEngine.svelte.js";
import { init } from "$lib/index.js";

Expand Down Expand Up @@ -29,6 +29,32 @@ describe('setTraceOptions', () => {
});
});

describe('resetTraceOptions', () => {
test("Should reset trace options to defaults.", () => {
// Arrange - Set to non-default value
setTraceOptions({ routerHierarchy: true });
expect(traceOptions.routerHierarchy).toBe(true);

// Act
resetTraceOptions();

// Assert - Should be back to default
expect(traceOptions.routerHierarchy).toBe(false);
});

test("Should reset trace options when already at defaults.", () => {
// Arrange - Ensure it's already at default
resetTraceOptions();
expect(traceOptions.routerHierarchy).toBe(false);

// Act
resetTraceOptions();

// Assert - Should still be at default
expect(traceOptions.routerHierarchy).toBe(false);
});
});

describe('registerRouter', () => {
let cleanup: () => void;
beforeAll(() => {
Expand Down
Loading