1
0

Updates to @Strong()

This commit is contained in:
2021-02-10 14:50:45 -06:00
parent 0ce0e943f5
commit 00dc4b1cdc
3 changed files with 104 additions and 8 deletions

View File

@@ -0,0 +1,67 @@
import 'reflect-metadata';
import { Serde } from "src/serde";
import { Strong } from 'src/decorators';
class StrongTestModel extends Serde<StrongTestModel> {
name: string;
date: Date;
@Strong('string') description: string;
}
class TestModel extends Serde<TestModel> {
key: string;
value: string;
}
class NestedStrongTestModel extends Serde<NestedStrongTestModel> {
name: string;
@Strong(TestModel) nested: TestModel;
}
describe('@Strong() Decorator', () => {
describe('simple strong model', () => {
it('should set value nicely', () => {
const testData: any = {
name: 'test model',
description: 'this is a test model',
}
expect(() => {
new StrongTestModel().deserialize(testData)
}).not.toThrow();
expect(new StrongTestModel().deserialize(testData).description).toBe('this is a test model');
});
it('should throw', () => {
const testData: any = {
name: 'test model',
description: 1234,
}
expect(() => {
new StrongTestModel().deserialize(testData)
}).toThrow('Strong Type mismatch');
});
});
describe('nested strong model', () => {
it('should set value nicely', () => {
const testData: any = {
name: 'test model',
nested: new TestModel().deserialize({ key: 'foo', value: 'bar' })
}
expect(() => {
new NestedStrongTestModel().deserialize(testData)
}).not.toThrow();
expect(new NestedStrongTestModel().deserialize(testData).name).toBe('test model');
});
it('should throw', () => {
const testData: any = {
name: 'test model',
nested: false
}
expect(() => {
new StrongTestModel().deserialize(testData)
}).toThrow('Strong Type mismatch');
});
})
});