-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathimplicit-type-declarations.spec.ts
144 lines (117 loc) · 3.79 KB
/
implicit-type-declarations.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import 'reflect-metadata';
import { plainToInstance } from '../../src/index';
import { defaultMetadataStorage } from '../../src/storage';
import { Expose, Type } from '../../src/decorators';
describe('implicit type conversion', () => {
it('should run only when enabled', () => {
defaultMetadataStorage.clear();
class SimpleExample {
@Expose()
readonly implicitTypeNumber: number;
@Expose()
readonly implicitTypeString: string;
}
const result1: SimpleExample = plainToInstance(
SimpleExample,
{
implicitTypeNumber: '100',
implicitTypeString: 133123,
},
{ enableImplicitConversion: true }
);
const result2: SimpleExample = plainToInstance(
SimpleExample,
{
implicitTypeNumber: '100',
implicitTypeString: 133123,
},
{ enableImplicitConversion: false }
);
expect(result1).toEqual({ implicitTypeNumber: 100, implicitTypeString: '133123' });
expect(result2).toEqual({ implicitTypeNumber: '100', implicitTypeString: 133123 });
});
});
describe('implicit and explicity type declarations', () => {
defaultMetadataStorage.clear();
class Example {
@Expose()
readonly implicitTypeViaOtherDecorator: Date;
@Type()
readonly implicitTypeViaEmptyTypeDecorator: number;
@Type(() => String)
readonly explicitType: string;
}
const result: Example = plainToInstance(
Example,
{
implicitTypeViaOtherDecorator: '2018-12-24T12:00:00Z',
implicitTypeViaEmptyTypeDecorator: '100',
explicitType: 100,
},
{ enableImplicitConversion: true }
);
it('should use implicitly defined design:type to convert value when no @Type decorator is used', () => {
expect(result.implicitTypeViaOtherDecorator).toBeInstanceOf(Date);
expect(result.implicitTypeViaOtherDecorator.getTime()).toEqual(new Date('2018-12-24T12:00:00Z').getTime());
});
it('should use implicitly defined design:type to convert value when empty @Type() decorator is used', () => {
expect(typeof result.implicitTypeViaEmptyTypeDecorator).toBe('number');
expect(result.implicitTypeViaEmptyTypeDecorator).toEqual(100);
});
it('should use explicitly defined type when @Type(() => Construtable) decorator is used', () => {
expect(typeof result.explicitType).toBe('string');
expect(result.explicitType).toEqual('100');
});
});
describe('plainToInstance transforms built-in primitive types properly', () => {
defaultMetadataStorage.clear();
class Example {
@Type()
date: Date;
@Type()
string: string;
@Type()
string2: string;
@Type()
number: number;
@Type()
number2: number;
@Type()
boolean: boolean;
@Type()
boolean2: boolean;
}
const result: Example = plainToInstance(
Example,
{
date: '2018-12-24T12:00:00Z',
string: '100',
string2: 100,
number: '100',
number2: 100,
boolean: 1,
boolean2: 0,
},
{ enableImplicitConversion: true }
);
it('should recognize and convert to Date', () => {
expect(result.date).toBeInstanceOf(Date);
expect(result.date.getTime()).toEqual(new Date('2018-12-24T12:00:00Z').getTime());
});
it('should recognize and convert to string', () => {
expect(typeof result.string).toBe('string');
expect(typeof result.string2).toBe('string');
expect(result.string).toEqual('100');
expect(result.string2).toEqual('100');
});
it('should recognize and convert to number', () => {
expect(typeof result.number).toBe('number');
expect(typeof result.number2).toBe('number');
expect(result.number).toEqual(100);
expect(result.number2).toEqual(100);
});
it('should recognize and convert to boolean', () => {
expect(result.boolean).toBeTruthy();
expect(result.boolean2).toBeFalsy();
});
});