-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
255 lines (225 loc) · 6.92 KB
/
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import React from 'react'
import { MemoryRouter, Switch, Route } from 'react-router-dom'
import { useLocation } from 'react-router'
import { Passage, Link, Redirect } from '.'
import renderer from 'react-test-renderer'
jest.mock('react-router', () => {
const reactRouter = jest.requireActual('react-router')
return {
...reactRouter,
useLocation: jest.fn((location) => ({
pathname: '/',
})),
}
})
describe('react-passage', () => {
describe('when there is no context', () => {
const href = '/blades'
const component = renderer.create(<Link to={href}>Blades</Link>)
it('renders an anchor tag', () => {
expectAnchorWith(component, href)
})
it('matches the snapshot', () => {
expect(component.toJSON()).toMatchSnapshot()
})
})
describe('when a route is unmatched', () => {
const renderDummyRouteTree = (link) =>
renderer.create(
<Passage>
<MemoryRouter>
<Switch>
<Route path="/get-started" exact render={() => {}} />
<Route render={() => <Link to={link}>Dummy Link</Link>} />
</Switch>
</MemoryRouter>
</Passage>
)
it('renders an anchor tag, when path does not match', () => {
const href = '/blades'
expectAnchorWith(renderDummyRouteTree(href), href)
})
it('renders an anchor tag, when path the same, but origin is different', () => {
const href = 'https://www.google.com/get-started'
expectAnchorWith(renderDummyRouteTree(href), href)
})
it('matches the snapshot', () => {
expect(renderDummyRouteTree('/blades').toJSON()).toMatchSnapshot()
})
})
describe('when a route is matched', () => {
const hrefRelativeMatch = '/get-started/plan/shave'
const hrefFullyQualifiedMatch =
'https://uk.dollarshaveclub.com/get-started/plan/shave'
const MOCK_LOCATION = {
hash: '',
host: 'uk.dollarshaveclub.com',
hostname: 'uk.dollarshaveclub.com',
href: 'https://uk.dollarshaveclub.com/',
origin: 'https://uk.dollarshaveclub.com',
pathname: '/',
port: '',
protocol: 'https:',
}
let realLocation
beforeAll(() => {
realLocation = window.location
delete window.location
window.location = MOCK_LOCATION
})
afterAll(() => {
window.location = realLocation
})
const renderDummyRoutes = (link) =>
renderer.create(
<Passage>
<MemoryRouter>
<Switch>
<Route path="/get-started/plan/:planId" exact render={() => {}} />
<Route path="/get-started" exact render={() => {}} />
<Route path="/users/:id" exact render={() => {}} />
<Route render={() => link} />
</Switch>
</MemoryRouter>
</Passage>
)
it('renders a Link tag, when relative path matches', () => {
expectLinkWith(
renderDummyRoutes(<Link to={hrefRelativeMatch}>Shave Core</Link>),
{ pathname: hrefRelativeMatch, hash: '', search: '', state: null }
)
expectLinkWith(
renderDummyRoutes(<Link to={hrefRelativeMatch}>Shave Core</Link>),
{ pathname: hrefRelativeMatch, hash: '', search: '', state: null }
)
})
it('renders a Link tag, when fully qualified url matches', () => {
expectLinkWith(
renderDummyRoutes(<Link to={hrefFullyQualifiedMatch}>Shave Core</Link>),
{ pathname: hrefRelativeMatch, hash: '', search: '', state: null }
)
})
it('renders a Link tag with Query Parementers', () => {
const hrefWithQueryParams = '/get-started?source=foo&medium=nav'
const LinkComponent = <Link to={hrefWithQueryParams}>Shave Core</Link>
expectLinkWith(renderDummyRoutes(LinkComponent), {
pathname: '/get-started',
hash: '',
search: '?source=foo&medium=nav',
state: null,
})
expect(LinkComponent).toMatchSnapshot()
})
it('matches the snapshot', () => {
expect(
renderDummyRoutes(
<Link to={hrefRelativeMatch}>Shave Core</Link>
).toJSON()
).toMatchSnapshot()
})
it('matches with a location object', () => {
expect(
renderDummyRoutes(
<Link
to={{
pathname: hrefRelativeMatch,
}}
>
Shave Core
</Link>
).toJSON()
).toMatchSnapshot()
})
it('matches with a function that returns a location object', () => {
expect(
renderDummyRoutes(
<Link to={(location) => location}>Shave Core</Link>
).toJSON()
).toMatchSnapshot()
expect(useLocation).toHaveBeenCalled()
})
it('matches but is overridden with external flag', () => {
expectAnchorWith(
renderDummyRoutes(
<Link external to={hrefRelativeMatch}>
Shave Core
</Link>
),
hrefRelativeMatch
)
})
})
it('redirects to a route without context', () => {
const mockVia = jest.fn()
renderer.create(<Redirect to="/our-products/shave" via={mockVia} />)
expect(mockVia).toHaveBeenCalledWith('/our-products/shave')
})
it('redirects to a matched react route', () => {
const component = renderer.create(
<Passage>
<MemoryRouter>
<Switch>
<Route
path="/get-started"
exact
render={() => <p>Redirected!</p>}
/>
<Route render={() => <Redirect to="/get-started" />} />
</Switch>
</MemoryRouter>
</Passage>
)
expect(component.toJSON()).toMatchSnapshot()
})
it('redirects to an unmatched react route', () => {
const mockVia = jest.fn()
renderer.create(
<Passage>
<MemoryRouter>
<Switch>
<Route
path="/get-started"
exact
render={() => <p>Redirected!</p>}
/>
<Route
render={() => <Redirect to="/our-products" via={mockVia} />}
/>
</Switch>
</MemoryRouter>
</Passage>
)
expect(mockVia).toHaveBeenCalledWith('/our-products')
})
describe('Children types', () => {
it('can handle null children types', () => {
renderer.create(
<Passage>
{null}
<MemoryRouter>
<Switch>
<Route
path="/get-started"
exact
render={() => <p>Redirected!</p>}
/>
</Switch>
</MemoryRouter>
</Passage>
)
})
})
})
// ---Helpers---
function expectAnchorWith(componentTree, href) {
const foundAnchor = componentTree.root.findByProps({
'data-safelink-type': 'a',
})
expect(foundAnchor.props.href).toBe(href)
}
function expectLinkWith(componentTree, toLocationObject) {
const foundLink = componentTree.root.findByProps({
'data-safelink-type': 'link',
})
expect(foundLink.props.to).toEqual(toLocationObject)
}