-
Notifications
You must be signed in to change notification settings - Fork 55
/
ResolverUtils.test.js
121 lines (107 loc) · 3.12 KB
/
ResolverUtils.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
import delay from 'delay';
import Matcher from '../src/Matcher';
import {
accumulateRouteValues,
checkResolved,
getComponents,
getRouteMatches,
getRouteValues,
isResolved,
} from '../src/ResolverUtils';
describe('ResolverUtils', () => {
let match;
const Foo = () => null;
const Bar = () => null;
beforeEach(() => {
const matcher = new Matcher([
{
path: 'foo',
Component: Foo,
value: 9,
children: [
{
path: 'bar',
getComponent: () => Bar,
getValue: ({ params }) => params.quux,
children: {
nav: [{ path: '(.*)?' }],
main: [{ path: 'baz' }, { path: 'qux/:quux' }],
},
},
],
},
]);
match = matcher.match({ pathname: '/foo/bar/qux/a' });
match.routes = matcher.getRoutes(match);
});
describe('checkResolved, isResolved', () => {
it('should return true for non-promises', async () => {
expect(isResolved(await checkResolved({}))).toBe(true);
});
it('should return true for resolved promises', async () => {
expect(isResolved(await checkResolved(Promise.resolve({})))).toBe(true);
expect(
isResolved(
await checkResolved(Promise.resolve({}).then((value) => value)),
),
).toBe(true);
});
it('should return false for unresolved promises', async () => {
expect(
isResolved(
await checkResolved(
// FIXME: This is not quite the right condition, but the test is
// flaky on Travis with a 0-delay timeout.
delay(10),
),
),
).toBe(false);
expect(isResolved(await checkResolved(new Promise(() => {})))).toBe(
false,
);
});
});
describe('accumulateRouteValues', () => {
it('should accumulate route values along match tree', () => {
expect(
accumulateRouteValues(
getRouteMatches(match),
match.routeIndices,
(value, { route: { path } }) => `${value}/${path}`,
'',
),
).toEqual(['/foo', '/foo/bar', '/foo/bar/(.*)?', '/foo/bar/qux/:quux']);
});
});
describe('getRouteMatches', () => {
it('should get per-route match information', () => {
expect(getRouteMatches(match)).toMatchObject([
{ route: { path: 'foo' }, params: { quux: 'a' } },
{ route: { path: 'bar' }, params: { quux: 'a' } },
{ route: { path: '(.*)?' }, params: { quux: 'a' } },
{ route: { path: 'qux/:quux' }, params: { quux: 'a' } },
]);
});
});
describe('getRouteValues', () => {
it('should get static and computed route values', () => {
expect(
getRouteValues(
getRouteMatches(match),
(route) => route.getValue,
(route) => route.value,
),
).toEqual([9, 'a', undefined, undefined]);
});
});
describe('getComponents', () => {
it('should get static and computed route components', () => {
expect(getComponents(getRouteMatches(match))).toEqual([
Foo,
Bar,
undefined,
undefined,
]);
});
});
});