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