Skip to content

Commit 84d1bec

Browse files
cov(@nestjs/core) increase test coverage
1 parent 513586e commit 84d1bec

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

src/core/router/router-execution-context.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,20 @@ export class RouterExecutionContext {
105105

106106
if (key.includes(CUSTOM_ROUTE_AGRS_METADATA)) {
107107
const { factory } = metadata[key];
108-
const customExtractValue = !isUndefined(factory) && isFunction(factory)
109-
? (req, res, next) => factory(data, req)
110-
: () => ({});
111-
108+
const customExtractValue = this.getCustomFactory(factory, data);
112109
return { index, extractValue: customExtractValue, type, data, pipes };
113110
}
114111
const extractValue = (req, res, next) => this.paramsFactory.exchangeKeyForValue(type, data, { req, res, next });
115112
return { index, extractValue, type, data, pipes };
116113
});
117114
}
118115

116+
public getCustomFactory(factory: (...args) => void, data): (...args) => any {
117+
return !isUndefined(factory) && isFunction(factory)
118+
? (req, res, next) => factory(data, req)
119+
: () => null;
120+
}
121+
119122
public mergeParamsMetatypes(
120123
paramsProperties: ParamProperties[],
121124
paramtypes: any[],

src/core/test/router/router-execution-context.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('RouterExecutionContext', () => {
2222
let bindSpy: sinon.SinonSpy;
2323
let factory: RouteParamsFactory;
2424
let consumer: PipesConsumer;
25+
let guardsConsumer: GuardsConsumer;
2526

2627
beforeEach(() => {
2728
callback = {
@@ -33,10 +34,11 @@ describe('RouterExecutionContext', () => {
3334

3435
factory = new RouteParamsFactory();
3536
consumer = new PipesConsumer();
37+
guardsConsumer = new GuardsConsumer();
3638

3739
contextCreator = new RouterExecutionContext(
3840
factory, new PipesContextCreator(new ApplicationConfig()), consumer,
39-
new GuardsContextCreator(new NestContainer()), new GuardsConsumer(),
41+
new GuardsContextCreator(new NestContainer()), guardsConsumer,
4042
new InterceptorsContextCreator(new NestContainer()), new InterceptorsConsumer(),
4143
);
4244
});
@@ -101,6 +103,12 @@ describe('RouterExecutionContext', () => {
101103
done();
102104
});
103105
});
106+
it('should throw exception when "tryActivate" returns false', () => {
107+
sinon.stub(guardsConsumer, 'tryActivate', () => false);
108+
expect(
109+
proxyContext(request, response, next),
110+
).to.eventually.throw();
111+
});
104112
});
105113
});
106114
});
@@ -186,6 +194,30 @@ describe('RouterExecutionContext', () => {
186194
expect(values[1]).to.deep.include(expectedValues[1]);
187195
});
188196
});
197+
describe('getCustomFactory', () => {
198+
describe('when factory is function', () => {
199+
it('should return curried factory', () => {
200+
const data = 3;
201+
const result = 10;
202+
const customFactory = (_, req) => result;
203+
204+
expect(contextCreator.getCustomFactory(customFactory, data)()).to.be.eql(result);
205+
});
206+
});
207+
describe('when factory is undefined / is not a function', () => {
208+
it('should return curried null identity', () => {
209+
const result = 10;
210+
const customFactory = undefined;
211+
expect(contextCreator.getCustomFactory(customFactory, undefined)()).to.be.eql(null);
212+
});
213+
});
214+
});
215+
describe('mergeParamsMetatypes', () => {
216+
it('should return "paramsProperties" when paramtypes array doesnt exists', () => {
217+
const paramsProperties = ['1'];
218+
expect(contextCreator.mergeParamsMetatypes(paramsProperties as any, null)).to.be.eql(paramsProperties);
219+
});
220+
});
189221
describe('getParamValue', () => {
190222
let consumerApplySpy: sinon.SinonSpy;
191223
const value = 3, metatype = null, transforms = [];

0 commit comments

Comments
 (0)