-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
230 lines (186 loc) · 5.19 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
230
/*!
* dush-options <https://github.com/tunnckoCore/dush-options>
*
* 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 options = require('./index')
var dush = require('dush')
var methods = require('dush-methods')
var isRegistered = require('minibase-is-registered')
function factory () {
return dush().use(methods()).use(isRegistered())
}
var app = factory()
test('should have `.option`, `.enable`, `.disable` methods', function (done) {
app.use(options())
test.strictEqual(typeof app.options, 'object')
test.strictEqual(typeof app.option, 'function')
test.strictEqual(typeof app.enable, 'function')
test.strictEqual(typeof app.disable, 'function')
done()
})
test('should `.enable` an option', function (done) {
var called = false
app.on('enable', function (key) {
test.strictEqual(key, 'settle')
called = true
})
app.enable('settle')
test.strictEqual(app.options.settle, true)
test.strictEqual(called, true)
done()
})
test('should `.disable` an option', function (done) {
var app = factory()
app.use(options())
app.disable('foobar')
var called = false
app.on('disable', function (key) {
test.strictEqual(key, 'quxies')
called = true
})
app.disable('quxies')
test.deepStrictEqual(app.options, {
foobar: false,
quxies: false
})
test.strictEqual(called, true)
done()
})
test('should merge `opts` pass to plugin with `app.options`', function (done) {
var app = factory()
app.use(function (app) {
app.options = {}
app.options.foo = 'bar'
})
app.use(options({ bar: 'qux' }))
test.deepStrictEqual(app.options, { foo: 'bar', bar: 'qux' })
done()
})
test('should not call plugin if has app.isRegistered and already registered', function (done) {
var app = factory()
app.use(options({ aa: 'bb' }))
test.deepStrictEqual(app.options, { aa: 'bb' })
app.use(options({ bb: 'qux' }))
test.deepStrictEqual(app.options, { aa: 'bb' })
done()
})
test('should `.option()` with no arguments return app.options', function (done) {
var opts = { init: 123 }
var app = factory().use(options(opts))
test.deepStrictEqual(app.options, opts)
var res = app.option()
test.deepStrictEqual(res, opts)
test.deepStrictEqual(app.options, res)
done()
})
test('should `.option(key)` get an option, support dot notation', function (done) {
var app = factory().use(options({ aa: { bb: 'cc' } }))
test.strictEqual(app.option('aa.bb'), 'cc')
test.deepStrictEqual(app.option('aa'), { bb: 'cc' })
done()
})
test('should `.option(object)` merge the object with app.options', function (done) {
var app = factory().use(options({
beep: 'boop'
}))
app.option({ qux: 222 })
test.deepStrictEqual(app.options, { beep: 'boop', qux: 222 })
done()
})
test('should `.option(key, value)` set option, support dot notation', function (done) {
var app = factory()
app.use(options({
qux: 22,
bar: {
baz: 333
}
}))
app.option('bar.baz', 555)
app.option('bar.qux', 555)
app.option('foo', 'bar')
app.option('qux', 'qux')
test.deepStrictEqual(app.options, {
bar: {
baz: 555,
qux: 555
},
foo: 'bar',
qux: 'qux'
})
done()
})
test('should emit `option` event with `key` and `val` when work as get', function (done) {
var app = factory().use(options({
foo: 'bar'
}))
var called = 0
app.on('option', function (options, key, val) {
test.deepEqual(options, { foo: 'bar' })
test.strictEqual(key, 'foo')
test.strictEqual(val, 'bar')
called++
})
var val = app.option('foo')
test.strictEqual(val, 'bar')
test.strictEqual(called, 1)
done()
})
test('should emit `option` event for dot notation get', function (done) {
var app = factory().use(options({
foo: { bar: 'qux' }
}))
app.on('option', function (_, key, val) {
test.strictEqual(key, 'foo.bar')
test.strictEqual(val, 'qux')
done()
})
app.option('foo.bar')
})
test('should emit `option` event when `app.option()` work as get all options', function (done) {
var opts = {
a: 'b',
c: 'd'
}
var called = 0
var app = factory().use(options(opts))
app.on('option', function (_opts) {
test.deepEqual(_opts, opts)
called++
})
var __options__ = app.option()
test.deepEqual(__options__, opts)
test.strictEqual(called, 1)
done()
})
test('should emit `option` event with prev and new options', function (done) {
var opts = { aaa: 'bbb', ccc: { ddd: 'ddd' } }
var app = factory().use(options(opts))
app.on('option', function (prev, curr) {
test.deepEqual(prev.ccc, { ddd: 'ddd' })
test.deepEqual(curr.ccc, { xyz: 123 })
done()
})
app.option({
ccc: { xyz: 123 }
})
})
test('should emit `option` when set value to key path', function (done) {
var opts = { a: { bc: 333 } }
var called = false
var app = factory().use(options(opts))
app.on('option', function (prev, key, val) {
test.deepEqual(prev, opts)
test.strictEqual(key, 'a.bc')
test.strictEqual(val, 'foo')
called = true
})
app.option('a.bc', 'foo')
test.deepEqual(app.options, { a: { bc: 'foo' } })
test.strictEqual(called, true)
done()
})