1
- import Morphism , { morphism , StrictSchema , Schema } from './morphism' ;
1
+ import Morphism , { morphism , StrictSchema , Schema , createSchema } from './morphism' ;
2
2
3
3
describe ( 'Typescript' , ( ) => {
4
4
describe ( 'Registry Type Checking' , ( ) => {
@@ -143,8 +143,12 @@ describe('Typescript', () => {
143
143
d . namingIsHard ;
144
144
145
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 } ] ) ;
146
+ morphism < StrictSchema < Destination , Source > > ( {
147
+ namingIsHard : 'boring_api_field' ,
148
+ } ) ( { boring_api_field : 2 } ) ;
149
+ const e = morphism < StrictSchema < Destination > > ( {
150
+ namingIsHard : 'boring_api_field' ,
151
+ } ) ( [ { boring_api_field : 2 } ] ) ;
148
152
const itemE = e . pop ( ) ;
149
153
expect ( itemE ) . toBeDefined ( ) ;
150
154
if ( itemE ) {
@@ -161,6 +165,37 @@ describe('Typescript', () => {
161
165
morphism < StrictSchema < D1 , S1 > > ( { a : ( { _a } ) => _a . toString ( ) } ) ;
162
166
morphism < StrictSchema < D1 , S1 > > ( { a : ( { _a } ) => _a . toString ( ) } ) ;
163
167
} ) ;
168
+
169
+ it ( 'shoud infer result type from source when a class is provided' , ( ) => {
170
+ class Source {
171
+ constructor ( public id : number , public ugly_field : string ) { }
172
+ }
173
+
174
+ class Destination {
175
+ constructor ( public id : number , public field : string ) { }
176
+ }
177
+
178
+ const source = [ new Source ( 1 , 'abc' ) , new Source ( 1 , 'def' ) ] ;
179
+
180
+ const schema : StrictSchema < Destination , Source > = {
181
+ id : 'id' ,
182
+ field : 'ugly_field' ,
183
+ } ;
184
+ const expected = [ new Destination ( 1 , 'abc' ) , new Destination ( 1 , 'def' ) ] ;
185
+
186
+ const result = morphism ( schema , source , Destination ) ;
187
+ result . forEach ( ( item , idx ) => {
188
+ expect ( item ) . toEqual ( expected [ idx ] ) ;
189
+ } ) ;
190
+ } ) ;
191
+
192
+ it ( 'should accept union types as Target' , ( ) => {
193
+ const schema = createSchema < { a : string } | { a : string ; b : string } , { c : string } > ( {
194
+ a : ( { c } ) => c ,
195
+ } ) ;
196
+
197
+ expect ( morphism ( schema , { c : 'result' } ) . a ) . toEqual ( 'result' ) ;
198
+ } ) ;
164
199
} ) ;
165
200
166
201
describe ( 'Morphism Function Type Checking' , ( ) => {
@@ -180,4 +215,27 @@ describe('Typescript', () => {
180
215
expect ( morphism ( schema , rows ) [ 0 ] . id ) . toEqual ( 1234 ) ;
181
216
} ) ;
182
217
} ) ;
218
+
219
+ describe ( 'Selector Action' , ( ) => {
220
+ it ( 'should match return type of fn with target property' , ( ) => {
221
+ interface Source {
222
+ foo : string ;
223
+ }
224
+
225
+ interface Target {
226
+ foo : number ;
227
+ }
228
+
229
+ const schema : StrictSchema < Target , Source > = {
230
+ foo : {
231
+ path : 'foo' ,
232
+ fn : val => {
233
+ return Number ( val ) ;
234
+ } ,
235
+ } ,
236
+ } ;
237
+ const source : Source = { foo : '1' } ;
238
+ expect ( morphism ( schema , source ) ) . toEqual ( { foo : 1 } ) ;
239
+ } ) ;
240
+ } ) ;
183
241
} ) ;
0 commit comments