Add tests for BaseService + fix hex colors (#1581)

This commit is contained in:
Paul Melnikow
2018-03-20 15:04:55 -07:00
committed by GitHub
parent e37668b392
commit 71ef474afc
2 changed files with 86 additions and 16 deletions

View File

@@ -87,6 +87,15 @@ module.exports = class BaseService {
return result;
}
async invokeHandler(namedParams) {
try {
return await this.handle(namedParams);
} catch (error) {
console.log(error);
return { message: 'error' };
}
}
static _makeBadgeData(overrides, serviceData) {
const {
style,
@@ -122,7 +131,7 @@ module.exports = class BaseService {
links: toArray(overrideLink || serviceLink),
colorA: makeColor(overrideColorA),
};
const color = makeColor(overrideColorB || serviceColor || defaultColor || 'lightgrey');
const color = overrideColorB || serviceColor || defaultColor || 'lightgrey';
setBadgeColor(badgeData, color);
return badgeData;
@@ -133,24 +142,15 @@ module.exports = class BaseService {
camp.route(this._regex,
handleRequest(async (queryParams, match, sendBadge, request) => {
let serviceData;
try {
const namedParams = this._namedParamsForMatch(match);
const serviceInstance = new serviceClass({
sendAndCacheRequest: request.asPromise,
});
serviceData = await serviceInstance.handle(namedParams);
} catch (error) {
serviceData = { message: 'error' };
console.log(error);
}
const namedParams = this._namedParamsForMatch(match);
const serviceInstance = new serviceClass({
sendAndCacheRequest: request.asPromise,
});
const serviceData = await serviceInstance.invokeHandler(namedParams);
const badgeData = this._makeBadgeData(queryParams, serviceData);
// Assumes the final capture group is the extension
const format = match.slice(-1)[0];
const badgeData = this._makeBadgeData(queryParams, serviceData);
sendBadge(format, badgeData);
}));
}

View File

@@ -1,6 +1,7 @@
'use strict';
const { expect } = require('chai');
const { test, given, forCases } = require('sazerac');
const sinon = require('sinon');
const BaseService = require('./base');
@@ -24,12 +25,71 @@ class DummyService extends BaseService {
}
describe('BaseService', () => {
describe('URL pattern matching', function () {
const regexExec = str => DummyService._regex.exec(str);
const getSomeArg = str => {
const [, someArg] = regexExec(str);
return someArg;
};
const namedParams = str => {
const match = regexExec(str);
return DummyService._namedParamsForMatch(match);
};
test(regexExec, () => {
forCases([
given('/foo/bar.bar.bar.zip'),
given('/foo/bar/bar.svg'),
]).expect(null);
});
test(getSomeArg, () => {
forCases([
given('/foo/bar.bar.bar.svg'),
given('/foo/bar.bar.bar.png'),
given('/foo/bar.bar.bar.gif'),
given('/foo/bar.bar.bar.jpg'),
given('/foo/bar.bar.bar.json'),
]).expect('bar.bar.bar');
});
test(namedParams, () => {
forCases([
given('/foo/bar.bar.bar.svg'),
given('/foo/bar.bar.bar.png'),
given('/foo/bar.bar.bar.gif'),
given('/foo/bar.bar.bar.jpg'),
given('/foo/bar.bar.bar.json'),
]).expect({ someArg: 'bar.bar.bar' });
});
});
it('Invokes the handler as expected', async function () {
const serviceInstance = new DummyService({});
const serviceData = await serviceInstance.invokeHandler({ someArg: 'bar.bar.bar' });
expect(serviceData).to.deep.equal({ message: 'Hello bar.bar.bar' });
});
describe('Error handling', function () {
it('Handles internal errors', async function () {
const serviceInstance = new DummyService({});
serviceInstance.handle = () => { throw Error("I've made a huge mistake"); };
const serviceData = await serviceInstance.invokeHandler({ someArg: 'bar.bar.bar' });
expect(serviceData).to.deep.equal({ message: 'error' });
});
});
describe('_makeBadgeData', function () {
describe('Overrides', function () {
it('overrides the label', function () {
const badgeData = DummyService._makeBadgeData({ label: 'purr count' }, { label: 'purrs' });
expect(badgeData.text).to.deep.equal(['purr count', 'n/a']);
});
it('overrides the color', function () {
const badgeData = DummyService._makeBadgeData({ colorB: '10ADED' }, { color: 'red' });
expect(badgeData.colorB).to.equal('#10ADED');
});
});
describe('Service data', function () {
@@ -37,6 +97,11 @@ describe('BaseService', () => {
const badgeData = DummyService._makeBadgeData({}, { message: '10k' });
expect(badgeData.text).to.deep.equal(['cat', '10k']);
});
it('applies the service color', function () {
const badgeData = DummyService._makeBadgeData({}, { color: 'red' });
expect(badgeData.colorscheme).to.equal('red');
});
});
describe('Defaults', function () {
@@ -44,6 +109,11 @@ describe('BaseService', () => {
const badgeData = DummyService._makeBadgeData({}, {});
expect(badgeData.text).to.deep.equal(['cat', 'n/a']);
});
it('uses the default color', function () {
const badgeData = DummyService._makeBadgeData({}, {});
expect(badgeData.colorscheme).to.equal('lightgrey');
});
});
});