Skip to content

Commit

Permalink
fix: extracted tests from browser-as-a-service
Browse files Browse the repository at this point in the history
  • Loading branch information
hfreire committed Mar 15, 2017
1 parent 0d675b2 commit 20d2829
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/serverful.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ const configureRoutes = function () {
}

try {
const module = require(join(__dirname, `/routes/${entry}`))
const module = require(join(path, `/routes/${entry}`))

this.http.route(module.toRoute())
} catch (error) {
Logger.error(error)
}
})
.catch(() => {})
})
}

Expand Down
128 changes: 128 additions & 0 deletions test/serverful.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2017, Hugo Freire <hugo@exec.sh>.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

describe('Serverful', () => {
let subject
let Serverful
let http

before(() => {
http = td.object([ 'connection', 'auth', 'on', 'route', 'start', 'stop', 'register' ])
http.auth.scheme = td.function()
http.auth.strategy = td.function()
http.auth.default = td.function()
http.app = td.object([])
})

afterEach(() => td.reset())

describe('when constructing a server', () => {
let pingRoute
let healthcheckRoute

before(() => {
Serverful = require('../src/serverful')
subject = new Serverful()
})

beforeEach(() => {
td.replace('hapi', { 'Server': function () { return http } })

pingRoute = td.object([])
const ping = td.replace('../src/routes/ping', td.object([ 'toRoute' ]))
td.when(ping.toRoute()).thenReturn(pingRoute)

healthcheckRoute = td.object([])
const healthcheck = td.replace('../src/routes/healthcheck', td.object([ 'toRoute' ]))
td.when(healthcheck.toRoute()).thenReturn(healthcheckRoute)
})

afterEach(() => {
delete require.cache[ require.resolve('../src/serverful') ]
})

it('should listen on hapi server start event', () => {
Serverful = require('../src/serverful')
subject = new Serverful()

td.verify(http.on('start'), { times: 1, ignoreExtraArgs: true })
})

it('should listen on hapi server stop event', () => {
Serverful = require('../src/serverful')
subject = new Serverful()

td.verify(http.on('stop'), { times: 1, ignoreExtraArgs: true })
})

it('should listen on hapi server response event', () => {
Serverful = require('../src/serverful')
subject = new Serverful()

td.verify(http.on('response'), { times: 1, ignoreExtraArgs: true })
})

it('should listen on hapi server request-error', () => {
Serverful = require('../src/serverful')
subject = new Serverful()

td.verify(http.on('request-error'), { times: 1, ignoreExtraArgs: true })
})

it.skip('should configure route to ping ', () => {
Serverful = require('../src/serverful')
subject = new Serverful()

td.verify(http.route(pingRoute), { times: 1 })
})

it.skip('should configure route to healthcheck ', () => {
Serverful = require('../src/serverful')
subject = new Serverful()

td.verify(http.route(healthcheckRoute), { times: 1 })
})
})

describe('when starting server', () => {
before(() => {
td.when(http.start()).thenCallback()

td.replace('hapi', { 'Server': function () { return http } })

Serverful = require('../src/serverful')
subject = new Serverful()
})

it('should invoke hapi server start', () => {
return subject.start()
.finally(() => {
td.verify(http.start(), { times: 1, ignoreExtraArgs: true })
})
})
})

describe('when stopping a running server', () => {
before(() => {
td.when(http.start()).thenCallback()
td.when(http.stop()).thenCallback()

td.replace('hapi', { 'Server': function () { return http } })

Serverful = require('../src/serverful')
subject = new Serverful()
return subject.start()
})

it('should invoke hapi server stop', () => {
return subject.stop()
.finally(() => {
td.verify(http.stop(), { times: 1, ignoreExtraArgs: true })
})
})
})
})

0 comments on commit 20d2829

Please sign in to comment.