Skip to content

Commit 07357b2

Browse files
authored
Update custom-transform.spec.ts
1 parent 4a393d2 commit 07357b2

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/functional/custom-transform.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,63 @@ describe("custom transformation decorator", () => {
157157
objArg.should.be.equal(user);
158158
typeArg.should.be.equal(TransformationType.CLASS_TO_PLAIN);
159159
});
160+
161+
let model: any;
162+
it ('should serialize json into model instance of class Person', () => {
163+
expect(() => {
164+
const json = {
165+
name: 'John Doe',
166+
address: {
167+
street: 'Main Street 25',
168+
tel: '5454-534-645',
169+
zip: 10353,
170+
country: 'West Samoa'
171+
},
172+
age: 25,
173+
hobbies: [
174+
{ type: 'sport', name: 'sailing' },
175+
{ type: 'relax', name: 'reading' },
176+
{ type: 'sport', name: 'jogging' },
177+
{ type: 'relax', name: 'movies' }
178+
]
179+
};
180+
class Hobby {
181+
public type: string;
182+
public name: string;
183+
}
184+
class Address {
185+
public street: string;
186+
187+
@Expose({ name: 'tel' })
188+
public telephone: string;
189+
190+
public zip: number;
191+
192+
public country: string;
193+
}
194+
class Person {
195+
public name: string;
196+
197+
@Type(() => Address)
198+
public address: Address;
199+
200+
@Type(() => Hobby)
201+
@Transform(value => value.filter(hobby => hobby.type === 'sport'), { toClassOnly: true })
202+
public hobbies: Hobby[];
203+
204+
public age: number;
205+
}
206+
model = plainToClass(Person, json);
207+
expect(model instanceof Person);
208+
expect(model.address instanceof Address);
209+
model.hobbies.forEach(hobby => expect(hobby instanceof Hobby && hobby.type === 'sport'));
210+
}).not.toThrowError();
211+
});
212+
213+
it ('should serialize a model into json', () => {
214+
expect(() => {
215+
classToPlain(model);
216+
}).not.toThrowError();
217+
});
160218

161219
});

0 commit comments

Comments
 (0)