-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
185 lines (156 loc) · 4.67 KB
/
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
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
/* @jest-environment jsdom */
const { router, route, wildcardRoute } = require('./index.js');
const { T, N, patch } = require('js-sdk-vdom');
function noop() {}
const ROUTE_1_ENTER = 'route_1_enter';
const ROUTE_1_UPDATE = 'route_1_update';
const ROUTE_1_EXIT = 'route_1_exit';
const ROUTE_2_ENTER = 'route_2_enter';
const ROUTE_2_UPDATE = 'route_2_update';
const ROUTE_2_EXIT = 'route_2_exit';
describe('building routes', () => {
it('root route', () => {
const tags = [];
function tag(t) { return function () { tags.push(t); } }
const { path, segments, enter, update, exit } =
route(
'/',
tag(ROUTE_1_ENTER),
tag(ROUTE_1_UPDATE),
tag(ROUTE_1_EXIT)
);
expect(path).toEqual('/');
expect(segments).toEqual([]);
expect(enter).not.toBeNull();
expect(update).not.toBeNull();
expect(exit).not.toBeNull();
});
it('root with a path', () => {
const tags = [];
function tag(t) { return function () { tags.push(t); } }
const { path, segments, enter, update, exit } =
route(
'/a/b/c',
tag(ROUTE_1_ENTER),
tag(ROUTE_1_UPDATE),
tag(ROUTE_1_EXIT)
);
expect(path).toEqual('/a/b/c');
expect(segments).toEqual(['a', 'b', 'c']);
expect(enter).not.toBeNull();
expect(update).not.toBeNull();
expect(exit).not.toBeNull();
});
});
describe('using router', () => {
it('entering a route', () => {
const tags = [];
function tag(t) { return function () { tags.push(t); } }
const r = router(
null,
route('/', tag(ROUTE_1_ENTER), tag(ROUTE_1_UPDATE), tag(ROUTE_1_EXIT)),
);
r('/');
expect(tags).toEqual([ROUTE_1_ENTER]);
});
it('entering and update a route', () => {
const tags = [];
function tag(t) { return function () { tags.push(t); } }
const r = router(
null,
route('/', tag(ROUTE_1_ENTER), tag(ROUTE_1_UPDATE), tag(ROUTE_1_EXIT)),
);
r('/');
r('/');
expect(tags).toEqual([ROUTE_1_ENTER, ROUTE_1_UPDATE]);
});
it('exiting and entering a route', () => {
const tags = [];
function tag(t) { return function () { tags.push(t); } }
const r = router(
null,
route('/a', tag(ROUTE_1_ENTER), tag(ROUTE_1_UPDATE), tag(ROUTE_1_EXIT)),
route('/b', tag(ROUTE_2_ENTER), tag(ROUTE_2_UPDATE), tag(ROUTE_2_EXIT)),
);
r('/a');
r('/a');
r('/b');
r('/b');
expect(tags).toEqual([ROUTE_1_ENTER, ROUTE_1_UPDATE, ROUTE_1_EXIT, ROUTE_2_ENTER, ROUTE_2_UPDATE]);
});
it('matching a route with params', () => {
const tags = [];
function tag(t) {
return function (params) {
expect(params).toEqual({ id: "1", p: 'a' });
tags.push(t);
}
}
const r = router(
null,
route('/:id/a/:p', tag(ROUTE_1_ENTER), tag(ROUTE_1_UPDATE), tag(ROUTE_1_EXIT)),
);
r('/1/a/a');
expect(tags).toEqual([ROUTE_1_ENTER]);
});
it('matching a route with params must discard the params after leave', () => {
const tags = [];
function tag(t, withParams = true) {
return function (params) {
expect(params).toEqual(withParams ? { id: "1", p: 'a' } : {});
tags.push(t);
}
}
const r = router(
null,
route('/:id/a/:p', tag(ROUTE_1_ENTER), tag(ROUTE_1_UPDATE), tag(ROUTE_1_EXIT)),
route('/', tag(ROUTE_2_ENTER, false), tag(ROUTE_2_UPDATE, false), tag(ROUTE_2_EXIT, false)),
);
r('/1/a/a');
r('/1/a/a');
r('/');
r('/');
expect(tags).toEqual([ROUTE_1_ENTER, ROUTE_1_UPDATE, ROUTE_1_EXIT, ROUTE_2_ENTER, ROUTE_2_UPDATE]);
});
it('trigger wildcard route when no route was matched', () => {
const tags = [];
function tag(t) {
return function (params) {
expect(params).toStrictEqual({});
tags.push(t);
}
}
const text = "must not enter";
const r = router(
wildcardRoute(tag(ROUTE_1_ENTER), tag(ROUTE_1_UPDATE), tag(ROUTE_1_EXIT)),
route('/a', tag(text), tag(text), tag(text)),
);
r('/b');
expect(tags).toEqual([ROUTE_1_ENTER]);
});
});
describe('rendering test', () => {
const app = (function (target) {
let p; return function (view) { p = patch(p, view(), target); }
}(document.body));
function list() {
const ls = [1, 2, 3].map(n => T(n.toString()));
return N('ul', { id: 'list' }, {}, ls);
}
function image() {
return N('img', { id: 'img' }, {}, []);
}
const r = router(
null,
route('/a', () => app(list)),
route('/b', () => app(image))
);
it('entering route `/a`', () => {
r('/a');
expect(document.body.firstChild.id).toBe('list');
});
it('rounting to route `/b`', () => {
r('/b');
expect(document.body.firstChild.id).toBe('img');
});
});