Skip to content

Commit

Permalink
feature(3.2.0): add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelyali committed Mar 26, 2019
1 parent 7ce69f6 commit a7d59a5
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 25 deletions.
8 changes: 5 additions & 3 deletions dist/classes/restful-service.class.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/classes/restful-service.class.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions dist/decorators/crud.decorator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/decorators/crud.decorator.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/decorators/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export declare function setSwaggerOkResponseMeta(meta: any, func: Function): voi
export declare function setSwaggerOperationMeta(meta: any, func: Function): void;
export declare function setSwaggerParamsMeta(meta: any, func: Function): void;
export declare function setSwaggerOkResponse(func: Function, dto: any, isArray?: boolean): void;
export declare function setSwaggerOperation(func: Function, summary?: string): void;
export declare function setSwaggerOperation(func: Function, summary: string): void;
export declare function setSwaggerParams(func: Function, crudOptions: CrudOptions): void;
export declare function setSwaggerQueryGetOne(func: Function): void;
export declare function setSwaggerQueryGetMany(func: Function, name: string): void;
Expand Down
5 changes: 3 additions & 2 deletions dist/decorators/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/decorators/helpers.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestjsx/crud",
"version": "3.2.0-beta.2",
"version": "3.2.0-beta.1",
"description": "NestJs CRUD for RESTful APIs",
"main": "index.js",
"types": "index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion dist/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/utils.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions src/classes/restful-service.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { GetManyDefaultResponse, RequestParamsParsed, RestfulOptions } from '../
export abstract class RestfulService<T> {
protected abstract options: RestfulOptions;

constructor() {
}
constructor() {}

/**
* Wrap page into page-info
Expand All @@ -16,13 +15,22 @@ export abstract class RestfulService<T> {
* @param limit
* @param offset
*/
public createPageInfo(data: T[], total: number, limit: number, offset: number): GetManyDefaultResponse<T> {
public createPageInfo(
data: T[],
total: number,
limit: number,
offset: number,
): GetManyDefaultResponse<T> {
return {
data,
count: data.length,
total,
page: Math.floor(offset / limit) + 1,
pageCount: limit && total ? Math.round(total / limit) : undefined,
pageCount:
limit && total
? Math.round(total / limit)
: /* istanbul ignore next line */
undefined,
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/decorators/crud.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ const baseRoutesInit = {
@Type((t) => dto)
bulk: any[];
}
/* istanbul ignore next line */
const BultDtoType = hasValidator ? BulkDto : {};

setRouteArgs(
{
Expand All @@ -195,7 +197,7 @@ const baseRoutesInit = {
target,
name,
);
setParamTypes([Array, hasValidator ? BulkDto : {}], prototype, name);
setParamTypes([Array, BultDtoType], prototype, name);
setInterceptors(
[RestfulParamsInterceptor, ...getRouteInterceptors(crudOptions.routes.createManyBase)],
prototype[name],
Expand Down Expand Up @@ -349,7 +351,7 @@ export const Crud = (dto: any, crudOptions: CrudOptions = {}) => (target: object
if (overrided && route && route.enable) {
// get base function metadata
const interceptors = getInterceptors(prototype[name]) || [];
const baseInterceptors = getInterceptors(prototype[overrided]) || [];
const baseInterceptors = getInterceptors(prototype[overrided]);
const baseAction = getAction(prototype[overrided]);
const baseSwaggerParams = getSwaggerParams(prototype[overrided]);
const baseSwaggerOkResponse = getSwaggeOkResponse(prototype[overrided]);
Expand Down
9 changes: 7 additions & 2 deletions src/decorators/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function setSwaggerOkResponse(func: Function, dto: any, isArray?: boolean
}
}

export function setSwaggerOperation(func: Function, summary: string = '') {
export function setSwaggerOperation(func: Function, summary: string) {
if (swagger) {
/* istanbul ignore next line */
const metadata = getSwaggerOperation(func);
Expand Down Expand Up @@ -231,6 +231,7 @@ export function setSwaggerQueryGetMany(func: Function, name: string) {
export function createParamMetadata(
paramtype: RouteParamtypes,
index: number,
/* istanbul ignore next line */
pipes: any[] = [],
data = undefined,
): any {
Expand Down Expand Up @@ -276,10 +277,12 @@ export function getParsedBody(func: Function) {
}

export function getParamTypes(prototype: any, name: string) {
/* istanbul ignore next line */
return Reflect.getMetadata(PARAMTYPES_METADATA, prototype, name) || [];
}

export function getRouteArgs(target: object, name: string) {
/* istanbul ignore next line */
return Reflect.getMetadata(ROUTE_ARGS_METADATA, target, name) || {};
}

Expand Down Expand Up @@ -321,7 +324,8 @@ export function setValidationPipe(crudOptions: CrudOptions, group: CrudValidate)
groups: [group],
transform: false,
})
: undefined;
: /* istanbul ignore next line */
undefined;
}

export function enableRoute(name: BaseRouteName, crudOptions: CrudOptions) {
Expand Down Expand Up @@ -425,6 +429,7 @@ export function overrideParsedBody(target: any, baseName: BaseRouteName, name: s
const paramTypes = getParamTypes(prototype, name) as any[];
const metatype = paramTypes[parsedBody.index];
const types = [String, Boolean, Number, Array, Object];
/* istanbul ignore next line */
const toCopy = types.some((t) => metatype === t) || isNil(metatype);

if (toCopy) {
Expand Down
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ try {
swaggerPkg = require('@nestjs/swagger/dist/constants');
} catch (error) {}

/* istanbul ignore next line */
export const swagger = swaggerPkg ? swaggerPkg : null;
export const hasValidator = !!classValidatorPkg;
export const hasTypeorm = !!typeormPkg;
Expand All @@ -28,4 +29,5 @@ export const mockValidatorDecorator = (name: string) =>
export const mockTransformerDecorator = (name: string) =>
classTransformerPkg && classTransformerPkg[name]
? classTransformerPkg[name]
: (...args: any[]) => (target, key) => {};
: /* istanbul ignore next line */
(...args: any[]) => (target, key) => {};
20 changes: 18 additions & 2 deletions test/typeorm/custom-route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Custom2Controller {
}
}

@Crud(Company, {})
@Crud(Company)
@Controller('custom1')
class Custom1Controller {
constructor(public service: CompaniesService) {}
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('Custom routes with @UsePathInterceptors()', () => {
});
});

describe('test params validation', () => {
describe('params validation', () => {
it('should return status 400', () => {
return request(server)
.get('/custom2/invalid')
Expand All @@ -219,4 +219,20 @@ describe('Custom routes with @UsePathInterceptors()', () => {
.expect(500);
});
});

describe('decidePagination', () => {
it('should return status 200', () => {
return request(server)
.get('/custom1')
.query({ page: 1, limit: 3 })
.expect(200)
.expect((res) => {
expect(res.body).toHaveProperty('data');
expect(res.body).toHaveProperty('count');
expect(res.body).toHaveProperty('total');
expect(res.body).toHaveProperty('page');
expect(res.body).toHaveProperty('pageCount');
});
});
});
});

0 comments on commit a7d59a5

Please sign in to comment.