/
Link-test.js
187 lines (154 loc) · 4.6 KB
/
Link-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import React from "react";
import ReactDOM from "react-dom";
import { MemoryRouter, HashRouter, Link } from "react-router-dom";
import renderStrict from "./utils/renderStrict";
describe("A <Link>", () => {
const node = document.createElement("div");
afterEach(() => {
ReactDOM.unmountComponentAtNode(node);
});
describe("with no <Router>", () => {
it("throws an error", () => {
jest.spyOn(console, "error").mockImplementation(() => {});
expect(() => {
renderStrict(<Link to="/">link</Link>, node);
}).toThrow(/You should not use <Link> outside a <Router>/);
expect(console.error).toHaveBeenCalledTimes(2);
});
});
describe("with no `to` prop", () => {
it("logs an error to the console", () => {
jest.spyOn(console, "error").mockImplementation(() => {});
renderStrict(
<MemoryRouter>
<Link>link</Link>
</MemoryRouter>,
node
);
expect(console.error).toHaveBeenCalledWith(
expect.stringContaining("The prop `to` is marked as required in `Link`")
);
});
});
it("accepts a string `to` prop", () => {
const to = "/the/path?the=query#the-hash";
renderStrict(
<MemoryRouter>
<Link to={to}>link</Link>
</MemoryRouter>,
node
);
const a = node.querySelector("a");
expect(a.getAttribute("href")).toEqual("/the/path?the=query#the-hash");
});
it("accepts an object `to` prop", () => {
const to = {
pathname: "/the/path",
search: "the=query",
hash: "#the-hash"
};
renderStrict(
<MemoryRouter>
<Link to={to}>link</Link>
</MemoryRouter>,
node
);
const a = node.querySelector("a");
expect(a.getAttribute("href")).toEqual("/the/path?the=query#the-hash");
});
describe("with no pathname", () => {
it("resolves using the current location", () => {
renderStrict(
<MemoryRouter initialEntries={["/somewhere"]}>
<Link to="?rendersWithPathname=true">link</Link>
</MemoryRouter>,
node
);
const a = node.querySelector("a");
expect(a.getAttribute("href")).toEqual(
"/somewhere?rendersWithPathname=true"
);
});
});
it("exposes its ref via an innerRef callback prop", done => {
function refCallback(n) {
if (n) {
expect(n.tagName).toEqual("A");
done();
}
}
renderStrict(
<MemoryRouter>
<Link to="/" innerRef={refCallback}>
link
</Link>
</MemoryRouter>,
node
);
});
it("exposes its ref via an innerRef RefObject prop", done => {
const refObject = {
get current() {
return null;
},
set current(n) {
if (n) {
expect(n.tagName).toEqual("A");
done();
}
}
};
renderStrict(
<MemoryRouter>
<Link to="/" innerRef={refObject}>
link
</Link>
</MemoryRouter>,
node
);
});
describe("with a <HashRouter>", () => {
afterEach(() => {
window.history.replaceState(null, "", "#");
});
function createLinkNode(hashType, to) {
renderStrict(
<HashRouter hashType={hashType}>
<Link to={to} />
</HashRouter>,
node
);
return node.querySelector("a");
}
describe('with the "slash" hashType', () => {
it("has the correct href", () => {
const linkNode = createLinkNode("slash", "/foo");
expect(linkNode.getAttribute("href")).toEqual("#/foo");
});
it("has the correct href with a leading slash if it is missing", () => {
const linkNode = createLinkNode("slash", "foo");
expect(linkNode.getAttribute("href")).toEqual("#/foo");
});
});
describe('with the "hashbang" hashType', () => {
it("has the correct href", () => {
const linkNode = createLinkNode("hashbang", "/foo");
expect(linkNode.getAttribute("href")).toEqual("#!/foo");
});
it("has the correct href with a leading slash if it is missing", () => {
const linkNode = createLinkNode("hashbang", "foo");
expect(linkNode.getAttribute("href")).toEqual("#!/foo");
});
});
describe('with the "noslash" hashType', () => {
it("has the correct href", () => {
const linkNode = createLinkNode("noslash", "foo");
expect(linkNode.getAttribute("href")).toEqual("#foo");
});
it("has the correct href and removes the leading slash", () => {
const linkNode = createLinkNode("noslash", "/foo");
expect(linkNode.getAttribute("href")).toEqual("#foo");
});
});
});
});