Skip to content

Commit

Permalink
fix(tests): cover routes in debug, metrics endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Oct 9, 2019
1 parent 2a62978 commit 815d1d6
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
19 changes: 18 additions & 1 deletion test/endpoint/TestDebugEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import { NextFunction, Request, Response, Router } from 'express';
import { ineeda } from 'ineeda';
import passport from 'passport';
import { spy } from 'sinon';
import { match, spy } from 'sinon';
import { Repository } from 'typeorm';

import { Bot } from '../../src/Bot';
Expand Down Expand Up @@ -39,6 +39,14 @@ async function createEndpoint(botReady: boolean, storageReady: boolean): Promise
});
}

function createRequest() {
const json = spy();
const response = ineeda<Response>({
json,
});
return { json, response };
}

// tslint:disable:no-identical-functions
describeLeaks('debug endpoint', async () => {
itLeaks('should have paths', async () => {
Expand Down Expand Up @@ -66,4 +74,13 @@ describeLeaks('debug endpoint', async () => {
expect(result).to.equal(router, 'must return the passed router');
expect(get).to.have.callCount(1);
});

describeLeaks('index route', async () => {
itLeaks('should return services', async () => {
const endpoint = await createEndpoint(true, true);
const { json, response } = createRequest();
await endpoint.getIndex(ineeda<Request>({}), response);
expect(json).to.have.been.calledOnce.and.calledWithMatch(match.array);
});
});
});
87 changes: 87 additions & 0 deletions test/endpoint/TestMetricsEndpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { expect } from 'chai';
import { Request, Response, Router } from 'express';
import { ineeda } from 'ineeda';
import passport from 'passport';
import { match, spy } from 'sinon';
import { Repository } from 'typeorm';

import { Bot } from '../../src/Bot';
import { INJECT_BOT, INJECT_STORAGE } from '../../src/BotService';
import { MetricsEndpoint } from '../../src/endpoint/MetricsEndpoint';
import { BotModule } from '../../src/module/BotModule';
import { Storage } from '../../src/storage';
import { describeLeaks, itLeaks } from '../helpers/async';
import { createService, createServiceContainer } from '../helpers/container';
import { getTestLogger } from '../helpers/logger';

async function createEndpoint(botReady: boolean, storageReady: boolean): Promise<MetricsEndpoint> {
const storage = ineeda<Storage>({
getRepository() {
return ineeda<Repository<{}>>();
},
});
const bot = ineeda<Bot>({
getStorage() {
return storage;
},
});

const { container, module } = await createServiceContainer(new BotModule({
logger: getTestLogger(),
}));
module.bind(INJECT_BOT).toInstance(bot);
module.bind(INJECT_STORAGE).toInstance(storage);

return createService(container, MetricsEndpoint, {
data: {
filters: [],
strict: false,
},
metadata: {
kind: 'metrics-endpoint',
name: 'test-endpoint',
},
});
}

function createRequest() {
const end = spy();
const set = spy();
const response = ineeda<Response>({
end,
set,
});
return { end, response, set };
}

// tslint:disable:no-identical-functions
describeLeaks('metrics endpoint', async () => {
itLeaks('should have paths', async () => {
const endpoint = await createEndpoint(false, false);
expect(endpoint.paths.length).to.equal(3);
expect(endpoint.paths).to.include('/metrics');
});

itLeaks('should configure a router', async () => {
const endpoint = await createEndpoint(false, false);
const get = spy();
const router = ineeda<Router>({
get,
});
const result = await endpoint.createRouter({
passport: ineeda<passport.Authenticator>(),
router,
});
expect(result).to.equal(router, 'must return the passed router');
expect(get).to.have.callCount(1);
});

describeLeaks('index route', async () => {
itLeaks('should return metrics', async () => {
const endpoint = await createEndpoint(true, true);
const { end, response } = createRequest();
await endpoint.getIndex(ineeda<Request>({}), response);
expect(end).to.have.been.calledOnce.and.calledWithMatch(match.string);
});
});
});

0 comments on commit 815d1d6

Please sign in to comment.