Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) }) | ||
}) | ||
}) | ||
}) |