Skip to content

Commit 941993e

Browse files
committed
Spy on memoryRouter instead of mocking
1 parent cc7b01b commit 941993e

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

packages/react-router-dom/modules/__tests__/Link-click-test.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ import renderStrict from "./utils/renderStrict.js";
99
describe("<Link> click events", () => {
1010
const node = document.createElement("div");
1111

12-
afterEach(() => {
13-
ReactDOM.unmountComponentAtNode(node);
12+
let memoryHistory, pushSpy, replaceSpy;
13+
14+
beforeEach(() => {
15+
memoryHistory = createMemoryHistory();
16+
pushSpy = jest.spyOn(memoryHistory, "push");
17+
replaceSpy = jest.spyOn(memoryHistory, "push");
1418
});
1519

16-
const memoryHistory = createMemoryHistory();
17-
memoryHistory.push = jest.fn();
20+
afterEach(() => {
21+
ReactDOM.unmountComponentAtNode(node);
1822

19-
beforeEach(() => {
20-
memoryHistory.push.mockReset();
23+
pushSpy.mockRestore();
24+
replaceSpy.mockRestore();
2125
});
2226

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

4246
expect(clickHandler).toBeCalledTimes(1);
43-
expect(memoryHistory.push).toBeCalledTimes(1);
44-
expect(memoryHistory.push).toBeCalledWith(to);
47+
expect(pushSpy).toBeCalledTimes(1);
48+
expect(pushSpy).toBeCalledWith(to);
4549
});
4650

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

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

71-
7275
expect(clickHandler).toBeCalledTimes(2);
73-
expect(memoryHistory.push).toBeCalledTimes(1);
74-
expect(memoryHistory.push).toBeCalledWith(to);
75-
expect(memoryHistory.replace).toBeCalledTimes(1);
76-
expect(memoryHistory.replace).toBeCalledWith(to);
76+
expect(pushSpy).toBeCalledTimes(1);
77+
expect(pushSpy).toBeCalledWith(to);
78+
expect(replaceSpy).toBeCalledTimes(1);
79+
expect(replaceSpy).toBeCalledWith(to);
7780
});
7881

7982
it("calls onClick eventhandler and history.push with function `to` prop", () => {
80-
const memoryHistoryFoo = createMemoryHistory({
81-
initialEntries: ["/foo"]
82-
});
83-
memoryHistoryFoo.push = jest.fn();
83+
// Make push a no-op so key IDs do not change
84+
pushSpy.mockImplementation();
85+
8486
const clickHandler = jest.fn();
8587
let to = null;
8688
const toFn = location => {
@@ -93,7 +95,7 @@ describe("<Link> click events", () => {
9395
};
9496

9597
renderStrict(
96-
<Router history={memoryHistoryFoo}>
98+
<Router history={memoryHistory}>
9799
<Link to={toFn} onClick={clickHandler}>
98100
link
99101
</Link>
@@ -108,8 +110,8 @@ describe("<Link> click events", () => {
108110
});
109111

110112
expect(clickHandler).toBeCalledTimes(1);
111-
expect(memoryHistoryFoo.push).toBeCalledTimes(1);
112-
expect(memoryHistoryFoo.push).toBeCalledWith(to);
113+
expect(pushSpy).toBeCalledTimes(1);
114+
expect(pushSpy).toBeCalledWith(to);
113115
});
114116

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

131-
expect(memoryHistory.push).toBeCalledTimes(0);
133+
expect(pushSpy).toBeCalledTimes(0);
132134
});
133135

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

150-
expect(memoryHistory.push).toBeCalledTimes(0);
152+
expect(pushSpy).toBeCalledTimes(0);
151153
});
152154

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

171-
expect(memoryHistory.push).toBeCalledTimes(0);
173+
expect(pushSpy).toBeCalledTimes(0);
172174
});
173175

174176
it("prevents the default event handler if an error occurs", () => {
175-
const memoryHistory = createMemoryHistory();
176-
memoryHistory.push = jest.fn();
177177
const error = new Error();
178178
const clickHandler = () => {
179179
throw error;
@@ -205,6 +205,6 @@ describe("<Link> click events", () => {
205205
console.error.mockRestore();
206206
expect(clickHandler).toThrow(error);
207207
expect(mockPreventDefault).toHaveBeenCalled();
208-
expect(memoryHistory.push).toBeCalledTimes(0);
208+
expect(pushSpy).toBeCalledTimes(0);
209209
});
210210
});

0 commit comments

Comments
 (0)