forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLink-test.js
155 lines (133 loc) · 4.38 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
import expect from 'expect'
import React from 'react'
import ReactDOM from 'react-dom'
import { Simulate } from 'react-addons-test-utils'
import MemoryRouter from 'react-router/MemoryRouter'
import HashRouter from '../HashRouter'
import Link from '../Link'
import Route from 'react-router/Route'
import Switch from 'react-router/Switch'
describe('A <Link>', () => {
it('accepts a location "to" prop', () => {
const location = {
pathname: '/the/path',
search: 'the=query',
hash: '#the-hash'
}
const node = document.createElement('div')
ReactDOM.render((
<MemoryRouter>
<Link to={location}>link</Link>
</MemoryRouter>
), node)
const href = node.querySelector('a').getAttribute('href')
expect(href).toEqual('/the/path?the=query#the-hash')
})
})
describe('When a <Link> is clicked', () => {
it('calls its onClick handler')
it('changes the location')
describe('and the onClick handler calls event.preventDefault()', () => {
it('does not change the location')
})
})
describe('A <Link> underneath a <HashRouter>', () => {
const node = document.createElement('div')
afterEach(() => {
ReactDOM.unmountComponentAtNode(node)
})
const createLinkNode = (hashType, to) => {
ReactDOM.render((
<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')
})
})
})
describe('A relative <Link>', () => {
const node = document.createElement('div')
afterEach(() => {
ReactDOM.unmountComponentAtNode(node)
})
it('resolves using the parent match', () => {
const initialEntries = ['/', '/recipes']
ReactDOM.render((
<MemoryRouter initialEntries={initialEntries} initialIndex={1}>
<Route path='/recipes' render={() => (
<Link to='tacos'>Chess</Link>
)} />
</MemoryRouter>
), node)
const a = node.getElementsByTagName('a')[0]
expect(a.pathname).toBe('/recipes/tacos')
})
it('works when not in a route', () => {
const initialEntries = ['/']
ReactDOM.render((
<MemoryRouter initialEntries={initialEntries} initialIndex={0}>
<Link to='recipes'>Recipes</Link>
</MemoryRouter>
), node)
const a = node.getElementsByTagName('a')[0]
expect(a.pathname).toBe('/recipes')
})
it('navigates correctly', () => {
const initialEntries = ['/', '/recipes']
const RESTAURANTS = 'RESTAURANTS'
ReactDOM.render((
<MemoryRouter initialEntries={initialEntries} initialIndex={1}>
<Switch>
<Route path='/recipes' render={() => (
<Link to='../restaurants'>Order Takeout</Link>
)} />
<Route path='/restaurants' render={() => (
<div>{RESTAURANTS}</div>
)} />
</Switch>
</MemoryRouter>
), node)
expect(node.textContent).toNotContain(RESTAURANTS)
const a = node.getElementsByTagName('a')[0]
Simulate.click(a, {
defaultPrevented: false,
preventDefault() { this.defaultPrevented = true },
metaKey: null,
altKey: null,
ctrlKey: null,
shiftKey: null,
button: 0
})
expect(node.textContent).toContain(RESTAURANTS)
})
})