git.fiddlerwoaroof.com
src/history-events.test.js
84348047
 import addMissingHistoryEvents from "./history-events";
30db14b1
 
 it("should overwrite pushstate and replacestate with event-emitting functions", () => {
   // given
   const pushState = jest.fn();
   const replaceState = jest.fn();
   const window = {
     dispatchEvent: jest.fn(),
     history: {
       pushState,
       replaceState
     }
   };
 
   // when
   addMissingHistoryEvents(window, window.history);
84348047
   window.history.pushState({ item: "push" }, "pushstate", "/pushstate");
   window.history.replaceState(
     { item: "replace" },
     "replacestate",
     "/replacestate"
   );
30db14b1
 
   //then
84348047
   expect(pushState.mock.calls).toEqual([
     [{ item: "push" }, "pushstate", "/pushstate"]
   ]);
   expect(replaceState.mock.calls).toEqual([
     [{ item: "replace" }, "replacestate", "/replacestate"]
   ]);
30db14b1
   expect(window.dispatchEvent.mock.calls.length).toEqual(2);
   const windowCalls = window.dispatchEvent.mock.calls;
 
84348047
   expect(windowCalls[0][0].detail).toEqual({
     state: { item: "push" },
     title: "pushstate",
     url: "/pushstate"
   });
   expect(windowCalls[1][0].detail).toEqual({
     state: { item: "replace" },
     title: "replacestate",
     url: "/replacestate"
   });
30db14b1
 });
 
 it("should only add history-events once if called any number of times on same objects", () => {
   // given
   const pushState = jest.fn();
   const replaceState = jest.fn();
   const window = {
     dispatchEvent: jest.fn(),
     history: {
       pushState,
       replaceState
     }
   };
 
   // when
   addMissingHistoryEvents(window, window.history);
   addMissingHistoryEvents(window, window.history);
   addMissingHistoryEvents(window, window.history);
   addMissingHistoryEvents(window, window.history);
 
84348047
   window.history.pushState({ item: "push" }, "pushstate", "/pushstate");
   window.history.replaceState(
     { item: "replace" },
     "replacestate",
     "/replacestate"
   );
30db14b1
 
   //then
   expect(window.dispatchEvent.mock.calls.length).toEqual(2);
84348047
 });