-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathhistory.test.ts
58 lines (47 loc) · 1.75 KB
/
history.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { describe, expect, it, vi } from 'vitest';
import { WINDOW } from '../../src/types';
import { afterEach } from 'node:test';
import { instrumentHistory } from './../../src/instrument/history';
describe('instrumentHistory', () => {
const originalHistory = WINDOW.history;
WINDOW.addEventListener = vi.fn();
afterEach(() => {
// @ts-expect-error - this is fine for testing
WINDOW.history = originalHistory;
});
it("doesn't throw if history is not available", () => {
// @ts-expect-error - this is fine for testing
WINDOW.history = undefined;
expect(instrumentHistory).not.toThrow();
expect(WINDOW.history).toBe(undefined);
});
it('adds an event listener for popstate', () => {
// adds an event listener for popstate
expect(WINDOW.addEventListener).toHaveBeenCalledTimes(1);
expect(WINDOW.addEventListener).toHaveBeenCalledWith('popstate', expect.any(Function));
});
it("doesn't throw if history.pushState is not a function", () => {
// @ts-expect-error - only partially adding history properties
WINDOW.history = {
replaceState: () => {},
pushState: undefined,
};
expect(instrumentHistory).not.toThrow();
expect(WINDOW.history).toEqual({
replaceState: expect.any(Function), // patched function
pushState: undefined, // unpatched
});
});
it("doesn't throw if history.replaceState is not a function", () => {
// @ts-expect-error - only partially adding history properties
WINDOW.history = {
replaceState: undefined,
pushState: () => {},
};
expect(instrumentHistory).not.toThrow();
expect(WINDOW.history).toEqual({
replaceState: undefined, // unpatched
pushState: expect.any(Function), // patched function
});
});
});