1
+ import "reflect-metadata" ;
2
+ import {
3
+ plainToClass ,
4
+ } from "../../src/index" ;
5
+ import { defaultMetadataStorage } from "../../src/storage" ;
6
+ import { Expose , Type } from "../../src/decorators" ;
7
+ import { expect } from "chai" ;
8
+
9
+ describe ( "implicit and explicity type declarations" , ( ) => {
10
+
11
+ defaultMetadataStorage . clear ( ) ;
12
+
13
+ class Example {
14
+
15
+ @Expose ( )
16
+ readonly implicitTypeViaOtherDecorator : Date ;
17
+
18
+ @Type ( )
19
+ readonly implicitTypeViaEmptyTypeDecorator : number ;
20
+
21
+ @Type ( ( ) => String )
22
+ readonly explicitType : string ;
23
+ }
24
+
25
+ const result : Example = plainToClass ( Example , {
26
+ implicitTypeViaOtherDecorator : "2018-12-24T12:00:00Z" ,
27
+ implicitTypeViaEmptyTypeDecorator : "100" ,
28
+ explicitType : 100 ,
29
+ } ) ;
30
+
31
+ it ( "should use implicitly defined design:type to convert value when no @Type decorator is used" , ( ) => {
32
+ expect ( result . implicitTypeViaOtherDecorator ) . to . be . instanceOf ( Date ) ;
33
+ expect ( result . implicitTypeViaOtherDecorator . getTime ( ) ) . to . be . equal ( new Date ( "2018-12-24T12:00:00Z" ) . getTime ( ) ) ;
34
+ } ) ;
35
+
36
+ it ( "should use implicitly defined design:type to convert value when empty @Type() decorator is used" , ( ) => {
37
+ expect ( result . implicitTypeViaEmptyTypeDecorator ) . that . is . a ( "number" ) ;
38
+ expect ( result . implicitTypeViaEmptyTypeDecorator ) . to . be . equal ( 100 ) ;
39
+ } ) ;
40
+
41
+ it ( "should use explicitly defined type when @Type(() => Construtable) decorator is used" , ( ) => {
42
+ expect ( result . explicitType ) . that . is . a ( "string" ) ;
43
+ expect ( result . explicitType ) . to . be . equal ( "100" ) ;
44
+ } ) ;
45
+
46
+ } ) ;
47
+
48
+ describe ( "plainToClass transforms builtin primitive types properly" , ( ) => {
49
+
50
+ defaultMetadataStorage . clear ( ) ;
51
+
52
+ class Example {
53
+
54
+ @Type ( )
55
+ date : Date ;
56
+
57
+ @Type ( )
58
+ string : string ;
59
+
60
+ @Type ( )
61
+ string2 : string ;
62
+
63
+ @Type ( )
64
+ number : number ;
65
+
66
+ @Type ( )
67
+ number2 : number ;
68
+
69
+ @Type ( )
70
+ boolean : boolean ;
71
+
72
+ @Type ( )
73
+ boolean2 : boolean ;
74
+ }
75
+
76
+ const result : Example = plainToClass ( Example , {
77
+ date : "2018-12-24T12:00:00Z" ,
78
+ string : "100" ,
79
+ string2 : 100 ,
80
+ number : "100" ,
81
+ number2 : 100 ,
82
+ boolean : 1 ,
83
+ boolean2 : 0 ,
84
+ } ) ;
85
+
86
+ it ( "should recognize and convert to Date" , ( ) => {
87
+ expect ( result . date ) . to . be . instanceOf ( Date ) ;
88
+ expect ( result . date . getTime ( ) ) . to . be . equal ( new Date ( "2018-12-24T12:00:00Z" ) . getTime ( ) ) ;
89
+ } ) ;
90
+
91
+ it ( "should recognize and convert to string" , ( ) => {
92
+ expect ( result . string ) . that . is . a ( "string" ) ;
93
+ expect ( result . string2 ) . that . is . a ( "string" ) ;
94
+ expect ( result . string ) . to . be . equal ( "100" ) ;
95
+ expect ( result . string2 ) . to . be . equal ( "100" ) ;
96
+ } ) ;
97
+
98
+ it ( "should recognize and convert to number" , ( ) => {
99
+ expect ( result . number ) . that . is . a ( "number" ) ;
100
+ expect ( result . number2 ) . that . is . a ( "number" ) ;
101
+ expect ( result . number ) . to . be . equal ( 100 ) ;
102
+ expect ( result . number2 ) . to . be . equal ( 100 ) ;
103
+ } ) ;
104
+
105
+ it ( "should recognize and convert to boolean" , ( ) => {
106
+ expect ( result . boolean ) . to . be . true ;
107
+ expect ( result . boolean2 ) . to . be . false ;
108
+ } ) ;
109
+
110
+ } ) ;
0 commit comments