1
- import Morphism , { morphism , StrictSchema , Schema , createSchema } from './morphism' ;
2
-
3
- describe ( 'Typescript' , ( ) => {
4
- describe ( 'Registry Type Checking' , ( ) => {
5
- it ( 'Should return a Mapper when using Register' , ( ) => {
1
+ import Morphism , {
2
+ morphism ,
3
+ StrictSchema ,
4
+ Schema ,
5
+ createSchema
6
+ } from "./morphism" ;
7
+
8
+ describe ( "Typescript" , ( ) => {
9
+ describe ( "Registry Type Checking" , ( ) => {
10
+ it ( "Should return a Mapper when using Register" , ( ) => {
6
11
class Foo {
7
12
foo : string ;
8
13
}
9
- const schema = { foo : ' bar' } ;
10
- const source = { bar : ' value' } ;
14
+ const schema = { foo : " bar" } ;
15
+ const source = { bar : " value" } ;
11
16
const mapper = Morphism . register ( Foo , schema ) ;
12
17
13
- expect ( mapper ( source ) . foo ) . toEqual ( ' value' ) ;
14
- expect ( mapper ( [ source ] [ 0 ] ) . foo ) . toEqual ( ' value' ) ;
18
+ expect ( mapper ( source ) . foo ) . toEqual ( " value" ) ;
19
+ expect ( mapper ( [ source ] [ 0 ] ) . foo ) . toEqual ( " value" ) ;
15
20
} ) ;
16
21
} ) ;
17
22
18
- describe ( ' Schema Type Checking' , ( ) => {
19
- it ( ' Should allow to type the Schema' , ( ) => {
23
+ describe ( " Schema Type Checking" , ( ) => {
24
+ it ( " Should allow to type the Schema" , ( ) => {
20
25
interface IFoo {
21
26
foo : string ;
22
27
bar : number ;
23
28
}
24
- const schema : Schema < IFoo > = { foo : ' qux' } ;
25
- const source = { qux : ' foo' } ;
29
+ const schema : Schema < IFoo > = { foo : " qux" } ;
30
+ const source = { qux : " foo" } ;
26
31
const target = morphism ( schema , source ) ;
27
32
28
33
expect ( target . foo ) . toEqual ( source . qux ) ;
29
34
} ) ;
30
35
31
- it ( ' Should allow to use a strict Schema' , ( ) => {
36
+ it ( " Should allow to use a strict Schema" , ( ) => {
32
37
interface IFoo {
33
38
foo : string ;
34
39
bar : number ;
35
40
}
36
- const schema : StrictSchema < IFoo > = { foo : ' qux' , bar : ( ) => 1 } ;
37
- const source = { qux : ' foo' } ;
41
+ const schema : StrictSchema < IFoo > = { foo : " qux" , bar : ( ) => 1 } ;
42
+ const source = { qux : " foo" } ;
38
43
const target = morphism ( schema , source ) ;
39
44
40
45
expect ( target . foo ) . toEqual ( source . qux ) ;
41
46
expect ( target . bar ) . toEqual ( 1 ) ;
42
47
} ) ;
43
48
44
- it ( ' should accept 2 generic parameters on StrictSchema' , ( ) => {
49
+ it ( " should accept 2 generic parameters on StrictSchema" , ( ) => {
45
50
interface Source {
46
51
inputA : string ;
47
52
inputB : string ;
@@ -53,59 +58,61 @@ describe('Typescript', () => {
53
58
fooC : string ;
54
59
}
55
60
const schema : StrictSchema < Destination , Source > = {
56
- fooA : ' inputA' ,
61
+ fooA : " inputA" ,
57
62
fooB : ( { inputB } ) => inputB ,
58
- fooC : ' inputC'
63
+ fooC : " inputC"
59
64
} ;
60
65
61
66
const mapper = morphism ( schema ) ;
62
67
63
- expect ( mapper ( { inputA : 'test' , inputB : 'test2' , inputC : 'test3' } ) ) . toEqual ( {
64
- fooA : 'test' ,
65
- fooB : 'test2' ,
66
- fooC : 'test3'
68
+ expect (
69
+ mapper ( { inputA : "test" , inputB : "test2" , inputC : "test3" } )
70
+ ) . toEqual ( {
71
+ fooA : "test" ,
72
+ fooB : "test2" ,
73
+ fooC : "test3"
67
74
} ) ;
68
75
} ) ;
69
76
70
- it ( ' should accept 2 generic parameters on Schema' , ( ) => {
77
+ it ( " should accept 2 generic parameters on Schema" , ( ) => {
71
78
interface Source2 {
72
79
inputA : string ;
73
80
}
74
81
const schema : Schema < { foo : string } , Source2 > = {
75
- foo : ' inputA'
82
+ foo : " inputA"
76
83
} ;
77
- morphism ( schema , { inputA : ' test' } ) ;
78
- morphism ( schema , [ { inputA : '' } ] ) ;
84
+ morphism ( schema , { inputA : " test" } ) ;
85
+ morphism ( schema , [ { inputA : "" } ] ) ;
79
86
} ) ;
80
87
81
- it ( ' should accept 2 generic parameters on Schema' , ( ) => {
88
+ it ( " should accept 2 generic parameters on Schema" , ( ) => {
82
89
interface S {
83
90
s1 : string ;
84
91
}
85
92
interface D {
86
93
d1 : string ;
87
94
}
88
95
const schema : StrictSchema < D , S > = {
89
- d1 : 's1'
96
+ d1 : "s1"
90
97
} ;
91
- const a = morphism ( schema ) ( [ { s1 : ' test' } ] ) ;
98
+ const a = morphism ( schema ) ( [ { s1 : " test" } ] ) ;
92
99
const itemA = a . shift ( ) ;
93
100
expect ( itemA ) . toBeDefined ( ) ;
94
101
if ( itemA ) {
95
102
itemA . d1 ;
96
103
}
97
- morphism ( schema , { s1 : ' teest' } ) . d1 . toString ( ) ;
98
- const b = morphism ( schema , [ { s1 : ' teest' } ] ) ;
104
+ morphism ( schema , { s1 : " teest" } ) . d1 . toString ( ) ;
105
+ const b = morphism ( schema , [ { s1 : " teest" } ] ) ;
99
106
const itemB = b . shift ( ) ;
100
107
expect ( itemB ) . toBeDefined ( ) ;
101
108
if ( itemB ) {
102
109
itemB . d1 ;
103
110
}
104
- morphism ( schema , [ { s1 : ' teest' } ] ) ;
105
- morphism ( schema , [ { s1 : ' test' } ] ) ;
111
+ morphism ( schema , [ { s1 : " teest" } ] ) ;
112
+ morphism ( schema , [ { s1 : " test" } ] ) ;
106
113
} ) ;
107
114
108
- it ( ' should not fail with typescript' , ( ) => {
115
+ it ( " should not fail with typescript" , ( ) => {
109
116
interface S {
110
117
s1 : string ;
111
118
}
@@ -122,29 +129,45 @@ describe('Typescript', () => {
122
129
namingIsHard : string ;
123
130
}
124
131
125
- const a = morphism < StrictSchema < Destination , Source > > ( { namingIsHard : 'boring_api_field' } , [ { boring_api_field : 2 } ] ) ;
132
+ const a = morphism < StrictSchema < Destination , Source > > (
133
+ { namingIsHard : "boring_api_field" } ,
134
+ [ { boring_api_field : 2 } ]
135
+ ) ;
126
136
const itemA = a . pop ( ) ;
127
137
expect ( itemA ) . toBeDefined ( ) ;
128
138
if ( itemA ) {
129
139
itemA . namingIsHard ;
130
140
}
131
141
132
- const b = morphism < StrictSchema < Destination , Source > > ( { namingIsHard : 'boring_api_field' } , { boring_api_field : 2 } ) ;
142
+ const b = morphism < StrictSchema < Destination , Source > > (
143
+ { namingIsHard : "boring_api_field" } ,
144
+ { boring_api_field : 2 }
145
+ ) ;
133
146
b . namingIsHard ;
134
147
135
- const c = morphism < StrictSchema < Destination > > ( { namingIsHard : 'boring_api_field' } , [ { boring_api_field : 2 } ] ) ;
148
+ const c = morphism < StrictSchema < Destination > > (
149
+ { namingIsHard : "boring_api_field" } ,
150
+ [ { boring_api_field : 2 } ]
151
+ ) ;
136
152
const itemC = c . pop ( ) ;
137
153
expect ( itemC ) . toBeDefined ( ) ;
138
154
if ( itemC ) {
139
155
itemC . namingIsHard ;
140
156
}
141
157
142
- const d = morphism < Destination > ( { namingIsHard : 'boring_api_field' } , { boring_api_field : 2 } ) ;
158
+ const d = morphism < Destination > (
159
+ { namingIsHard : "boring_api_field" } ,
160
+ { boring_api_field : 2 }
161
+ ) ;
143
162
d . namingIsHard ;
144
163
145
- morphism ( { namingIsHard : 'boring_api_field' } ) ;
146
- morphism < StrictSchema < Destination , Source > > ( { namingIsHard : 'boring_api_field' } ) ( { boring_api_field : 2 } ) ;
147
- const e = morphism < StrictSchema < Destination > > ( { namingIsHard : 'boring_api_field' } ) ( [ { boring_api_field : 2 } ] ) ;
164
+ morphism ( { namingIsHard : "boring_api_field" } ) ;
165
+ morphism < StrictSchema < Destination , Source > > ( {
166
+ namingIsHard : "boring_api_field"
167
+ } ) ( { boring_api_field : 2 } ) ;
168
+ const e = morphism < StrictSchema < Destination > > ( {
169
+ namingIsHard : "boring_api_field"
170
+ } ) ( [ { boring_api_field : 2 } ] ) ;
148
171
const itemE = e . pop ( ) ;
149
172
expect ( itemE ) . toBeDefined ( ) ;
150
173
if ( itemE ) {
@@ -162,7 +185,7 @@ describe('Typescript', () => {
162
185
morphism < StrictSchema < D1 , S1 > > ( { a : ( { _a } ) => _a . toString ( ) } ) ;
163
186
} ) ;
164
187
165
- it ( ' shoud infer result type from source when a class is provided' , ( ) => {
188
+ it ( " shoud infer result type from source when a class is provided" , ( ) => {
166
189
class Source {
167
190
constructor ( public id : number , public ugly_field : string ) { }
168
191
}
@@ -171,31 +194,34 @@ describe('Typescript', () => {
171
194
constructor ( public id : number , public field : string ) { }
172
195
}
173
196
174
- const source = [ new Source ( 1 , ' abc' ) , new Source ( 1 , ' def' ) ] ;
197
+ const source = [ new Source ( 1 , " abc" ) , new Source ( 1 , " def" ) ] ;
175
198
176
199
const schema : StrictSchema < Destination , Source > = {
177
- id : 'id' ,
178
- field : ' ugly_field'
200
+ id : "id" ,
201
+ field : " ugly_field"
179
202
} ;
180
- const expected = [ new Destination ( 1 , ' abc' ) , new Destination ( 1 , ' def' ) ] ;
203
+ const expected = [ new Destination ( 1 , " abc" ) , new Destination ( 1 , " def" ) ] ;
181
204
182
205
const result = morphism ( schema , source , Destination ) ;
183
206
result . forEach ( ( item , idx ) => {
184
207
expect ( item ) . toEqual ( expected [ idx ] ) ;
185
208
} ) ;
186
209
} ) ;
187
210
188
- it ( 'should accept union types as Target' , ( ) => {
189
- const schema = createSchema < { a : string } | { a : string ; b : string } , { c : string } > ( {
211
+ it ( "should accept union types as Target" , ( ) => {
212
+ const schema = createSchema <
213
+ { a : string } | { a : string ; b : string } ,
214
+ { c : string }
215
+ > ( {
190
216
a : ( { c } ) => c
191
217
} ) ;
192
218
193
- expect ( morphism ( schema , { c : ' result' } ) . a ) . toEqual ( ' result' ) ;
219
+ expect ( morphism ( schema , { c : " result" } ) . a ) . toEqual ( " result" ) ;
194
220
} ) ;
195
221
} ) ;
196
222
197
- describe ( ' Morphism Function Type Checking' , ( ) => {
198
- it ( ' should infer target type from array input' , ( ) => {
223
+ describe ( " Morphism Function Type Checking" , ( ) => {
224
+ it ( " should infer target type from array input" , ( ) => {
199
225
interface Source {
200
226
ID : number ;
201
227
}
@@ -206,9 +232,32 @@ describe('Typescript', () => {
206
232
207
233
const rows : Array < Source > = [ { ID : 1234 } ] ;
208
234
209
- const schema : StrictSchema < Destination , Source > = { id : 'ID' } ;
235
+ const schema : StrictSchema < Destination , Source > = { id : "ID" } ;
210
236
expect ( morphism ( schema , rows ) ) . toBeDefined ( ) ;
211
237
expect ( morphism ( schema , rows ) [ 0 ] . id ) . toEqual ( 1234 ) ;
212
238
} ) ;
213
239
} ) ;
240
+
241
+ describe ( "Selector Action" , ( ) => {
242
+ it ( "should match return type of fn with target property" , ( ) => {
243
+ interface Source {
244
+ foo : string ;
245
+ }
246
+
247
+ interface Target {
248
+ foo : number ;
249
+ }
250
+
251
+ const schema : StrictSchema < Target , Source > = {
252
+ foo : {
253
+ path : "foo" ,
254
+ fn : val => {
255
+ return Number ( val ) ;
256
+ }
257
+ }
258
+ } ;
259
+ const source : Source = { foo : "1" } ;
260
+ expect ( morphism ( schema , source ) ) . toEqual ( { foo : 1 } ) ;
261
+ } ) ;
262
+ } ) ;
214
263
} ) ;
0 commit comments