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
22 changes: 12 additions & 10 deletions src/lib/kernel/InterceptedHistoryApi.svelte.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ describe('InterceptedHistoryApi', () => {
const initialUrl = 'http://example.com/';
let historyApi: InterceptedHistoryApi;
let browserMocks: ReturnType<typeof setupBrowserMocks>;
let origPushState: typeof globalThis.window.history.pushState;
let origReplaceState: typeof globalThis.window.history.replaceState;

beforeEach(() => {
browserMocks = setupBrowserMocks(initialUrl);
origPushState = globalThis.window.history.pushState;
origReplaceState = globalThis.window.history.replaceState;
historyApi = new InterceptedHistoryApi();
});

afterEach(() => {
historyApi.dispose();
browserMocks.cleanup();
vi.resetAllMocks();
});

describe('constructor', () => {
Expand All @@ -24,9 +29,10 @@ describe('InterceptedHistoryApi', () => {
expect(historyApi.url.href).toBe(initialUrl);
});

test('Should replace window.history with itself.', () => {
test("Should replace window.history's pushState and replaceState methods.", () => {
// Assert.
expect(globalThis.window.history).toBe(historyApi);
expect(globalThis.window.history.pushState).not.toBe(origPushState);
expect(globalThis.window.history.replaceState).not.toBe(origReplaceState);
});

test('Should use provided initial URL.', () => {
Expand Down Expand Up @@ -315,14 +321,13 @@ describe('InterceptedHistoryApi', () => {

test('Should call the original history method when navigation is not cancelled.', () => {
// Arrange.
const originalPushState = vi.spyOn(browserMocks.history, 'pushState');
const state = { path: { test: 'value' }, hash: {} };

// Act.
historyApi.pushState(state, '', 'http://example.com/other');

// Assert.
expect(originalPushState).toHaveBeenCalledWith(state, '', 'http://example.com/other');
expect(origPushState).toHaveBeenCalledWith(state, '', 'http://example.com/other');
});
});

Expand Down Expand Up @@ -375,15 +380,13 @@ describe('InterceptedHistoryApi', () => {
expect(callback).not.toHaveBeenCalled();
});

test('Should restore original window.history.', () => {
// Arrange.
const originalHistory = browserMocks.history;

test('Should restore original window.history methods.', () => {
// Act.
historyApi.dispose();

// Assert.
expect(globalThis.window.history).toBe(originalHistory);
expect(globalThis.window.history.pushState).toBe(origPushState);
expect(globalThis.window.history.replaceState).toBe(origReplaceState);
});

test('Should be safe to call multiple times.', () => {
Expand Down Expand Up @@ -422,7 +425,6 @@ describe('InterceptedHistoryApi', () => {
});
historyApi.on('beforeNavigate', callback1);
historyApi2.on('beforeNavigate', callback2);
expect(globalThis.window.history).toBe(historyApi2);

// Act.
historyApi2.pushState({}, '', 'http://example.com/test');
Expand Down
16 changes: 10 additions & 6 deletions src/lib/kernel/InterceptedHistoryApi.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ export class InterceptedHistoryApi extends StockHistoryApi implements FullModeHi
navigationCancelled: {}
};
#nextSubId = 0;
#originalHistory: History | undefined;
#origReplaceState;
#origPushState;

constructor(initialUrl?: string, initialState?: State) {
super(initialUrl, initialState);
if (globalThis.window) {
this.#originalHistory = globalThis.window.history;
globalThis.window.history = this;
this.#origReplaceState = globalThis.window.history.replaceState;
this.#origPushState = globalThis.window.history.pushState;
globalThis.window.history.replaceState = this.replaceState.bind(this);
globalThis.window.history.pushState = this.pushState.bind(this);
}
}

Expand Down Expand Up @@ -82,7 +85,7 @@ export class InterceptedHistoryApi extends StockHistoryApi implements FullModeHi
);
event.state = this.state;
}
this.#originalHistory?.[`${method}State`](event.state, unused, url);
(method === 'push' ? this.#origPushState : this.#origReplaceState)?.bind(globalThis.window.history)(event.state, unused, url);
this.url.href = globalThis.window?.location?.href ?? new URL(url ?? '', this.url).href;
this.state = event.state as State;
}
Expand All @@ -108,8 +111,9 @@ export class InterceptedHistoryApi extends StockHistoryApi implements FullModeHi
beforeNavigate: {},
navigationCancelled: {}
};
if (this.#originalHistory) {
globalThis.window.history = this.#originalHistory;
if (globalThis.window) {
globalThis.window.history.replaceState = this.#origReplaceState!;
globalThis.window.history.pushState = this.#origPushState!;
}
super.dispose();
}
Expand Down