@@ -295,11 +295,11 @@ describe('Controller.get()', () => {
295
295
id : string = '' ;
296
296
}
297
297
class User extends IDEntity {
298
- type = 'user ' ;
298
+ type = 'users ' ;
299
299
username : string = '' ;
300
300
}
301
301
class Group extends IDEntity {
302
- type = 'group ' ;
302
+ type = 'groups ' ;
303
303
groupname : string = '' ;
304
304
memberCount = 0 ;
305
305
}
@@ -349,6 +349,70 @@ describe('Controller.get()', () => {
349
349
// @ts -expect-error
350
350
( ) => controller . get ( queryPerson , { id : '1' , doesnotexist : 5 } , state ) ;
351
351
} ) ;
352
+
353
+ it ( 'Union based on args with function schemaAttribute' , ( ) => {
354
+ class IDEntity extends Entity {
355
+ id : string = '' ;
356
+ }
357
+ class User extends IDEntity {
358
+ type = 'user' ;
359
+ username : string = '' ;
360
+ }
361
+ class Group extends IDEntity {
362
+ type = 'group' ;
363
+ groupname : string = '' ;
364
+ memberCount = 0 ;
365
+ }
366
+ const queryPerson = new schema . Union (
367
+ {
368
+ users : User ,
369
+ groups : Group ,
370
+ } ,
371
+ ( value : { type : 'users' | 'groups' } ) => value . type ,
372
+ ) ;
373
+ const controller = new Controller ( ) ;
374
+ const state = {
375
+ ...initialState ,
376
+ entities : {
377
+ User : {
378
+ '1' : { id : '1' , type : 'users' , username : 'bob' } ,
379
+ } ,
380
+ Group : {
381
+ '2' : { id : '2' , type : 'groups' , groupname : 'fast' , memberCount : 5 } ,
382
+ } ,
383
+ } ,
384
+ } ;
385
+ const user = controller . get ( queryPerson , { id : '1' , type : 'users' } , state ) ;
386
+ expect ( user ) . toBeDefined ( ) ;
387
+ expect ( user ) . toBeInstanceOf ( User ) ;
388
+ expect ( user ) . toMatchSnapshot ( ) ;
389
+ const group = controller . get (
390
+ queryPerson ,
391
+ { id : '2' , type : 'groups' } ,
392
+ state ,
393
+ ) ;
394
+ expect ( group ) . toBeDefined ( ) ;
395
+ expect ( group ) . toBeInstanceOf ( Group ) ;
396
+ expect ( group ) . toMatchSnapshot ( ) ;
397
+
398
+ // should maintain referential equality
399
+ expect ( user ) . toBe (
400
+ controller . get ( queryPerson , { id : '1' , type : 'users' } , state ) ,
401
+ ) ;
402
+
403
+ // these are the 'fallback case' where it cannot determine type discriminator, so just enumerates
404
+ ( ) => controller . get ( queryPerson , { id : '1' } , state ) ;
405
+ // @ts -expect-error
406
+ ( ) => controller . get ( queryPerson , { id : '1' , type : 'notrealtype' } , state ) ;
407
+ // @ts -expect-error
408
+ ( ) => controller . get ( queryPerson , { id : { bob : 5 } , type : 'users' } , state ) ;
409
+ // @ts -expect-error
410
+ expect ( controller . get ( queryPerson , 5 , state ) ) . toBeUndefined ( ) ;
411
+ // @ts -expect-error
412
+ ( ) => controller . get ( queryPerson , { doesnotexist : 5 } , state ) ;
413
+ // @ts -expect-error
414
+ ( ) => controller . get ( queryPerson , { id : '1' , doesnotexist : 5 } , state ) ;
415
+ } ) ;
352
416
} ) ;
353
417
354
418
describe ( 'Snapshot.getQueryMeta()' , ( ) => {
0 commit comments