-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathrouter.spec.ts
380 lines (329 loc) · 13.5 KB
/
router.spec.ts
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import { createComputed, createRoot, createSignal } from "solid-js";
import { createRouterContext } from "../src/routing.js";
import type { LocationChange } from "../src/types.js";
import { createAsyncRoot, createCounter, waitFor } from "./helpers.js";
const fakeBranches = () => [];
const fakeContext = () => ({});
describe("Router should", () => {
describe("have member `base` which should", () => {
test(`have a default path when base path is not defined`, () => {
createRoot(() => {
const signal = createSignal<LocationChange>({ value: "" });
const { base } = createRouterContext({ signal }, fakeBranches);
expect(base.path()).toBe("/");
});
});
test(`have a normalized version of the base path when defined`, () => {
createRoot(() => {
const signal = createSignal<LocationChange>({ value: "" });
const { base } = createRouterContext({ signal }, fakeBranches, fakeContext, {
base: "base"
});
expect(base.path()).toBe("/base");
});
});
test(`throw when the base path is invalid`, () => {
createRoot(() => {
const signal = createSignal<LocationChange>({ value: "" });
expect(() =>
createRouterContext({ signal }, fakeBranches, fakeContext, { base: "http://example.com" })
).toThrow();
});
});
});
describe("have member `location` which should", () => {
test(`be initialized by the integration signal`, () => {
createRoot(() => {
const signal = createSignal<LocationChange>({
value: "/foo/bar?hello=world"
});
const { location } = createRouterContext({ signal }, fakeBranches);
expect(location.pathname).toBe("/foo/bar");
expect(location.search).toBe("?hello=world");
});
});
describe(`contain property 'pathname' which should`, () => {
test(`be reactive to the path part of the integration signal`, () =>
createAsyncRoot(resolve => {
const expected = "/fizz/buzz";
const signal = createSignal<LocationChange>({
value: "/foo/bar?hello=world"
});
const { location } = createRouterContext({ signal }, fakeBranches);
expect(location.pathname).toBe("/foo/bar");
signal[1]({ value: expected + "?hello=world" });
waitFor(() => signal[0]().value === expected + "?hello=world").then(() => {
expect(location.pathname).toBe(expected);
resolve();
});
}));
test(`ignore the queryString part of the integration signal`, () =>
createRoot(() => {
const signal = createSignal<LocationChange>({
value: "/foo/bar?hello=world"
});
const { location } = createRouterContext({ signal }, fakeBranches);
const count = createCounter(() => location.pathname);
expect(location.pathname).toBe("/foo/bar");
signal[1]({ value: "/foo/bar?fizz=buzz" });
expect(location.pathname).toBe("/foo/bar");
expect(count()).toBe(0);
}));
test(`handle URL decoding`, () =>
createRoot(() => {
const signal = createSignal<LocationChange>({
value: "/foo bar+baz"
});
const { location } = createRouterContext({ signal }, fakeBranches);
expect(location.pathname).toBe("/foo%20bar+baz");
}));
}); // end of "contain property 'pathname'"
describe(`contain property 'search' which should`, () => {
test(`be reactive to the search part of the integration signal`, () =>
createAsyncRoot(resolve => {
const expected = "?fizz=buzz";
const signal = createSignal<LocationChange>({
value: "/foo/bar?hello=world"
});
const { location } = createRouterContext({ signal }, fakeBranches);
expect(location.search).toBe("?hello=world");
signal[1]({ value: "/foo/baz" + expected });
waitFor(() => signal[0]().value === "/foo/baz" + expected).then(() => {
expect(location.search).toBe(expected);
resolve();
});
}));
test(`ignore the path part of the integration signal`, () =>
createRoot(() => {
const signal = createSignal<LocationChange>({
value: "/foo/bar?hello=world"
});
const { location } = createRouterContext({ signal }, fakeBranches);
const count = createCounter(() => location.search);
expect(location.search).toBe("?hello=world");
signal[1]({ value: "/fizz/buzz?hello=world" });
expect(location.search).toBe("?hello=world");
expect(count()).toBe(0);
}));
test(`handle URL decoding`, () =>
createRoot(() => {
const signal = createSignal<LocationChange>({
value: "/foo?hello+world=bar+baz"
});
const { location } = createRouterContext({ signal }, fakeBranches);
expect(location.search).toBe("?hello+world=bar+baz");
}));
}); //end of "contain property 'search'"
describe(`contain property 'hash' which should`, () => {
test(`handle URL decoding`, () =>
createRoot(() => {
const signal = createSignal<LocationChange>({
value: "/foo#bar baz"
});
const { location } = createRouterContext({ signal }, fakeBranches);
expect(location.hash).toBe("#bar%20baz");
}));
}); // end of "contain property 'hash'"
describe("have member `query` which should", () => {
test(`be parsed from location.search`, () => {
createRoot(() => {
const signal = createSignal<LocationChange>({
value: "/foo/bar?hello=world&fizz=buzz"
});
const { location } = createRouterContext({ signal }, fakeBranches);
expect(location.query.hello).toEqual("world");
expect(location.query.fizz).toEqual("buzz");
});
});
test(`be reactive to location.search`, () =>
createAsyncRoot(resolve => {
const signal = createSignal<LocationChange>({
value: "/foo/bar?hello=world"
});
const { location } = createRouterContext({ signal }, fakeBranches);
expect(location.query.hello).toEqual("world");
signal[1]({ value: "/foo/bar?hello=world&fizz=buzz" });
waitFor(() => signal[0]().value === "/foo/bar?hello=world&fizz=buzz").then(() => {
expect(location.query.fizz).toEqual("buzz");
resolve();
});
}));
test(`have fine-grain reactivity`, () =>
createAsyncRoot(resolve => {
const signal = createSignal<LocationChange>({
value: "/foo/bar?hello=world"
});
const { location } = createRouterContext({ signal }, fakeBranches);
const count = createCounter(() => location.query.hello);
expect(location.query.hello).toEqual("world");
signal[1]({ value: "/foo/bar?hello=world&fizz=buzz" });
waitFor(() => signal[0]().value === "/foo/bar?hello=world&fizz=buzz").then(() => {
expect(location.query.fizz).toEqual("buzz");
expect(count()).toBe(0);
resolve();
});
}));
test(`have properties which are reactive`, () =>
createAsyncRoot(resolve => {
const signal = createSignal<LocationChange>({
value: "/foo/bar?hello=world"
});
const { location } = createRouterContext({ signal }, fakeBranches);
const count = createCounter(() => location.query.hello);
expect(location.query.hello).toEqual("world");
signal[1]({ value: "/foo/bar?hello=foo" });
waitFor(() => signal[0]().value === "/foo/bar?hello=foo").then(() => {
expect(location.search).toEqual("?hello=foo");
expect(location.query.hello).toEqual("foo");
expect(count()).toBe(1);
resolve();
});
}));
}); // end of "have member `query`"
}); // end "have member `location`"
describe("have member `navigate` which should", () => {
test(`update the location in the next microtask`, () => {
createAsyncRoot(resolve => {
const signal = createSignal<LocationChange>({
value: "/"
});
const { location, navigatorFactory } = createRouterContext({ signal }, fakeBranches);
const navigate = navigatorFactory();
expect(location.pathname).toBe("/");
navigate("/foo/1");
setTimeout(() => {
expect(location.pathname).toBe("/foo/1");
resolve();
});
});
});
test(`do nothing if the new path is the same`, () =>
createAsyncRoot(resolve => {
const signal = createSignal<LocationChange>({
value: "/foo/bar"
});
const { location, navigatorFactory } = createRouterContext({ signal }, fakeBranches);
const navigate = navigatorFactory();
const count = createCounter(() => location.pathname);
expect(location.pathname).toBe("/foo/bar");
navigate("/foo/bar");
setTimeout(() => {
expect(location.pathname).toBe("/foo/bar");
expect(count()).toBe(0);
resolve();
});
}));
test(`update the integrationSignal`, () =>
createAsyncRoot(resolve => {
const signal = createSignal<LocationChange>({
value: "/"
});
const { navigatorFactory } = createRouterContext({ signal }, fakeBranches);
const navigate = navigatorFactory();
expect(signal[0]().value).toBe("/");
navigate("/foo/bar");
waitFor(() => signal[0]().value === "/foo/bar").then(n => {
expect(n).toBe(1);
expect(signal[0]().replace).not.toBe(true);
resolve();
});
}));
test(`pass state to location`, () =>
createAsyncRoot(resolve => {
const state = { foo: "bar" };
const signal = createSignal<LocationChange>({ value: "/" });
const { location, navigatorFactory } = createRouterContext({ signal }, fakeBranches);
const navigate = navigatorFactory();
expect(location.state).toBeUndefined();
navigate("/foo", { state });
waitFor(() => signal[0]().value === "/foo").then(n => {
expect(n).toBe(1);
expect(location.state).toEqual(state);
resolve();
});
}));
test(`allow state replacement without location change`, () =>
createAsyncRoot(resolve => {
const state = { foo: "bar" };
const signal = createSignal<LocationChange>({ value: "/" });
const { location, navigatorFactory } = createRouterContext({ signal }, fakeBranches);
const navigate = navigatorFactory();
expect(location.state).toBeUndefined();
navigate("/", { state });
waitFor(() => signal[0]().state === state).then(n => {
expect(n).toBe(1);
expect(location.state).toEqual(state);
resolve();
});
}));
test(`be able to be called many times before it updates the integrationSignal`, () =>
createAsyncRoot(resolve => {
const signal = createSignal<LocationChange>({
value: "/"
});
const { navigatorFactory } = createRouterContext({ signal }, fakeBranches);
const navigate = navigatorFactory();
expect(signal[0]()).toEqual({ value: "/" });
navigate("/foo/1");
navigate("/foo/2");
navigate("/foo/3");
navigate("/foo/4");
navigate("/foo/5");
waitFor(() => signal[0]().value === "/foo/5").then(n => {
expect(n).toBe(1);
expect(signal[0]().replace).not.toBe(true);
resolve();
});
}));
test(`throw if called more than 100 times during a reactive update`, () => {
createRoot(() => {
const signal = createSignal<LocationChange>({
value: "/"
});
const { navigatorFactory } = createRouterContext({ signal }, fakeBranches);
const navigate = navigatorFactory();
function pushAlot() {
for (let i = 0; i < 101; i++) {
navigate(`/foo/${i}`);
}
}
expect(pushAlot).toThrow("Too many redirects");
});
});
}); // end of "have member `navigate`"
describe("have member `isRouting` which should", () => {
test("be true when the push or replace causes transition", () =>
createAsyncRoot(resolve => {
const signal = createSignal<LocationChange>({
value: "/"
});
const { navigatorFactory, isRouting } = createRouterContext({ signal }, fakeBranches);
const navigate = navigatorFactory();
expect(isRouting()).toBe(false);
navigate("/target");
expect(isRouting()).toBe(true);
waitFor(() => !isRouting()).then(resolve);
}));
test("turn false, only after location has changed", () =>
createAsyncRoot(resolve => {
const signal = createSignal<LocationChange>({
value: "/"
});
const { navigatorFactory, isRouting } = createRouterContext({ signal }, fakeBranches);
const navigate = navigatorFactory();
navigate("/target");
// capture location immediately after `isRouting` turns false
let postRoutingValue: string | undefined;
createComputed(() => {
if (!isRouting() && !postRoutingValue) {
postRoutingValue = signal[0]().value;
}
});
return waitFor(() => !isRouting())
.then(() => {
expect(postRoutingValue).toBe("/target");
})
.finally(resolve);
}));
});
});