@@ -295,11 +295,11 @@ describe('Controller.get()', () => {
295295 id : string = '' ;
296296 }
297297 class User extends IDEntity {
298- type = 'user ' ;
298+ type = 'users ' ;
299299 username : string = '' ;
300300 }
301301 class Group extends IDEntity {
302- type = 'group ' ;
302+ type = 'groups ' ;
303303 groupname : string = '' ;
304304 memberCount = 0 ;
305305 }
@@ -349,6 +349,70 @@ describe('Controller.get()', () => {
349349 // @ts -expect-error
350350 ( ) => controller . get ( queryPerson , { id : '1' , doesnotexist : 5 } , state ) ;
351351 } ) ;
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+ } ) ;
352416} ) ;
353417
354418describe ( 'Snapshot.getQueryMeta()' , ( ) => {
0 commit comments