Skip to content

Commit

Permalink
Add more tests (#86)
Browse files Browse the repository at this point in the history
* Add res.removeHeader/remove test

* Reorganize tests
  • Loading branch information
RafalWilinski authored Jul 29, 2017
1 parent ef6242d commit 6a6bf28
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 125 deletions.
16 changes: 7 additions & 9 deletions test/helpers/gather-os-metrics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ chai.should();

const gatherOsMetrics = require('../../src/helpers/gather-os-metrics');

describe('helpers', () => {
describe('gather-os-metrics', () => {
describe('when invoked', () => {
it('then ...', () => {
const span = { os: [], responses: [] };
describe('gather-os-metrics', () => {
describe('when invoked', () => {
it('then ...', () => {
const span = { os: [], responses: [] };

gatherOsMetrics({}, span);
gatherOsMetrics({}, span);

// todo: not sure what should I test, maybe the resulted span structure?
// todo: also this component has got some internal timing events?
});
// todo: not sure what should I test, maybe the resulted span structure?
// todo: also this component has got some internal timing events?
});
});
});
58 changes: 28 additions & 30 deletions test/helpers/on-headers-listener.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,40 @@ chai.should();
const onHeadersListener = require('../../src/helpers/on-headers-listener');
const defaultConfig = require('../../src/helpers/default-config');

describe('helpers', () => {
describe('on-headers-listener', () => {
describe('when invoked', () => {
const clock = sinon.useFakeTimers();
const spans = defaultConfig.spans;

before(() => {
spans.forEach((span) => {
span.responses = [];
});
describe('on-headers-listener', () => {
describe('when invoked', () => {
const clock = sinon.useFakeTimers();
const spans = defaultConfig.spans;

before(() => {
spans.forEach((span) => {
span.responses = [];
});
});

after(() => {
clock.restore();
});
after(() => {
clock.restore();
});

it('then for all spans, responses length should equal 1', () => {
onHeadersListener(404, process.hrtime(), spans);
it('then for all spans, responses length should equal 1', () => {
onHeadersListener(404, process.hrtime(), spans);

spans.forEach((span) => {
span.responses.length.should.equal(1);
});
spans.forEach((span) => {
span.responses.length.should.equal(1);
});
});

describe('when invoked after 1 second', () => {
it('then for span interval 1, responses length should equal 2', () => {
clock.tick(1000);
onHeadersListener(500, process.hrtime(), spans);

spans.forEach((span) => {
if (span.interval === 1) {
span.responses.length.should.equal(2);
} else {
span.responses.length.should.equal(1);
}
});
describe('when invoked after 1 second', () => {
it('then for span interval 1, responses length should equal 2', () => {
clock.tick(1000);
onHeadersListener(500, process.hrtime(), spans);

spans.forEach((span) => {
if (span.interval === 1) {
span.responses.length.should.equal(2);
} else {
span.responses.length.should.equal(1);
}
});
});
});
Expand Down
16 changes: 7 additions & 9 deletions test/helpers/send-metrics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ chai.should();

const sendMetrics = require('../../src/helpers/send-metrics');

describe('helpers', () => {
describe('send-metrics', () => {
describe('when invoked', () => {
it('then io.emit called', () => {
const io = { emit: sinon.stub() };
const span = { os: [], responses: [] };
describe('send-metrics', () => {
describe('when invoked', () => {
it('then io.emit called', () => {
const io = { emit: sinon.stub() };
const span = { os: [], responses: [] };

sendMetrics(io, span);
sendMetrics(io, span);

sinon.assert.calledWith(io.emit, 'esm_stats');
});
sinon.assert.calledWith(io.emit, 'esm_stats');
});
});
});
28 changes: 13 additions & 15 deletions test/helpers/socket-io-init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@ chai.should();
const socketIoInit = require('../../src/helpers/socket-io-init');
const defaultConfig = require('../../src/helpers/default-config');

describe('helpers', () => {
describe('socket-io-init', () => {
describe('when invoked', () => {
it('then all spans should have os and responses property', () => {
const spans = defaultConfig.spans;
describe('socket-io-init', () => {
describe('when invoked', () => {
it('then all spans should have os and responses property', () => {
const spans = defaultConfig.spans;

spans.forEach((span) => {
span.should.not.have.property('os');
// info: not working as if it was another test interfering
// span.should.not.have.property('responses');
});
spans.forEach((span) => {
span.should.not.have.property('os');
// info: not working as if it was another test interfering
// span.should.not.have.property('responses');
});

socketIoInit({}, defaultConfig);
socketIoInit({}, defaultConfig);

spans.forEach((span) => {
span.should.have.property('os');
span.should.have.property('responses');
});
spans.forEach((span) => {
span.should.have.property('os');
span.should.have.property('responses');
});
});
});
Expand Down
116 changes: 57 additions & 59 deletions test/helpers/validate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,77 @@ chai.should();
const defaultConfig = require('../../src/helpers/default-config');
const validate = require('../../src/helpers/validate');

describe('helpers', () => {
describe('validate', () => {
describe('when config is null or undefined', () => {
const config = validate();

it(`then title === ${defaultConfig.title}`, () => {
config.title.should.equal(defaultConfig.title);
});

it(`then path === ${defaultConfig.path}`, () => {
config.path.should.equal(defaultConfig.path);
});

it(`then spans === ${JSON.stringify(defaultConfig.spans)}`, () => {
config.spans.should.equal(defaultConfig.spans);
});

it('then port === null', () => {
chai.expect(config.port).to.be.null;
});

it('then websocket === null', () => {
chai.expect(config.websocket).to.be.null;
});
describe('validate', () => {
describe('when config is null or undefined', () => {
const config = validate();

it(`then title === ${defaultConfig.title}`, () => {
config.title.should.equal(defaultConfig.title);
});

describe('when config is invalid', () => {
const config = validate({ title: true, path: false, spans: 'not-an-array', port: 'abc', websocket: false });
it(`then path === ${defaultConfig.path}`, () => {
config.path.should.equal(defaultConfig.path);
});

it(`then title === ${defaultConfig.title}`, () => {
config.title.should.equal(defaultConfig.title);
});
it(`then spans === ${JSON.stringify(defaultConfig.spans)}`, () => {
config.spans.should.equal(defaultConfig.spans);
});

it(`then path === ${defaultConfig.path}`, () => {
config.path.should.equal(defaultConfig.path);
});
it('then port === null', () => {
chai.expect(config.port).to.be.null;
});

it(`then spans === ${JSON.stringify(defaultConfig.spans)}`, () => {
config.spans.should.equal(defaultConfig.spans);
});
it('then websocket === null', () => {
chai.expect(config.websocket).to.be.null;
});
});

it('then port === null', () => {
chai.expect(config.port).to.be.null;
});
describe('when config is invalid', () => {
const config = validate({ title: true, path: false, spans: 'not-an-array', port: 'abc', websocket: false });

it('then websocket === null', () => {
chai.expect(config.websocket).to.be.null;
});
it(`then title === ${defaultConfig.title}`, () => {
config.title.should.equal(defaultConfig.title);
});

describe('when config is valid', () => {
const customConfig = { title: 'Custom title', path: '/custom-path', spans: [{}, {}, {}], port: 9999, websocket: {} };
const config = validate(customConfig);
it(`then path === ${defaultConfig.path}`, () => {
config.path.should.equal(defaultConfig.path);
});

it(`then title === ${customConfig.title}`, () => {
config.title.should.equal(customConfig.title);
});
it(`then spans === ${JSON.stringify(defaultConfig.spans)}`, () => {
config.spans.should.equal(defaultConfig.spans);
});

it(`then path === ${customConfig.path}`, () => {
config.path.should.equal(customConfig.path);
});
it('then port === null', () => {
chai.expect(config.port).to.be.null;
});

it(`then spans === ${JSON.stringify(customConfig.spans)}`, () => {
config.spans.should.equal(customConfig.spans);
});
it('then websocket === null', () => {
chai.expect(config.websocket).to.be.null;
});
});

describe('when config is valid', () => {
const customConfig = { title: 'Custom title', path: '/custom-path', spans: [{}, {}, {}], port: 9999, websocket: {} };
const config = validate(customConfig);

it('then websocket === {}', () => {
config.websocket.should.deep.equal({});
});
it(`then title === ${customConfig.title}`, () => {
config.title.should.equal(customConfig.title);
});

it(`then path === ${customConfig.path}`, () => {
config.path.should.equal(customConfig.path);
});

it(`then spans === ${JSON.stringify(customConfig.spans)}`, () => {
config.spans.should.equal(customConfig.spans);
});

it('then websocket === {}', () => {
config.websocket.should.deep.equal({});
});

it(`then port === ${customConfig.port}`, () => {
config.port.should.equal(customConfig.port);
});
it(`then port === ${customConfig.port}`, () => {
config.port.should.equal(customConfig.port);
});
});
});
26 changes: 23 additions & 3 deletions test/middleware-wrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ chai.should();
const expresStatusMonitor = require('../src/middleware-wrapper');
const defaultConfig = require('../src/helpers/default-config');

describe('express-status-monitor', () => {
describe('middleware-wrapper', () => {
describe('when initialised', () => {
const middleware = expresStatusMonitor();

it('then it should be an instance of Function', () => {
it('then it should be an instance of a Function', () => {
middleware.should.be.an.instanceof(Function);
});

Expand All @@ -35,7 +35,27 @@ describe('express-status-monitor', () => {
sinon.assert.notCalled(res.send);
});

describe('and used as separate middlware and page handler', () => {
it('and res.removeHeader is present, then header is removed', () => {
const middlewareWithConfig = expresStatusMonitor({
iframe: true,
});
const resWithHeaders = Object.assign({}, res);
resWithHeaders.headers = {
'X-Frame-Options': 1,
};
resWithHeaders.removeHeader = sinon.stub();

middlewareWithConfig(req, resWithHeaders, next);
sinon.assert.called(resWithHeaders.removeHeader);

resWithHeaders.removeHeader = undefined;
resWithHeaders.remove = sinon.stub();

middlewareWithConfig(req, resWithHeaders, next);
sinon.assert.called(resWithHeaders.remove);
});

describe('and used as separate middleware and page handler', () => {
it('exposes a page handler', () => {
middleware.pageRoute.should.be.an.instanceof(Function);
middleware.pageRoute(req, res, next);
Expand Down

0 comments on commit 6a6bf28

Please sign in to comment.