Skip to content

Commit 9016456

Browse files
StringEpsilontimdorr
authored andcommitted
Wrote event handling tests for Link. (remix-run#6663)
1 parent 536e184 commit 9016456

1 file changed

Lines changed: 95 additions & 2 deletions

File tree

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

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3-
import { MemoryRouter, HashRouter, Link } from "react-router-dom";
4-
3+
import { MemoryRouter, Router, HashRouter, Link } from "react-router-dom";
4+
import { createMemoryHistory } from "history";
55
import renderStrict from "./utils/renderStrict";
6+
import ReactTestUtils from "react-dom/test-utils";
67

78
describe("A <Link>", () => {
89
const node = document.createElement("div");
@@ -184,4 +185,96 @@ describe("A <Link>", () => {
184185
});
185186
});
186187
});
188+
189+
describe("on click events", () => {
190+
const memoryHistory = createMemoryHistory();
191+
memoryHistory.push = jest.fn();
192+
193+
beforeEach(() => {
194+
memoryHistory.push.mockReset();
195+
});
196+
197+
it("calls onClick eventhandler and history.push", () => {
198+
const clickHandler = jest.fn();
199+
const to = "/the/path?the=query#the-hash";
200+
201+
renderStrict(
202+
<Router history={memoryHistory}>
203+
<Link to={to} onClick={clickHandler}>
204+
link
205+
</Link>
206+
</Router>,
207+
node
208+
);
209+
210+
const a = node.querySelector("a");
211+
ReactTestUtils.Simulate.click(a, {
212+
defaultPrevented: false,
213+
button: 0
214+
});
215+
216+
expect(clickHandler).toBeCalledTimes(1);
217+
expect(memoryHistory.push).toBeCalledTimes(1);
218+
expect(memoryHistory.push).toBeCalledWith(to);
219+
});
220+
221+
it("does not call history.push on right click", () => {
222+
const to = "/the/path?the=query#the-hash";
223+
224+
renderStrict(
225+
<Router history={memoryHistory}>
226+
<Link to={to}>link</Link>
227+
</Router>,
228+
node
229+
);
230+
231+
const a = node.querySelector("a");
232+
ReactTestUtils.Simulate.click(a, {
233+
defaultPrevented: false,
234+
button: 1
235+
});
236+
237+
expect(memoryHistory.push).toBeCalledTimes(0);
238+
});
239+
240+
it("does not call history.push on prevented event.", () => {
241+
const to = "/the/path?the=query#the-hash";
242+
243+
renderStrict(
244+
<Router history={memoryHistory}>
245+
<Link to={to}>link</Link>
246+
</Router>,
247+
node
248+
);
249+
250+
const a = node.querySelector("a");
251+
ReactTestUtils.Simulate.click(a, {
252+
defaultPrevented: true,
253+
button: 0
254+
});
255+
256+
expect(memoryHistory.push).toBeCalledTimes(0);
257+
});
258+
259+
it("does not call history.push target not specifying 'self'", () => {
260+
const to = "/the/path?the=query#the-hash";
261+
262+
renderStrict(
263+
<Router history={memoryHistory}>
264+
<Link to={to} target="_blank">
265+
link
266+
</Link>
267+
</Router>,
268+
node
269+
);
270+
271+
const a = node.querySelector("a");
272+
ReactTestUtils.Simulate.click(a, {
273+
defaultPrevented: false,
274+
button: 0
275+
});
276+
277+
expect(memoryHistory.push).toBeCalledTimes(0);
278+
});
279+
});
187280
});

0 commit comments

Comments
 (0)