Skip to content

Commit 714341a

Browse files
committed
feat(jsonView): add test
1 parent 2592c17 commit 714341a

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/decorators.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ClassTransformer } from './ClassTransformer';
1+
import {ClassTransformer} from "./ClassTransformer";
22
import {defaultMetadataStorage} from "./storage";
33
import {TypeMetadata} from "./metadata/TypeMetadata";
44
import {ExposeMetadata} from "./metadata/ExposeMetadata";
@@ -54,17 +54,17 @@ export function Exclude(options?: ExcludeOptions) {
5454
/**
5555
* Return the object with the exposed properties only.
5656
*/
57-
export function JsonView(params: {}, method?: string): Function {
57+
export function JsonView(params?: {}, method?: string): Function {
5858

59-
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
59+
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
6060
const classTransformer: any = new ClassTransformer();
6161
const originalMethod = descriptor.value;
6262

6363
descriptor.value = function(...args: any[]) {
6464
let result: any = originalMethod.apply(this, args);
6565

6666
let transformer: Function;
67-
if (typeof method === 'string' && method) {
67+
if (typeof method === "string" && method) {
6868
transformer = classTransformer[method];
6969
}
7070
else {
@@ -73,6 +73,6 @@ export function JsonView(params: {}, method?: string): Function {
7373

7474
result = transformer(result, params);
7575
return result;
76-
}
77-
}
76+
};
77+
};
7878
}

test/functional/basic-functionality.spec.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
classToClass, classToClassFromExist
88
} from "../../src/index";
99
import {defaultMetadataStorage} from "../../src/storage";
10-
import {Exclude, Expose, Type} from "../../src/decorators";
10+
import {Exclude, Expose, Type, JsonView} from "../../src/decorators";
1111
import {expect} from "chai";
1212

1313
describe("basic functionality", () => {
@@ -1680,5 +1680,47 @@ describe("basic functionality", () => {
16801680
classToClassFromExistUser.should.be.eql([fromExistUserLike1, fromExistUserLike2]);
16811681

16821682
});
1683+
1684+
it("should expose properties with json view", () => {
1685+
defaultMetadataStorage.clear();
1686+
1687+
@Exclude()
1688+
class User {
1689+
1690+
id: number;
1691+
1692+
@Expose()
1693+
firstName: string;
1694+
1695+
@Expose()
1696+
lastName: string;
1697+
1698+
password: string;
1699+
}
1700+
1701+
const user = new User();
1702+
user.firstName = "Umed";
1703+
user.lastName = "Khudoiberdiev";
1704+
user.password = "imnosuperman";
1705+
1706+
const plainUser = {
1707+
firstName: "Umed",
1708+
lastName: "Khudoiberdiev"
1709+
};
1710+
1711+
class UserController {
1712+
1713+
@JsonView()
1714+
getUser() {
1715+
return user;
1716+
}
1717+
}
1718+
1719+
const controller = new UserController();
1720+
1721+
let result = controller.getUser();
1722+
expect(result.password).to.be.undefined;
1723+
expect(result).to.be.eql(plainUser);
1724+
});
16831725

16841726
});

0 commit comments

Comments
 (0)