-
Notifications
You must be signed in to change notification settings - Fork 8
/
History.js
188 lines (177 loc) · 5.61 KB
/
History.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
import test from 'ava';
import createMemoryHistory from 'history/lib/createMemoryHistory';
import React from 'react';
import { render } from 'react-dom';
import sinon from 'sinon';
import 'babel-core/register';
import History from '../../src/components/History';
test('History calls onChange for new history push', t => {
const node = document.createElement('div');
document.body.appendChild(node);
const history = createMemoryHistory();
const onChangeSpy = sinon.spy();
render(
<History history={history} onChange={onChangeSpy} url="/"/>,
node
);
t.false(onChangeSpy.called);
history.push('/hello');
t.true(onChangeSpy.called);
t.same(onChangeSpy.lastCall.args, ['/hello', null]);
});
test('History calls onChange with state for new history push', t => {
const node = document.createElement('div');
document.body.appendChild(node);
const history = createMemoryHistory();
const onChangeSpy = sinon.spy();
render(
<History history={history} onChange={onChangeSpy} url="/"/>,
node
);
t.false(onChangeSpy.called);
history.push({pathname: '/hello', state: {name: 'Jane'}});
t.true(onChangeSpy.called);
t.same(onChangeSpy.lastCall.args, ['/hello', {name: 'Jane'}]);
});
test('History changes when new url is passed to history', t => {
const node = document.createElement('div');
document.body.appendChild(node);
const history = createMemoryHistory();
const onChangeSpy = sinon.spy();
const onLocationChangeSpy = sinon.spy();
render(
<History history={history} onChange={onChangeSpy} url="/"/>,
node
);
history.listen(onLocationChangeSpy);
render(
<History history={history} onChange={onChangeSpy} url="/hello"/>,
node
);
t.false(onChangeSpy.called);
t.true(onLocationChangeSpy.called);
t.is(onLocationChangeSpy.lastCall.args[0].pathname, '/hello');
});
test('History changes when new url and state is passed to history', t => {
const node = document.createElement('div');
document.body.appendChild(node);
const history = createMemoryHistory();
const onChangeSpy = sinon.spy();
const onLocationChangeSpy = sinon.spy();
render(
<History history={history} onChange={onChangeSpy} url="/"/>,
node
);
history.listen(onLocationChangeSpy);
render(
<History history={history} onChange={onChangeSpy} url="/hello" state={{name: 'Jane'}}/>,
node
);
t.false(onChangeSpy.called);
t.true(onLocationChangeSpy.called);
t.is(onLocationChangeSpy.lastCall.args[0].pathname, '/hello');
t.same(onLocationChangeSpy.lastCall.args[0].state, {name: 'Jane'});
});
test('History sends current lcoation in callback', t => {
const node = document.createElement('div');
document.body.appendChild(node);
const history = createMemoryHistory();
const onChangeSpy = sinon.spy();
render(
<History history={history} onChange={onChangeSpy} url={null}/>,
node
);
t.true(onChangeSpy.called);
t.same(onChangeSpy.lastCall.args, ['/', null]);
});
test('History changes when link is clicked', t => {
const node = document.createElement('div');
document.body.appendChild(node);
const history = createMemoryHistory();
const onChangeSpy = sinon.spy();
render(
<div>
<History history={history} onChange={onChangeSpy} url="/"/>
<a id="hello" href="/hello">Hello</a>
</div>,
node
);
t.false(onChangeSpy.called);
let mouseEvent = new document.defaultView.MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
document.getElementById('hello').dispatchEvent(mouseEvent);
t.true(onChangeSpy.called);
t.same(onChangeSpy.lastCall.args, ['https://example.com/hello']);
});
test('History has no change for right and meta clicks', t => {
const node = document.createElement('div');
document.body.appendChild(node);
const history = createMemoryHistory();
const onChangeSpy = sinon.spy();
render(
<div>
<History history={history} onChange={onChangeSpy} url="/"/>
<a id="hello" href="/hello">Hello</a>
</div>,
node
);
let mouseEvent = new document.defaultView.MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
metaKey: true
});
document.getElementById('hello').dispatchEvent(mouseEvent);
mouseEvent = new document.defaultView.MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
button: 2
});
document.getElementById('hello').dispatchEvent(mouseEvent);
t.false(onChangeSpy.called);
});
test('History pops state', t => {
const node = document.createElement('div');
document.body.appendChild(node);
const history = createMemoryHistory();
const onChangeSpy = sinon.spy();
render(
<History history={history} onChange={onChangeSpy} url="/"/>,
node
);
history.push({pathname: '/hello', state: {x: 1}});
render(
<History history={history} onChange={onChangeSpy} url="/hello" state={{x: 1}}/>,
node
);
history.push({pathname: '/bye', state: {x: 2}});
render(
<History history={history} onChange={onChangeSpy} url="/bye" state={{x: 2}}/>,
node
);
history.goBack();
t.same(onChangeSpy.lastCall.args, ['/hello', {x: 1}]);
});
test('allow cancelling a change', t => {
const node = document.createElement('div');
document.body.appendChild(node);
const history = createMemoryHistory();
const onChangeSpy = sinon.spy();
const onListenSpy = sinon.spy();
history.listen(onListenSpy);
render(
<History history={history} onChange={onChangeSpy} url="/"/>,
node
);
history.push({pathname: '/hello'});
render(
<History history={history} onChange={onChangeSpy} url="/"/>,
node
);
t.is(onListenSpy.callCount, 3);
t.is(onListenSpy.lastCall.args[0].pathname, '/');
});