Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50675 views
1
/*
2
* dispatch-test.js: Tests for the core dispatch method.
3
*
4
* (C) 2011, Nodejitsu Inc.
5
* MIT LICENSE
6
*
7
*/
8
9
var assert = require('assert'),
10
vows = require('vows'),
11
director = require('../../../lib/director');
12
13
vows.describe('director/core/dispatch').addBatch({
14
"An instance of director.Router": {
15
topic: function () {
16
var that = this;
17
that.matched = {};
18
that.matched['/'] = [];
19
that.matched['foo'] = [];
20
that.matched['f*'] = []
21
22
var router = new director.Router({
23
'/': {
24
before: function () { that.matched['/'].push('before /') },
25
on: function () { that.matched['/'].push('on /') },
26
after: function () { that.matched['/'].push('after /') }
27
},
28
'/foo': {
29
before: function () { that.matched.foo.push('before foo') },
30
on: function () { that.matched.foo.push('on foo') },
31
after: function () { that.matched.foo.push('after foo') },
32
'/bar': {
33
before: function () { that.matched.foo.push('before foo bar') },
34
on: function () { that.matched.foo.push('foo bar') },
35
after: function () { that.matched.foo.push('after foo bar') },
36
'/buzz': function () { that.matched.foo.push('foo bar buzz') }
37
}
38
},
39
'/f*': {
40
'/barbie': function () { that.matched['f*'].push('f* barbie') }
41
}
42
});
43
44
router.configure({
45
recurse: 'backward'
46
});
47
48
return router;
49
},
50
"should have the correct routing table": function (router) {
51
assert.isObject(router.routes.foo);
52
assert.isObject(router.routes.foo.bar);
53
assert.isObject(router.routes.foo.bar.buzz);
54
assert.isFunction(router.routes.foo.bar.buzz.on);
55
},
56
"the dispatch() method": {
57
"/": function (router) {
58
assert.isTrue(router.dispatch('on', '/'));
59
assert.isTrue(router.dispatch('on', '/'));
60
61
assert.equal(this.matched['/'][0], 'before /');
62
assert.equal(this.matched['/'][1], 'on /');
63
assert.equal(this.matched['/'][2], 'after /');
64
},
65
"/foo/bar/buzz": function (router) {
66
assert.isTrue(router.dispatch('on', '/foo/bar/buzz'));
67
68
assert.equal(this.matched.foo[0], 'foo bar buzz');
69
assert.equal(this.matched.foo[1], 'before foo bar');
70
assert.equal(this.matched.foo[2], 'foo bar');
71
assert.equal(this.matched.foo[3], 'before foo');
72
assert.equal(this.matched.foo[4], 'on foo');
73
},
74
"/foo/barbie": function (router) {
75
assert.isTrue(router.dispatch('on', '/foo/barbie'));
76
assert.equal(this.matched['f*'][0], 'f* barbie');
77
},
78
"/foo/barbie/": function (router) {
79
assert.isFalse(router.dispatch('on', '/foo/barbie/'));
80
},
81
"/foo/BAD": function (router) {
82
assert.isFalse(router.dispatch('on', '/foo/BAD'));
83
},
84
"/bar/bar": function (router) {
85
assert.isFalse(router.dispatch('on', '/bar/bar'));
86
},
87
"with the strict option disabled": {
88
topic: function (router) {
89
return router.configure({
90
recurse: 'backward',
91
strict: false
92
});
93
},
94
"should have the proper configuration set": function (router) {
95
assert.isFalse(router.strict);
96
},
97
"/foo/barbie/": function (router) {
98
assert.isTrue(router.dispatch('on', '/foo/barbie/'));
99
assert.equal(this.matched['f*'][0], 'f* barbie');
100
},
101
"/foo/bar/buzz": function (router) {
102
assert.isTrue(router.dispatch('on', '/foo/bar/buzz'));
103
104
assert.equal(this.matched.foo[0], 'foo bar buzz');
105
assert.equal(this.matched.foo[1], 'before foo bar');
106
assert.equal(this.matched.foo[2], 'foo bar');
107
assert.equal(this.matched.foo[3], 'before foo');
108
assert.equal(this.matched.foo[4], 'on foo');
109
},
110
}
111
}
112
}
113
}).export(module);
114
115