From 71ef474afc8ca7ecbf4a2a6e699fdf024f21dad0 Mon Sep 17 00:00:00 2001 From: Paul Melnikow Date: Tue, 20 Mar 2018 15:04:55 -0700 Subject: [PATCH] Add tests for BaseService + fix hex colors (#1581) --- services/base.js | 32 ++++++++++---------- services/base.spec.js | 70 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 16 deletions(-) diff --git a/services/base.js b/services/base.js index 43531c11a7..d60432fc1b 100644 --- a/services/base.js +++ b/services/base.js @@ -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); })); } diff --git a/services/base.spec.js b/services/base.spec.js index 418622f52f..fc5579afd9 100644 --- a/services/base.spec.js +++ b/services/base.spec.js @@ -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'); + }); }); });