Skip to content

Commit

Permalink
Merge 25710f9 into 0043728
Browse files Browse the repository at this point in the history
  • Loading branch information
msakrejda committed Sep 9, 2016
2 parents 0043728 + 25710f9 commit 1b0b3f5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -30,6 +30,7 @@
},
"devDependencies": {
"chai": "^3.5.0",
"chai-as-promised": "^5.3.0",
"coveralls": "^2.11.12",
"eslint": "^2.1.0",
"eslint-config-standard": "^6.0.0",
Expand Down
98 changes: 98 additions & 0 deletions test/lib/clusters_test.js
@@ -0,0 +1,98 @@
'use strict'

const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')

chai.use(chaiAsPromised)

const proxyquire = require('proxyquire')
const expect = chai.expect
const mocha = require('mocha')
const describe = mocha.describe
const it = mocha.it
const beforeEach = mocha.beforeEach

const cli = require('heroku-cli-util')
const co = require('co')

const heroku = {}

let fetchAll
let fetchOne

const fetcher = (arg) => {
expect(arg).to.equal(heroku)
return {
all: fetchAll,
addon: fetchOne
}
}

const clusters = proxyquire('../../lib/clusters', {
'./fetcher': fetcher
})

describe('withCluster', () => {
beforeEach(() => {
fetchOne = () => Promise.resolve(null)
fetchAll = () => Promise.resolve([])
cli.exit.mock()
cli.mockConsole()
})

describe('with an explicit cluster argument', () => {
it('propagates the error if the fetcher rejects the promise', () => {
fetchOne = () => Promise.reject(new Error('oh snap'))
let called = false
return expect(co.wrap(clusters.withCluster)(heroku,
'my-app', 'kafka-1',
function * (arg) { called = true }))
.to.be.rejected
.then(() => { expect(called).to.be.false })
})

it('invokes the callback with the returned add-on', () => {
let addon = { name: 'kafka-1' }
fetchOne = () => Promise.resolve(addon)
let calledWith = addon
return expect(co.wrap(clusters.withCluster)(heroku,
'my-app', 'kafka-1',
function * (arg) { calledWith = arg }))
.to.be.fulfilled
.then(() => { expect(calledWith).to.equal(addon) })
})
})

describe('with no explicit cluster argument', () => {
it('warns and exits if no add-ons are found', () => {
fetchAll = () => Promise.resolve([])
let called = false
return expect(co.wrap(clusters.withCluster)(heroku,
'my-app', null,
function * (arg) { called = true }))
.to.be.rejectedWith(cli.exit.ErrorExit, /found no kafka add-ons on my-app/)
.then(() => { expect(called).to.be.false })
})

it('warns and exits if multiple add-ons are found', () => {
fetchAll = () => Promise.resolve([ { name: 'kafka-1' }, { name: 'kafka-2' } ])
let called = false
return expect(co.wrap(clusters.withCluster)(heroku,
'my-app', null,
function * (arg) { called = true }))
.to.be.rejectedWith(cli.exit.ErrorExit, /found more than one kafka add-on on my-app: kafka-1, kafka-2/)
.then(() => { expect(called).to.be.false })
})

it('invokes the callback with the returned add-on', () => {
let addon = { name: 'kafka-1' }
fetchAll = () => Promise.resolve([ addon ])
let calledWith = addon
return expect(co.wrap(clusters.withCluster)(heroku,
'my-app', null,
function * (arg) { calledWith = arg }))
.to.be.fulfilled
.then(() => { expect(calledWith).to.equal(addon) })
})
})
})

0 comments on commit 1b0b3f5

Please sign in to comment.