-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
229 lines (194 loc) · 5 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
'use strict';
require('mocha');
require('should');
var assert = require('assert');
var Base = require('base');
var plugins = require('./');
var base;
describe('plugins', function() {
describe('use', function() {
beforeEach(function() {
base = new Base();
base.use(plugins());
});
it('should add a `fns` property:', function() {
assert(base.fns);
assert(Array.isArray(base.fns));
});
it('should ensure the instance has the `fns` property:', function() {
base = new Base();
delete base.fns;
assert(!base.fns);
base.use(plugins());
assert(base.fns);
assert(Array.isArray(base.fns));
});
it('should ensure a plain object has the `fns` property:', function() {
var obj = {};
plugins()(obj);
assert(obj.fns);
assert(Array.isArray(obj.fns));
});
it('should ensure a plain object has a "use" method:', function() {
var obj = {};
plugins()(obj);
assert(obj.use);
assert.equal(typeof obj.use, 'function');
});
it('should not overwrite an existing `fns` property:', function() {
base = new Base();
base.fns = [function() {}];
base.use(plugins());
assert(base.fns);
assert(Array.isArray(base.fns));
assert.equal(base.fns.length, 1);
});
it('should call the function passed to `use`:', function(cb) {
base.use(function(app) {
assert(app);
cb();
});
});
it('should expose the app instance:', function(cb) {
base.foo = 'bar';
base.use(function(app) {
assert.equal(app.foo, 'bar');
cb();
});
});
it('should expose the app instance as "this":', function(cb) {
base.foo = 'bar';
base.use(function(app) {
assert.equal(this.foo, 'bar');
cb();
});
});
it('should emit `use`:', function(cb) {
base.on('use', function() {
cb();
});
base.use(function(app) {
});
});
});
describe('named plugins', function() {
it('should register named plugins', function() {
var app = new Base();
app.use(plugins());
app.type = 'app';
app.use(plugins());
var names = [];
app.use('foo', function() {
names.push(this.type);
});
app.use('bar', function() {
names.push(this.type);
});
app.use('baz', function() {
names.push(this.type);
});
var foo = new Base();
foo.type = 'foo';
foo.parent = app;
app.run(foo);
var bar = new Base();
bar.type = 'bar';
bar.parent = foo;
foo.run(bar);
var baz = new Base();
baz.type = 'baz';
baz.parent = bar;
bar.run(baz);
assert.deepEqual(names, ['foo', 'bar', 'baz']);
});
});
describe('run', function() {
beforeEach(function() {
base = new Base();
base.use(plugins());
});
it('should expose the run method:', function() {
assert(base.run);
assert.equal(typeof base.run, 'function');
});
it('should run all registered plugins:', function(cb) {
var config = {};
var called = 0;
function letter(ch, val) {
return function(app) {
return function(config) {
called++;
config[ch] = val;
};
};
}
base.use(letter('a', 'b'));
base.use(letter('c', 'd'));
base.use(letter('e', 'f'));
base.run(config);
assert(config.a);
assert(config.c);
assert(config.e);
assert.equal(called, 3);
cb();
});
it('should push returned functions onto `plugins`:', function(cb) {
base.use(function() {
return function() {
};
});
assert.equal(base.fns.length, 1);
cb();
});
it('should run all stored plugins:', function(cb) {
var called = 0;
base.use(function(app) {
return function(config) {
called++;
app.foo = config.foo;
};
});
var config = {foo: 'bar'};
base.run(config);
assert.equal(base.foo, 'bar');
assert.equal(called, 1);
cb();
});
it('should not run on non-object values', function(cb) {
var called = 0;
base.use(function(app) {
return function(config) {
called++;
app.foo = config.foo;
};
});
base.run('foo');
assert.equal(called, 0);
cb();
});
it('should call the `use` method on the object passed to run:', function(cb) {
base
.use(function() {
return function(config) {
config.a = 'a';
};
})
.use(function() {
return function(config) {
config.b = 'b';
};
})
.use(function() {
return function(config) {
config.c = 'c';
};
});
var config = new Base();
base.run(config);
assert.equal(config.a, 'a');
assert.equal(config.b, 'b');
assert.equal(config.c, 'c');
cb();
});
});
});