-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathdom.js
270 lines (238 loc) · 7.12 KB
/
dom.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { Router, Link, route } from 'src';
import { Match, Link as ActiveLink } from 'src/match';
import { h, render } from 'preact';
const Empty = () => null;
function fireEvent(on, type) {
let e = document.createEvent('Event');
e.initEvent(type, true, true);
on.dispatchEvent(e);
}
describe('dom', () => {
let scratch, $, mount;
before( () => {
scratch = document.createElement('div');
document.body.appendChild(scratch);
$ = s => scratch.querySelector(s);
mount = jsx => render(jsx, scratch, scratch.firstChild);
});
beforeEach( () => {
// manually reset the URL before every test
history.replaceState(null, null, '/');
fireEvent(window, 'popstate');
});
afterEach( () => {
mount(<Empty />);
scratch.innerHTML = '';
});
after( () => {
document.body.removeChild(scratch);
});
describe('<Link />', () => {
it('should render a normal link', () => {
expect(
mount(<Link href="/foo" bar="baz">hello</Link>).outerHTML
).to.eql(
mount(<a href="/foo" bar="baz">hello</a>).outerHTML
);
});
it('should route when clicked', () => {
let onChange = sinon.spy();
mount(
<div>
<Link href="/foo">foo</Link>
<Router onChange={onChange}>
<div default />
</Router>
</div>
);
onChange.reset();
$('a').click();
expect(onChange)
.to.have.been.calledOnce
.and.to.have.been.calledWithMatch({ url:'/foo' });
});
});
describe('<a>', () => {
it('should route for existing routes', () => {
let onChange = sinon.spy();
mount(
<div>
<a href="/foo">foo</a>
<Router onChange={onChange}>
<div default />
</Router>
</div>
);
onChange.reset();
$('a').click();
// fireEvent($('a'), 'click');
expect(onChange)
.to.have.been.calledOnce
.and.to.have.been.calledWithMatch({ url:'/foo' });
});
it('should not intercept non-preact elements', () => {
let onChange = sinon.spy();
mount(
<div>
<div dangerouslySetInnerHTML={{ __html: `<a href="#foo">foo</a>` }} />
<Router onChange={onChange}>
<div default />
</Router>
</div>
);
onChange.reset();
$('a').click();
expect(onChange).not.to.have.been.called;
expect(location.href).to.contain('#foo');
});
});
describe('Router', () => {
it('should add and remove children', () => {
class A {
componentWillMount() {}
componentWillUnmount() {}
render(){ return <div />; }
}
sinon.spy(A.prototype, 'componentWillMount');
sinon.spy(A.prototype, 'componentWillUnmount');
mount(
<Router>
<A path="/foo" />
</Router>
);
expect(A.prototype.componentWillMount).not.to.have.been.called;
route('/foo');
expect(A.prototype.componentWillMount).to.have.been.calledOnce;
expect(A.prototype.componentWillUnmount).not.to.have.been.called;
route('/bar');
expect(A.prototype.componentWillMount).to.have.been.calledOnce;
expect(A.prototype.componentWillUnmount).to.have.been.calledOnce;
});
it('should support re-routing', done => {
class A {
componentWillMount() {
route('/b');
}
render(){ return <div class="a" />; }
}
class B {
componentWillMount(){}
render(){ return <div class="b" />; }
}
sinon.spy(A.prototype, 'componentWillMount');
sinon.spy(B.prototype, 'componentWillMount');
mount(
<Router>
<A path="/a" />
<B path="/b" />
</Router>
);
expect(A.prototype.componentWillMount).not.to.have.been.called;
route('/a');
expect(A.prototype.componentWillMount).to.have.been.calledOnce;
A.prototype.componentWillMount.reset();
expect(location.pathname).to.equal('/b');
setTimeout( () => {
expect(A.prototype.componentWillMount).not.to.have.been.called;
expect(B.prototype.componentWillMount).to.have.been.calledOnce;
expect(scratch).to.have.deep.property('firstElementChild.className', 'b');
done();
}, 10);
});
it('should not carry over the previous value of a query parameter', () => {
class A {
render({ bar }){ return <p>bar is {bar}</p>; }
}
let routerRef;
mount(
<Router ref={r => routerRef = r}>
<A path="/foo" />
</Router>
);
route('/foo');
expect(routerRef.base.outerHTML).to.eql('<p>bar is </p>');
route('/foo?bar=5');
expect(routerRef.base.outerHTML).to.eql('<p>bar is 5</p>');
route('/foo');
expect(routerRef.base.outerHTML).to.eql('<p>bar is </p>');
});
it('should support relative links', () => {
class A {
render() {
return <a href="two">go</a>;
}
}
history.replaceState(null, null, '/foo/one');
mount(
<Router>
<A default />
</Router>
);
scratch.querySelector('a').click();
expect(location.pathname).to.equal('/foo/two');
});
});
describe('preact-router/match', () => {
describe('<Match>', () => {
it('should invoke child function with match status when routing', done => {
let spy1 = sinon.spy(),
spy2 = sinon.spy();
mount(
<div>
<Router />
<Match path="/foo">{spy1}</Match>
<Match path="/bar">{spy2}</Match>
</div>
);
expect(spy1, 'spy1 /foo').to.have.been.calledOnce.and.calledWithMatch({ matches: false, path:'/', url:'/' });
expect(spy2, 'spy2 /foo').to.have.been.calledOnce.and.calledWithMatch({ matches: false, path:'/', url:'/' });
spy1.reset();
spy2.reset();
route('/foo');
setTimeout( () => {
expect(spy1, 'spy1 /foo').to.have.been.calledOnce.and.calledWithMatch({ matches: true, path:'/foo', url:'/foo' });
expect(spy2, 'spy2 /foo').to.have.been.calledOnce.and.calledWithMatch({ matches: false, path:'/foo', url:'/foo' });
spy1.reset();
spy2.reset();
route('/foo?bar=5');
setTimeout( () => {
expect(spy1, 'spy1 /foo?bar=5').to.have.been.calledOnce.and.calledWithMatch({ matches: true, path:'/foo', url:'/foo?bar=5' });
expect(spy2, 'spy2 /foo?bar=5').to.have.been.calledOnce.and.calledWithMatch({ matches: false, path:'/foo', url:'/foo?bar=5' });
spy1.reset();
spy2.reset();
route('/bar');
setTimeout( () => {
expect(spy1, 'spy1 /bar').to.have.been.calledOnce.and.calledWithMatch({ matches: false, path:'/bar', url:'/bar' });
expect(spy2, 'spy2 /bar').to.have.been.calledOnce.and.calledWithMatch({ matches: true, path:'/bar', url:'/bar' });
done();
}, 20);
}, 20);
}, 20);
});
});
describe('<Link>', () => {
it('should render with active class when active', done => {
mount(
<div>
<Router />
<ActiveLink activeClassName="active" path="/foo">foo</ActiveLink>
<ActiveLink activeClassName="active" class="bar" path="/bar">bar</ActiveLink>
</div>
);
route('/foo');
setTimeout( () => {
expect(scratch.innerHTML).to.eql('<div><a class="active">foo</a><a class="bar">bar</a></div>');
route('/foo?bar=5');
setTimeout( () => {
expect(scratch.innerHTML).to.eql('<div><a class="active">foo</a><a class="bar">bar</a></div>');
route('/bar');
setTimeout( () => {
expect(scratch.innerHTML).to.eql('<div><a class="">foo</a><a class="bar active">bar</a></div>');
done();
});
});
});
});
});
});
});