-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
288 lines (258 loc) · 8.67 KB
/
index.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*!
* minibase-tests <https://github.com/node-minibase/minibase-tests>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var assert = require('assert')
var Bluebird = require('bluebird')
var assertKindof = require('assert-kindof')
var EventEmitter3 = require('eventemitter3')
var Runner = require('./runner')
/**
* > Test any app based on [minibase][] and [base][], just
* pass constructor as `App` argument. If it is `base` based
* pass `opts.isBase: true` option. When run `.runTests` it
* returns resolved Promise with array with length of 0 if all
* tests are passed. If any of the tests fails that `result`
* array will contain these tests - their title, index and the error.
* Resolved array also has `.tests` property which is the count of
* all tests, so easily you can do `res.tests - res.length` to find
* how many tests are failed, and get them by outputing `res`.
*
* **Example**
*
* ```js
* var suite = require('minibase-tests')
* var Base = require('base')
* var Assemble = require('assemble-core')
* var Templates = require('templates')
* var MiniBase = require('minibase').MiniBase
*
* suite(Base, { isBase: true })
* .runTests().then(function (res) {
* // if `res` has bigger length
* // it will contain failed tests
* console.log(res.length) // => 0
* console.log(res.tests) // => 17
* })
* suite(Assemble, { isBase: true })
* .runTests().then(function (res) {
* console.log(res.length) // => 0
* console.log(res.tests) // => 17
* })
* suite(Templates, { isBase: true })
* .runTests().then(function (res) {
* console.log(res.length) // => 0
* console.log(res.tests) // => 17
* })
*
* // MiniBase itself passes these tests too
* suite(MiniBase).runTests().then(function (res) {
* console.log(res.length) // => 0
* console.log(res.tests) // => 17
* })
*
* function MyApp () {
* MiniBase.call(this)
* }
* MiniBase.extend(MyApp)
*
* suite(MyApp).runTests().then(function (res) {
* console.log(res.length) // => 0
* console.log(res.tests) // => 17
* })
* ```
*
* @name suite
* @param {Function} `App` app constructor, if not a function returns rejected promise
* @param {Object} `opts` optional object, pass `isBase: true` for [base][] apps
* @return {Promise} promise if `App` not a function or instance of [Runner](./runner.js), so call `.runTests()`
* @api public
*/
module.exports = function minibaseTests (App, opts) {
if (typeof App !== 'function') {
var err = new TypeError('minibase-tests: expect `App` to be constructor')
return Bluebird.reject(err)
}
opts = opts && typeof opts === 'object' ? opts : {}
var app = App()
var runner = new Runner()
runner.addTest('should return an instance of MiniBase with or without `new` keyword', function () {
if (opts.isBase) {
return Bluebird.resolve()
}
assert.strictEqual(typeof (new App()), 'object')
assert.strictEqual(typeof (App()), 'object')
return Bluebird.resolve()
})
runner.addTest('should be an instance of event emitter', function () {
if (opts.isBase) {
return Bluebird.resolve()
}
assert.strictEqual(App() instanceof EventEmitter3, true, 'should be instanceof EventEmitter3')
assert.strictEqual((new App()) instanceof EventEmitter3, true, 'should be instanceof EventEmitter3')
return Bluebird.resolve()
})
runner.addTest('should expose `.delegate`, `.define` and `.extend` static methods', function () {
assertKindof.function(App.extend)
if (opts.isBase) {
return Bluebird.resolve()
}
assertKindof.function(App.delegate)
assertKindof.function(App.define)
return Bluebird.resolve()
})
runner.addTest('should expose `.define`, `.use` and `.delegate` prototype methods', function () {
assert.ok(assertKindof.is.function(app.use))
assert.ok(assertKindof.is.function(app.define))
if (opts.isBase) {
return Bluebird.resolve()
}
assert.ok(assertKindof.is.function(app.delegate))
return Bluebird.resolve()
})
runner.addTest('should extend the given Ctor with static methods', function () {
function Ctor () {
App.call(this)
}
App.extend(Ctor)
assertKindof.function(Ctor.extend)
if (!opts.isBase) {
assertKindof.function(Ctor.define)
assertKindof.function(Ctor.delegate)
}
function foo () {}
Ctor.extend(foo)
assertKindof.function(foo.extend)
if (!opts.isBase) {
assert.strictEqual(typeof foo.delegate, 'function')
assert.strictEqual(typeof foo.define, 'function')
}
assertKindof.undefined(foo())
assertKindof.object(new Ctor())
return Bluebird.resolve()
})
runner.addTest('should extend the prototype of given Ctor', function () {
function Foo () {
App.call(this)
}
App.extend(Foo)
var foo = new Foo()
assertKindof.function(foo.use)
assertKindof.function(foo.define)
if (!opts.isBase) assertKindof.function(foo.delegate)
return Bluebird.resolve()
})
runner.addTest('should expose `options` instance properties', function () {
assertKindof.object(app.options)
if (!opts.isBase) assertKindof.number(app._anonymousPluginsCount)
return Bluebird.resolve()
})
runner.addTest('should expose event emitter prototype methods', function () {
var some = new App({
foo: 'qqq'
})
assertKindof.function(some.on)
assertKindof.object(some)
assertKindof.object(some.options)
assertKindof.function(some.emit)
assertKindof.function(some.once)
return Bluebird.resolve()
})
runner.addTest('should call the function passed to `.use` method', function () {
return new Bluebird(function (resolve, reject) {
var base = App()
base.once('error', reject)
base.use(function () {
resolve()
})
})
})
runner.addTest('should `.use` has instance context', function () {
return new Bluebird(function (resolve, reject) {
var base = new App()
base.foo = 123
base.once('error', reject)
base.use(function () {
assert.strictEqual(this instanceof App, true)
if (!opts.isBase) assert.strictEqual(this instanceof EventEmitter3, true)
assert.strictEqual(this.foo, 123)
resolve()
})
})
})
runner.addTest('should `.use` be passed with app instance context', function () {
return new Bluebird(function (resolve) {
app.bar = 'foo'
app.use(function (self) {
assert.strictEqual(self.bar, 'foo')
assert.strictEqual(self instanceof App, true)
if (!opts.isBase) assert.strictEqual(self instanceof EventEmitter3, true)
resolve()
})
})
})
runner.addTest('should `.define` a key-value pair on the instance', function () {
var app = new App({ aaa: 'bbb' })
app.define('foo', 'bar')
assert.strictEqual(app.foo, 'bar')
return Bluebird.resolve()
})
runner.addTest('should `.define` an own property', function () {
app.define('bar', 123)
assert.strictEqual(app.hasOwnProperty('bar'), true)
return Bluebird.resolve()
})
runner.addTest('should `.define` a non-emumerable property', function () {
app.define('qux', 'bar')
assert.strictEqual(Object.keys(app).indexOf('qux'), -1)
return Bluebird.resolve()
})
runner.addTest('should define multiple properties with `.delegate`', function () {
if (opts.isBase) {
return Bluebird.resolve()
}
app.delegate({
foo: 'xxx',
baz: 'zzz'
})
assert.strictEqual(app.hasOwnProperty('foo'), true)
assert.strictEqual(app.hasOwnProperty('baz'), true)
assert.strictEqual(app.foo, 'xxx')
assert.strictEqual(app.baz, 'zzz')
return Bluebird.resolve()
})
runner.addTest('should emit `error` event when error in plugin', function () {
if (opts.isBase) {
return Bluebird.resolve()
}
var app = new App({ silent: true })
return new Bluebird(function (resolve) {
app.on('error', function (err) {
assertKindof.error(err)
assertKindof.string(err.name)
assertKindof.string(err.message)
assert.strictEqual(err.message, 'plugin err')
assert.strictEqual(err instanceof Error, true)
resolve()
})
app.use(function (app) {
throw new Error('plugin err')
})
})
})
runner.addTest('should default MiniBase plugins passed to `.use` be synchronous', function () {
return new Bluebird(function (resolve, reject) {
app.once('error', reject)
app.use(function (self, cb) {
if (!opts.isBase) assert.strictEqual(arguments.length, 1)
if (!opts.isBase) assert.strictEqual(assertKindof.is.function(cb), false)
assertKindof.object(self)
resolve()
})
})
})
return runner
}