Add tests for .getPluginConfig

This commit is contained in:
Lee Dohm 2017-05-13 20:19:22 -07:00
parent 69cdf51547
commit 24ef94f232
No known key found for this signature in database
GPG Key ID: A27E146D71F5F6B6
4 changed files with 81 additions and 0 deletions

3
test/fixtures/config/basic.yml vendored Normal file
View File

@ -0,0 +1,3 @@
foo: 5
bar: 7
baz: 11

0
test/fixtures/config/empty.yml vendored Normal file
View File

1
test/fixtures/config/malformed.yml vendored Normal file
View File

@ -0,0 +1 @@
@

View File

@ -1,3 +1,5 @@
const fs = require('fs');
const path = require('path');
const expect = require('expect');
const Context = require('../lib/context');
const createRobot = require('../lib/robot');
@ -7,6 +9,12 @@ const nullLogger = {};
nullLogger[level] = function () { };
});
function readConfig(fileName) {
const configPath = path.join(__dirname, 'fixtures', 'config', fileName);
const content = fs.readFileSync(configPath, {encoding: 'utf8'});
return {content: Buffer.from(content).toString('base64')};
}
describe('Robot', function () {
let webhook;
let robot;
@ -63,4 +71,73 @@ describe('Robot', function () {
expect(spy).toNotHaveBeenCalled();
});
});
describe('getPluginConfig', function () {
let github;
beforeEach(function () {
github = {
repos: {
getContent: spy
}
};
});
it('gets a valid configuration', async function () {
spy.andReturn(Promise.resolve(readConfig('basic.yml')));
const config = await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml');
expect(spy).toHaveBeenCalled();
expect(spy.calls[0].arguments[0]).toEqual({
owner: 'owner',
repo: 'repo',
path: '.github/test-file.yml'
});
expect(config).toEqual({
foo: 5,
bar: 7,
baz: 11
});
});
it('throws when the file is missing', async function () {
spy.andReturn(Promise.reject(new Error('An error occurred')));
let e;
let contents;
try {
await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml');
} catch (err) {
e = err;
}
expect(contents).toNotExist();
expect(e).toExist();
expect(e.message).toEqual('An error occurred');
});
it('throws when the configuration file is malformed', async function () {
spy.andReturn(Promise.resolve(readConfig('malformed.yml')));
let e;
let contents;
try {
contents = await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml');
} catch (err) {
e = err;
}
expect(contents).toNotExist();
expect(e).toExist();
expect(e.message).toMatch(/^end of the stream or a document separator/);
});
it('returns an empty object when the file is empty', async function () {
spy.andReturn(readConfig('empty.yml'));
const contents = await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml');
expect(contents).toEqual({});
});
});
});