|
1 | 1 | import "reflect-metadata";
|
2 | 2 | import {defaultMetadataStorage} from "../../src/storage";
|
3 |
| -import {Exclude, Expose, TransformClassToPlain, TransformClassToClass} from "../../src/decorators"; |
| 3 | +import { Exclude, Expose, TransformClassToPlain, TransformClassToClass, TransformPlainToClass } from "../../src/decorators"; |
4 | 4 | import {expect} from "chai";
|
5 | 5 |
|
6 | 6 | describe("transformer methods decorator", () => {
|
@@ -50,6 +50,49 @@ describe("transformer methods decorator", () => {
|
50 | 50 | expect(result).to.be.instanceof(User);
|
51 | 51 | });
|
52 | 52 |
|
| 53 | + it("should expose non configuration properties and return User instance class instead of plain object", () => { |
| 54 | + defaultMetadataStorage.clear(); |
| 55 | + |
| 56 | + @Exclude() |
| 57 | + class User { |
| 58 | + |
| 59 | + id: number; |
| 60 | + |
| 61 | + @Expose() |
| 62 | + firstName: string; |
| 63 | + |
| 64 | + @Expose() |
| 65 | + lastName: string; |
| 66 | + |
| 67 | + password: string; |
| 68 | + } |
| 69 | + |
| 70 | + class UserController { |
| 71 | + |
| 72 | + @TransformPlainToClass(User) |
| 73 | + getUser() { |
| 74 | + const user: any = {}; |
| 75 | + user.firstName = "Snir"; |
| 76 | + user.lastName = "Segal"; |
| 77 | + user.password = "imnosuperman"; |
| 78 | + |
| 79 | + return user; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const controller = new UserController(); |
| 84 | + |
| 85 | + const result = controller.getUser(); |
| 86 | + expect(result.password).to.be.undefined; |
| 87 | + |
| 88 | + const user = new User(); |
| 89 | + user.firstName = "Snir"; |
| 90 | + user.lastName = "Segal"; |
| 91 | + |
| 92 | + expect(result).to.be.eql(user); |
| 93 | + expect(result).to.be.instanceof(User); |
| 94 | + }); |
| 95 | + |
53 | 96 | it("should expose non configuration properties", () => {
|
54 | 97 | defaultMetadataStorage.clear();
|
55 | 98 |
|
|
0 commit comments