Skip to content

Commit

Permalink
tests(@nestjs/core) fix failing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 21, 2018
1 parent c9184ac commit 4cfda07
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 78 deletions.
16 changes: 10 additions & 6 deletions packages/core/middleware/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class MiddlewareBuilder implements MiddlewareConsumer {
this.includedRoutes = filterMiddleware(middleware);
}

public getExcludedRoutes(): RouteInfo[] {
return this.excludedRoutes;
}

public with(...args): MiddlewareConfigProxy {
this.contextParameters = args;
return this;
Expand Down Expand Up @@ -85,13 +89,13 @@ export class MiddlewareBuilder implements MiddlewareConsumer {
}

private isRouteExcluded(routeInfo: RouteInfo): boolean {
return this.excludedRoutes.some(excluded => {
const pathLastIndex = routeInfo.path.length - 1;
const validatedRoutePath =
routeInfo.path[pathLastIndex] === '/'
? routeInfo.path.slice(0, pathLastIndex)
: routeInfo.path;
const pathLastIndex = routeInfo.path.length - 1;
const validatedRoutePath =
routeInfo.path[pathLastIndex] === '/'
? routeInfo.path.slice(0, pathLastIndex)
: routeInfo.path;

return this.excludedRoutes.some(excluded => {
const isPathEqual = validatedRoutePath === excluded.path;
if (!isPathEqual) {
return false;
Expand Down
131 changes: 131 additions & 0 deletions packages/core/test/middleware/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { RequestMethod } from '@nestjs/common';
import { expect } from 'chai';
import { Controller, Get } from '../../../common';
import { NestContainer } from '../../injector/container';
import { MiddlewareBuilder } from '../../middleware/builder';
import { RoutesMapper } from '../../middleware/routes-mapper';

describe('MiddlewareBuilder', () => {
let builder: MiddlewareBuilder;

beforeEach(() => {
builder = new MiddlewareBuilder(new RoutesMapper(new NestContainer()));
});
describe('apply', () => {
let configProxy;
beforeEach(() => {
configProxy = builder.apply([]);
});
it('should return configuration proxy', () => {
const metatype = (MiddlewareBuilder as any).ConfigProxy;
expect(configProxy instanceof metatype).to.be.true;
});
describe('configuration proxy', () => {
it('should returns itself on "with()" call', () => {
expect(configProxy.with()).to.be.eq(configProxy);
});
describe('when "forRoutes()" called', () => {
@Controller('path')
class Test {
@Get('route')
public getAll() {}
}
const route = { path: '/test', method: 0 };
it('should store configuration passed as argument', () => {
configProxy.forRoutes(route, Test);

expect(builder.build()).to.deep.equal([
{
middleware: [],
forRoutes: [
{
method: 0,
path: route.path,
},
{
method: 0,
path: '/path/route',
},
],
},
]);
});
});
});
});

describe('exclude', () => {
it('should map string to RouteInfo', () => {
const path = '/test';
const proxy: any = builder.apply().exclude(path);

expect(proxy.getExcludedRoutes()).to.be.eql([
{
path,
method: RequestMethod.ALL,
},
]);
});
});

describe('isRouteExcluded', () => {
const routeInfo = { path: '/test', method: RequestMethod.POST };
let proxy: any;

beforeEach(() => {
proxy = builder.apply();
});
describe('when path is equal', () => {
describe('when method is ALL', () => {
it('should return true', () => {
proxy.exclude(routeInfo.path);

expect(proxy.isRouteExcluded(routeInfo)).to.be.true;
});
});
describe('when method is equal', () => {
it('should return true', () => {
proxy.exclude({
path: routeInfo.path,
method: RequestMethod.POST,
});

expect(proxy.isRouteExcluded(routeInfo)).to.be.true;
});
});
describe('when path has / at the end', () => {
it('should return true', () => {
proxy.exclude({
path: 'test',
method: RequestMethod.POST,
});

expect(proxy.isRouteExcluded({
...routeInfo,
path: '/test/',
})).to.be.true;
});
});
describe('when method is not equal', () => {
it('should return false', () => {
proxy.exclude({
path: routeInfo.path,
method: RequestMethod.GET,
});

expect(proxy.isRouteExcluded(routeInfo)).to.be.false;
});
});
});
describe('when path is not equal', () => {
it('should return false', () => {
proxy.exclude({
path: 'testx',
method: RequestMethod.POST,
});

expect(proxy.isRouteExcluded(routeInfo)).to.be.false;
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ describe('MiddlewareModule', () => {
).to.be.rejectedWith(InvalidMiddlewareException);
});

it('should store middleware when middleware is stored in container', () => {
it('should mount middleware when is stored in container', () => {
const route = 'testPath';
const configuration = {
middleware: [TestMiddleware],
forRoutes: ['test', AnotherRoute, TestRoute],
};

const useSpy = sinon.spy();
const createMiddlewareFactorySpy = sinon.spy();
const app = {
use: useSpy,
createMiddlewareFactory: createMiddlewareFactorySpy,
};
const container = new MiddlewareContainer();
const moduleKey = 'Test' as any;
Expand All @@ -149,12 +149,12 @@ describe('MiddlewareModule', () => {

middlewareModule.registerRouteMiddleware(
container,
route,
{ path: route, method: RequestMethod.ALL },
configuration,
moduleKey,
app as any,
);
expect(useSpy.calledOnce).to.be.true;
expect(createMiddlewareFactorySpy.calledOnce).to.be.true;
});
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { expect } from 'chai';
import { RoutesMapper } from '../../middleware/routes-mapper';
import { Controller } from '../../../common/decorators/core/controller.decorator';
import { RequestMapping } from '../../../common/decorators/http/request-mapping.decorator';
import { RequestMethod } from '../../../common/enums/request-method.enum';
import { UnknownRequestMappingException } from '../../errors/exceptions/unknown-request-mapping.exception';
import { NestContainer } from '../../injector/container';
import { RoutesMapper } from '../../middleware/routes-mapper';

describe('RoutesMapper', () => {
@Controller('test')
Expand All @@ -27,22 +26,12 @@ describe('RoutesMapper', () => {
forRoutes: [{ path: 'test', method: RequestMethod.GET }, TestRoute],
};

expect(mapper.mapRouteToRouteProps(config.forRoutes[0])).to.deep.equal([
'/test',
expect(mapper.mapRouteToRouteInfo(config.forRoutes[0])).to.deep.equal([
{ path: '/test', method: RequestMethod.GET },
]);
expect(mapper.mapRouteToRouteProps(config.forRoutes[1])).to.deep.equal([
'/test/test',
'/test/another',
expect(mapper.mapRouteToRouteInfo(config.forRoutes[1])).to.deep.equal([
{ path: '/test/test', method: RequestMethod.GET },
{ path: '/test/another', method: RequestMethod.DELETE },
]);
});

it('should throw exception when invalid object was passed as route', () => {
const config = {
middleware: 'Test',
forRoutes: [{ method: RequestMethod.GET }],
};
expect(
mapper.mapRouteToRouteProps.bind(mapper, config.forRoutes[0]),
).throws(UnknownRequestMappingException);
});
});
File renamed without changes.
50 changes: 0 additions & 50 deletions packages/core/test/middlewares/builder.spec.ts

This file was deleted.

0 comments on commit 4cfda07

Please sign in to comment.