Skip to content

Commit

Permalink
fix(events): emit "option", "enable" and "disable" events
Browse files Browse the repository at this point in the history
fixes #3
  • Loading branch information
tunnckoCore committed Apr 1, 2017
1 parent 41ae23c commit 1eb4084
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,23 @@ module.exports = function dushOptions (options) {

app.option = function option (key, value) {
if (!arguments.length) {
app.emit('option', app.options)
// option:getAll app.options
return app.options
}
if (arguments.length === 1 && typeof key === 'string') {
return get(app.options, key)
var val = get(app.options, key)
app.emit('option', app.options, key, val)
// option:get key
return val
}
if (isObject(key)) {
app.emit('option', app.options, key)
// option:setAll key
app.options = mixin({}, app.options, key)
} else {
app.emit('option', app.options, key, value)
// option:set key, value
set(app.options, key, value)
}
return app.options
Expand Down Expand Up @@ -135,6 +144,7 @@ module.exports = function dushOptions (options) {
*/

app.enable = function enable (key) {
app.emit('enable', key)
app.option(key, true)
return app
}
Expand Down Expand Up @@ -168,6 +178,7 @@ module.exports = function dushOptions (options) {
*/

app.disable = function disable (key) {
app.emit('disable', key)
app.option(key, false)
return app
}
Expand Down
97 changes: 97 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ test('should have `.option`, `.enable`, `.disable` methods', 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()
})

Expand All @@ -44,12 +50,20 @@ test('should `.disable` an option', function (done) {
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()
})

Expand Down Expand Up @@ -131,3 +145,86 @@ test('should `.option(key, value)` set option, support dot notation', function (
})
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()
})

0 comments on commit 1eb4084

Please sign in to comment.