Skip to content

Commit

Permalink
Spy on memoryRouter instead of mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
guidobouman committed Jun 9, 2021
1 parent cc7b01b commit 941993e
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions packages/react-router-dom/modules/__tests__/Link-click-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import renderStrict from "./utils/renderStrict.js";
describe("<Link> click events", () => {
const node = document.createElement("div");

afterEach(() => {
ReactDOM.unmountComponentAtNode(node);
let memoryHistory, pushSpy, replaceSpy;

beforeEach(() => {
memoryHistory = createMemoryHistory();
pushSpy = jest.spyOn(memoryHistory, "push");
replaceSpy = jest.spyOn(memoryHistory, "push");
});

const memoryHistory = createMemoryHistory();
memoryHistory.push = jest.fn();
afterEach(() => {
ReactDOM.unmountComponentAtNode(node);

beforeEach(() => {
memoryHistory.push.mockReset();
pushSpy.mockRestore();
replaceSpy.mockRestore();
});

it("calls onClick eventhandler and history.push", () => {
Expand All @@ -40,13 +44,13 @@ describe("<Link> click events", () => {
});

expect(clickHandler).toBeCalledTimes(1);
expect(memoryHistory.push).toBeCalledTimes(1);
expect(memoryHistory.push).toBeCalledWith(to);
expect(pushSpy).toBeCalledTimes(1);
expect(pushSpy).toBeCalledWith(to);
});

it("calls history.replace on duplicate navigation", () => {
const clickHandler = jest.fn();
const to = "/the/path?the=query#the-hash";
const to = "/duplicate/path?the=query#the-hash";

renderStrict(
<Router history={memoryHistory}>
Expand All @@ -68,19 +72,17 @@ describe("<Link> click events", () => {
button: 0
});


expect(clickHandler).toBeCalledTimes(2);
expect(memoryHistory.push).toBeCalledTimes(1);
expect(memoryHistory.push).toBeCalledWith(to);
expect(memoryHistory.replace).toBeCalledTimes(1);
expect(memoryHistory.replace).toBeCalledWith(to);
expect(pushSpy).toBeCalledTimes(1);
expect(pushSpy).toBeCalledWith(to);
expect(replaceSpy).toBeCalledTimes(1);
expect(replaceSpy).toBeCalledWith(to);
});

it("calls onClick eventhandler and history.push with function `to` prop", () => {
const memoryHistoryFoo = createMemoryHistory({
initialEntries: ["/foo"]
});
memoryHistoryFoo.push = jest.fn();
// Make push a no-op so key IDs do not change
pushSpy.mockImplementation();

const clickHandler = jest.fn();
let to = null;
const toFn = location => {
Expand All @@ -93,7 +95,7 @@ describe("<Link> click events", () => {
};

renderStrict(
<Router history={memoryHistoryFoo}>
<Router history={memoryHistory}>
<Link to={toFn} onClick={clickHandler}>
link
</Link>
Expand All @@ -108,8 +110,8 @@ describe("<Link> click events", () => {
});

expect(clickHandler).toBeCalledTimes(1);
expect(memoryHistoryFoo.push).toBeCalledTimes(1);
expect(memoryHistoryFoo.push).toBeCalledWith(to);
expect(pushSpy).toBeCalledTimes(1);
expect(pushSpy).toBeCalledWith(to);
});

it("does not call history.push on right click", () => {
Expand All @@ -128,7 +130,7 @@ describe("<Link> click events", () => {
button: 1
});

expect(memoryHistory.push).toBeCalledTimes(0);
expect(pushSpy).toBeCalledTimes(0);
});

it("does not call history.push on prevented event.", () => {
Expand All @@ -147,7 +149,7 @@ describe("<Link> click events", () => {
button: 0
});

expect(memoryHistory.push).toBeCalledTimes(0);
expect(pushSpy).toBeCalledTimes(0);
});

it("does not call history.push target not specifying 'self'", () => {
Expand All @@ -168,12 +170,10 @@ describe("<Link> click events", () => {
button: 0
});

expect(memoryHistory.push).toBeCalledTimes(0);
expect(pushSpy).toBeCalledTimes(0);
});

it("prevents the default event handler if an error occurs", () => {
const memoryHistory = createMemoryHistory();
memoryHistory.push = jest.fn();
const error = new Error();
const clickHandler = () => {
throw error;
Expand Down Expand Up @@ -205,6 +205,6 @@ describe("<Link> click events", () => {
console.error.mockRestore();
expect(clickHandler).toThrow(error);
expect(mockPreventDefault).toHaveBeenCalled();
expect(memoryHistory.push).toBeCalledTimes(0);
expect(pushSpy).toBeCalledTimes(0);
});
});

0 comments on commit 941993e

Please sign in to comment.