Files
nest/packages/core/test/router/router-proxy.spec.ts
2018-12-01 21:02:36 +01:00

83 lines
2.6 KiB
TypeScript

import { expect } from 'chai';
import * as sinon from 'sinon';
import { HttpException } from '../../../common/exceptions/http.exception';
import { ExceptionsHandler } from '../../exceptions/exceptions-handler';
import { RouterProxy } from '../../router/router-proxy';
import { NoopHttpAdapter } from '../utils/noop-adapter';
describe('RouterProxy', () => {
let routerProxy: RouterProxy;
let handlerMock: sinon.SinonMock;
let handler: ExceptionsHandler;
beforeEach(() => {
handler = new ExceptionsHandler(new NoopHttpAdapter({}));
handlerMock = sinon.mock(handler);
routerProxy = new RouterProxy();
});
describe('createProxy', () => {
it('should method return thunk', () => {
const proxy = routerProxy.createProxy(() => {}, handler);
expect(typeof proxy === 'function').to.be.true;
});
it('should method encapsulate callback passed as argument', () => {
const expectation = handlerMock.expects('next').once();
const proxy = routerProxy.createProxy((req, res, next) => {
throw new HttpException('test', 500);
}, handler);
proxy(null, null, null);
expectation.verify();
});
it('should method encapsulate async callback passed as argument', done => {
const expectation = handlerMock.expects('next').once();
const proxy = routerProxy.createProxy(async (req, res, next) => {
throw new HttpException('test', 500);
}, handler);
proxy(null, null, null);
setTimeout(() => {
expectation.verify();
done();
}, 0);
});
});
describe('createExceptionLayerProxy', () => {
it('should method return thunk', () => {
const proxy = routerProxy.createExceptionLayerProxy(() => {}, handler);
expect(typeof proxy === 'function').to.be.true;
});
it('should method encapsulate callback passed as argument', () => {
const expectation = handlerMock.expects('next').once();
const proxy = routerProxy.createExceptionLayerProxy(
(err, req, res, next) => {
throw new HttpException('test', 500);
},
handler,
);
proxy(null, null, null, null);
expectation.verify();
});
it('should method encapsulate async callback passed as argument', done => {
const expectation = handlerMock.expects('next').once();
const proxy = routerProxy.createExceptionLayerProxy(
async (err, req, res, next) => {
throw new HttpException('test', 500);
},
handler,
);
proxy(null, null, null, null);
setTimeout(() => {
expectation.verify();
done();
}, 0);
});
});
});