-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
199 lines (166 loc) · 5.53 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*!
* dush-router <https://github.com/tunnckoCore/dush-router>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
var test = require('mukla')
var dush = require('dush')
var router = require('./index')
var app = dush().use(router())
test('should add `.addRoute`, `.createRoute` and `.navigate` methods', function (done) {
test.strictEqual(typeof app.addRoute, 'function')
test.strictEqual(typeof app.createRoute, 'function')
test.strictEqual(typeof app.navigate, 'function')
done()
})
test('should `.createRoute` return a Route object', function (done) {
var handler = function handlerName () {}
var route = app.createRoute('/foobar', handler)
test.strictEqual(typeof route, 'object')
test.strictEqual(route.route, '/foobar')
test.strictEqual(typeof route.handler, 'function')
test.strictEqual(typeof route.match, 'function')
test.strictEqual(route.handler.name, 'handlerName')
test.strictEqual(app._routes.length, 0)
done()
})
test('should route.match(pathname) return object on match', function (done) {
var called = 0
var route = app.createRoute('/user/:name', function () {
called++
})
test.deepEqual(route.keys, ['name'])
var params = route.match('/user/quxie')
test.strictEqual(typeof params, 'object')
test.strictEqual(params.name, 'quxie')
test.strictEqual(app._routes.length, 0)
test.strictEqual(called, 0)
done()
})
test('should `route.match(pathname)` return null if match but no params', function (done) {
var called = 0
var route = app.createRoute('/hi', function heya () {
called++
})
var res = route.match('/hi')
test.strictEqual(res, null)
test.strictEqual(called, 0)
done()
})
test('should `route.match(pathname)` return false if no match', function (done) {
var app = dush()
app.use(router())
var r = app.createRoute('/xyz', function () {})
test.strictEqual(r.match('/xxx'), false)
done()
})
test('should `.addRoute(route, handler)` return self', function (done) {
var ret = app.addRoute('/foobar', function () {})
test.strictEqual(typeof ret, 'object')
test.strictEqual(typeof ret.addRoute, 'function')
test.strictEqual(typeof ret.navigate, 'function')
test.strictEqual(typeof ret.createRoute, 'function')
test.strictEqual(ret._routes.length, 1)
test.strictEqual(app._routes.length, 1)
done()
})
test('should `.navigate` call route handler with params and given state', function (done) {
app.addRoute('/hello/:place', function (context) {
test.strictEqual(typeof context, 'object')
test.strictEqual(typeof context.state, 'object')
test.strictEqual(typeof context.params, 'object')
test.strictEqual(context.params.place, 'world')
test.strictEqual(context.state.foo, 'bar')
test.strictEqual(context.route, '/hello/:place')
test.strictEqual(context.pathname, '/hello/world')
done()
})
app.navigate('/hello/world', { foo: 'bar' })
})
test('should emit `notFound` event when no route found', function (done) {
var app = dush().use(router())
var called = 0
app.on('notFound', function (context) {
test.strictEqual(typeof context, 'object')
test.strictEqual(context.pathname, '/zazzy')
test.deepEqual(context.state, { aaa: 'bbb' })
called++
})
app.addRoute('/goo/gle', function () {})
app.addRoute('/foobar', function () {})
app.navigate('/zazzy', { aaa: 'bbb' })
test.strictEqual(app._routes.length, 2)
test.strictEqual(called, 1)
done()
})
test('should `.navigate` return what is returned from the route handler', function (done) {
app.addRoute('/abc', function routeHandler (ctx) {
return 100 + ctx.state.num
})
var res = app.navigate('/abc', { num: 200 })
test.strictEqual(res, 300)
done()
})
test('should allow custom `.on("route")` to change handler arguments', function (done) {
var app = dush().use(router())
var called = 0
// remove the default handling
app.off('route')
// define new one
app.on('route', function onRoute (handler, context) {
return handler(context.state, context.params || {})
})
app.once('notFound', function (context) {
test.strictEqual(context.pathname, '/zaz')
called = called + 1
})
app.addRoute('/foo/:id', function fooid (state, params) {
test.deepEqual(state, { name: 'charlike' })
test.deepEqual(params, { id: 123 })
return (called = called + 1)
})
app.addRoute('/barry', function (state, params) {
test.deepEqual(state, {})
test.deepEqual(params, {})
return (called = called + 1)
})
app.navigate('/zaz')
var ret = app.navigate('/foo/123', { name: 'charlike' })
test.strictEqual(ret, 2)
var res = app.navigate('/barry')
test.strictEqual(res, 3)
test.strictEqual(called, 3)
done()
})
test('should add multiple handlers on same route', function (done) {
var app = dush().use(router())
var called = 0
app.addRoute('/foo/:id', function (ctx) {
test.deepStrictEqual(ctx.params, { id: 'bar' })
called++
})
app.addRoute('/foo/:id', function (ctx) {
test.deepStrictEqual(ctx.params, { id: 'bar' })
test.strictEqual(called, 1)
called++
})
app.navigate('/foo/bar')
test.strictEqual(called, 2)
done()
})
test('should passed handler to `route` event return non-undefined', function (done) {
var app = dush().use(router())
app.off('route')
app.on('route', function (viewFn, context) {
var ret = viewFn(context)
test.strictEqual(ret, 123, 'should be non-undefined')
done()
})
app.addRoute('/foo/quxie', function () {
return 123
})
app.navigate('/foo/quxie')
})