-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragon-router.test.js
218 lines (183 loc) · 6.57 KB
/
dragon-router.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
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
function wait (cb, time) {
return new Promise ((resolve) => {
setTimeout(() => {
cb();
resolve()
}, time)
})
}
['./dist/dragon-router', './dist/dragon-router.min'].forEach(distFile => {
const {Router, Context, DerivedSubpath, RouteHandler, TokenizedPath} = require(distFile)
const router = new Router({registerOn: window});
const middleware1 = (ctx, next) => { ctx.params.middleware1 = true; next(); }
const middleware2 = (ctx, next) => { ctx.params.middleware2 = true; next(); }
const middleware3 = (ctx, next) => { ctx.params.middleware3 = true; next(); }
test(`${distFile} :: We can get all the class definitions out of the package`, () => {
[Router, Context, DerivedSubpath, RouteHandler, TokenizedPath].forEach(classDefinition => {
expect(classDefinition).toBeDefined();
});
});
test(`${distFile} :: We can define literal matchers`, () => {
let calledTimes = 0;
router.use('/user', (context) => {
expect(context.path).toBe('/user');
calledTimes += 1;
})
router.navigate('/user')
expect(calledTimes).toBe(1)
});
test(`${distFile} :: We can define section parameters`, () => {
let calledTimes = 0;
router.use('/user/:section(info|settings)', (context) => {
expect(['info', 'settings']).toContain(context.params.section);
calledTimes += 1;
})
router.navigate('/user/info')
router.navigate('/user/settings')
expect(calledTimes).toBe(2)
});
test(`${distFile} :: We can define optional parameters`, () => {
let calledTimes = 0;
router.use('/about/:person?', (context) => {
expect([undefined, 'sally', 'sam']).toContain(context.params.person);
calledTimes += 1;
})
router.navigate('/about/')
router.navigate('/about/sally')
router.navigate('/about/sam')
expect(calledTimes).toBe(3)
});
test(`${distFile} :: We can define star routes`, () => {
let calledTimes = 0;
router.use('/home*', (context) => {
expect(context.path).toMatch(/^\/home.*$/);
calledTimes += 1;
})
router.navigate('/home')
router.navigate('/homer')
router.navigate('/home/test')
expect(calledTimes).toBe(3)
});
test(`${distFile} :: We can add middleware that can manipulate the Context`, () => {
let calledTimes = 0;
router.use('/test/middleware', middleware1, middleware2, middleware3, (context) => {
expect(context.params).toMatchObject({
middleware1 : true, middleware2 : true, middleware3 : true
});
calledTimes += 1;
})
router.navigate('/test/middleware')
expect(calledTimes).toBe(1)
});
test(`${distFile} :: We can use route handlers`, () => {
let calledTimes = 0;
let handler = new RouteHandler('/test/route-handler', [middleware1, middleware2, middleware3, (context) => {
expect(context.params).toMatchObject({
middleware1 : true, middleware2 : true, middleware3 : true
});
calledTimes += 1;
}]);
router.use(handler)
router.navigate('/test/route-handler')
expect(calledTimes).toBe(1)
});
test(`${distFile} :: We can use derived subpaths`, async () => {
let calledTimes = 0;
router.use(new DerivedSubpath('fillThisPartIn', () => 'this-part-was-filled-in'))
router.use('/test/$:fillThisPartIn(this-part-was-filled-in)/:rest(the-rest-of-the-path)', (context) => {
expect(context.path).toBe('/test/this-part-was-filled-in/the-rest-of-the-path');
calledTimes += 1;
})
router.navigate('/test/the-rest-of-the-path')
router.navigate('/test/this-part-was-filled-in/the-rest-of-the-path')
await wait(() => {
expect(calledTimes).toBe(2)
}, 50)
});
test(`${distFile} :: We can use RegExp`, () => {
let calledTimes = 0;
let matcher = new RegExp(/^\/this\/is\/a\/\w+\/matcher$/)
router.use(matcher, (context) => {
expect(context.path.startsWith('/this/is/a/')).toBe(true)
expect(context.path.endsWith('/matcher')).toBe(true)
calledTimes += 1;
})
router.navigate('/this/is/a/regex/matcher')
router.navigate('/this/is/a/cool/matcher')
router.navigate('/this/is/a/fancy/matcher')
expect(calledTimes).toBe(3)
});
router.unregister()
const router2 = new Router({ basePath: '/test/base-path', registerOn: window });
test(`${distFile} :: We can use mount our app on a base path`, () => {
let calledTimes = 0;
router2.use('/', (context) => {
switch (calledTimes) {
case 0: expect(context.path).toBe('/test/base-path'); break;
case 1: expect(context.path).toBe('/test/base-path/'); break;
}
calledTimes += 1;
})
router2.use('/:test(test1|test2)/:subsection?', (context) => {
switch (calledTimes) {
case 2: expect(context.path).toBe('/test/base-path/test1'); break;
case 3: expect(context.path).toBe('/test/base-path/test2/subsectionA'); break;
case 4: expect(context.path).toBe('/test/base-path/test2/subsectionB'); break;
}
calledTimes += 1;
})
router2.navigate('/test/base-path')
router2.navigate('/test/base-path/')
router2.navigate('/test/base-path/test1')
router2.navigate('/test/base-path/test2/subsectionA')
router2.navigate('/test/base-path/test2/subsectionB')
expect(calledTimes).toBe(5)
});
router2.unregister()
const router3 = new Router({registerOn: window, routerId: 'test'});
test(`${distFile} :: We can use global middleware`, () => {
let calledTimes = 0;
router3.use(
(ctx, next) => {
expect(ctx.beenThroughGlobalMiddleware).toBe(undefined)
calledTimes += 1;
next()
},
(ctx, next) => {
ctx.beenThroughGlobalMiddleware = true
calledTimes += 1;
next()
},
(ctx, next) => {
expect(ctx.beenThroughGlobalMiddleware).toBe(true)
calledTimes += 1;
next()
}
)
router3.use('/test/global/middleware', (ctx) => {
expect(ctx.beenThroughGlobalMiddleware).toBe(true)
calledTimes += 1;
})
router3.navigate('/test/global/middleware')
expect(calledTimes).toBe(4)
});
router3.unregister()
test(`${distFile} :: Router methods are chainable`, () => {
let calledTimes = 0;
new Router()
.registerOn(window)
.use('/test/chainable', (ctx) => {
calledTimes += 1;
expect(ctx.globalMiddlewareFired).toBe(true)
})
.use((ctx, next) => {
ctx.globalMiddlewareFired = true;
next();
})
.navigate('/test/chainable')
.back()
.forward()
.unregister()
expect(calledTimes).toBe(1)
})
})