-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
155 lines (132 loc) · 3.47 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
'use strict';
require('mocha');
require('should');
var assert = require('assert');
var Base = require('base-app');
var routes = require('./');
var app;
describe('routes', function() {
beforeEach(function() {
app = new Base();
app.isApp = true;
app.use(routes());
});
describe('routes', function() {
it('should create a route for the given path:', function(cb) {
var count = 0;
app.once('onLoad', function(view) {
count++;
assert.deepEqual(view, {
path: 'blog/foo.js',
options: {
handled: [ 'onLoad' ],
method: 'onLoad'
}
});
});
app.route('blog/:title')
.onLoad(function(view, next) {
count++;
next();
});
app.handle('onLoad', { path: 'blog/foo.js' }, function(err) {
if (err) {
cb(err);
return;
}
assert.equal(count, 2);
cb();
});
});
it('should emit events when a route method is called:', function(cb) {
var count = 0;
app.once('onLoad', function(view) {
count++;
assert.deepEqual(view, {
path: 'blog/foo.js',
options: {
handled: [ 'onLoad' ],
method: 'onLoad'
}
});
});
app.param('title', function(view, next, title) {
assert.equal(title, 'foo.js');
count++;
next();
});
app.onLoad('blog/:title', function(view, next) {
assert.equal(view.path, 'blog/foo.js');
count++;
next();
});
app.handle('onLoad', {path: 'blog/foo.js'}, function(err) {
if (err) {
cb(err);
return;
}
assert.equal(count, 3);
cb();
});
});
it('should emit and handle errors', function(cb) {
var count = 0;
app.on('error', function(err) {
count++;
assert.equal(err.message, "'foo.js' == 'fo.js'");
});
// should be wrong...
app.param('title', function(view, next, title) {
count++;
assert.equal(title, 'fo.js');
next();
});
app.onLoad('blog/:title', function(view, next) {
count++;
assert.equal(view.path, '/blog/foo.js');
next();
});
app.handle('onLoad', {path: 'blog/foo.js'}, function(err) {
assert(err);
assert.equal(count, 2);
cb();
});
});
it('should have path property', function() {
var route = new app.Route('/blog/:year/:month/:day/:slug').all([
function() {}
]);
assert.equal(route.path, '/blog/:year/:month/:day/:slug');
});
it('should have stack property', function() {
var route = new app.Route('/blog/:year/:month/:day/:slug').all([
function() {}
]);
assert.equal(Array.isArray(route.stack), true);
assert.equal(route.stack.length, 1);
});
});
describe('params', function() {
it('should call param function when routing', function(cb) {
var count = 0;
app.param('id', function(view, next, id) {
count++;
assert.equal(id, '123');
next();
});
app.all('/foo/:id/bar', function(view, next) {
count++;
assert.equal(view.options.params.id, '123');
next();
});
app.router.handle({ path: '/foo/123/bar' }, function(err) {
if (err) {
cb(err);
return;
}
assert.equal(count, 2);
cb();
});
});
});
});