Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close must receive a function or a falsey value #33

Merged
merged 3 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,33 @@ function wrap (server, opts, instance) {
}

server[afterKey] = function (func) {
if (typeof func !== 'function') {
throw new Error('not a function')
}
instance.after(encapsulateThreeParam(func, this))
return this
}

server[readyKey] = function (func) {
if (typeof func !== 'function') {
throw new Error('not a function')
}
instance.ready(encapsulateThreeParam(func, this))
return this
}

server[onCloseKey] = function (func) {
if (typeof func !== 'function') {
throw new Error('not a function')
}
instance.onClose(encapsulateTwoParam(func, this))
return this
}

server[closeKey] = function (func) {
if (func && typeof func !== 'function') {
throw new Error('not a function')
}
instance.close(encapsulateThreeParam(func, this))
return this
}
Expand Down Expand Up @@ -207,6 +219,9 @@ Boot.prototype.onClose = function (func) {
Boot.prototype.close = function (func) {
this._error = null
if (func) {
if (func && typeof func !== 'function') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here just the typeof is enough.

throw new Error('not a function')
}
this._closeQ.push(func)
this._thereIsCloseCb = true
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
},
"homepage": "https://github.com/mcollina/avvio#readme",
"devDependencies": {
"express": "^4.15.4",
"express": "^4.16.2",
"pre-commit": "^1.2.2",
"standard": "^10.0.3",
"tap": "^10.7.2",
"tap": "^10.7.3",
"then-sleep": "^1.0.1"
},
"dependencies": {
Expand Down
40 changes: 40 additions & 0 deletions test/close.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,43 @@ test('close without a cb', (t) => {

app.close()
})

test('close passing not a function', (t) => {
t.plan(1)

const app = boot()

app.onClose((instance, done) => {
t.ok('called')
done()
})

t.throws(() => app.close({}), { message: 'not a function' })
})

test('close passing not a function', (t) => {
t.plan(1)

const app = boot()

app.onClose((instance, done) => {
t.ok('called')
done()
})

t.throws(() => app.close({}), { message: 'not a function' })
})

test('close passing not a function when wrapping', (t) => {
t.plan(1)

const app = {}
boot(app)

app.onClose((instance, done) => {
t.ok('called')
done()
})

t.throws(() => app.close({}), { message: 'not a function' })
})