@@ -73,6 +73,166 @@ describe(`${schema.Union.name} normalization`, () => {
7373 } ) ;
7474} ) ;
7575
76+ describe ( `${ schema . Union . name } buildQueryKey` , ( ) => {
77+ class User extends IDEntity {
78+ static key = 'User' ;
79+ }
80+ class Group extends IDEntity {
81+ static key = 'Group' ;
82+ }
83+
84+ // Common schema definitions
85+ const stringAttributeUnion = new schema . Union (
86+ {
87+ users : User ,
88+ groups : Group ,
89+ } ,
90+ 'type' ,
91+ ) ;
92+
93+ const functionAttributeUnion = new schema . Union (
94+ {
95+ users : User ,
96+ groups : Group ,
97+ } ,
98+ input => {
99+ return (
100+ input . username ? 'users'
101+ : input . groupname ? 'groups'
102+ : undefined
103+ ) ;
104+ } ,
105+ ) ;
106+
107+ test ( 'buildQueryKey with discriminator in args' , ( ) => {
108+ const memo = new SimpleMemoCache ( ) ;
109+
110+ const state = {
111+ entities : {
112+ User : {
113+ 1 : { id : '1' , username : 'Janey' , type : 'users' } ,
114+ } ,
115+ Group : {
116+ 2 : { id : '2' , groupname : 'People' , type : 'groups' } ,
117+ } ,
118+ } ,
119+ indexes : { } ,
120+ } ;
121+
122+ // Fast case - args include type discriminator
123+ const result1 = memo . memo . buildQueryKey (
124+ stringAttributeUnion ,
125+ [ { id : '1' , type : 'users' } ] ,
126+ state ,
127+ ) ;
128+ expect ( result1 ) . toEqual ( { id : '1' , schema : 'users' } ) ;
129+
130+ const result2 = memo . memo . buildQueryKey (
131+ stringAttributeUnion ,
132+ [ { id : '2' , type : 'groups' } ] ,
133+ state ,
134+ ) ;
135+ expect ( result2 ) . toEqual ( { id : '2' , schema : 'groups' } ) ;
136+ } ) ;
137+
138+ test ( 'buildQueryKey without discriminator - fallback case' , ( ) => {
139+ const memo = new SimpleMemoCache ( ) ;
140+
141+ const state = {
142+ entities : {
143+ User : {
144+ 1 : { id : '1' , username : 'Janey' , type : 'users' } ,
145+ } ,
146+ Group : {
147+ 2 : { id : '2' , groupname : 'People' , type : 'groups' } ,
148+ } ,
149+ } ,
150+ indexes : { } ,
151+ } ;
152+
153+ // Fallback case - args missing type discriminator, only {id}
154+ // Should try every possible schema until it finds a match
155+ const result1 = memo . memo . buildQueryKey (
156+ stringAttributeUnion ,
157+ [ { id : '1' } ] ,
158+ state ,
159+ ) ;
160+ expect ( result1 ) . toEqual ( { id : '1' , schema : 'users' } ) ;
161+
162+ const result2 = memo . memo . buildQueryKey (
163+ stringAttributeUnion ,
164+ [ { id : '2' } ] ,
165+ state ,
166+ ) ;
167+ expect ( result2 ) . toEqual ( { id : '2' , schema : 'groups' } ) ;
168+ } ) ;
169+
170+ test ( 'buildQueryKey with function schemaAttribute missing discriminator' , ( ) => {
171+ const memo = new SimpleMemoCache ( ) ;
172+
173+ const state = {
174+ entities : {
175+ User : {
176+ 1 : { id : '1' , username : 'Janey' } ,
177+ } ,
178+ Group : {
179+ 2 : { id : '2' , groupname : 'People' } ,
180+ } ,
181+ } ,
182+ indexes : { } ,
183+ } ;
184+
185+ // With function schemaAttribute, args missing username/groupname
186+ // Should fallback to trying every schema
187+ const result1 = memo . memo . buildQueryKey (
188+ functionAttributeUnion ,
189+ [ { id : '1' } ] ,
190+ state ,
191+ ) ;
192+ expect ( result1 ) . toEqual ( { id : '1' , schema : 'users' } ) ;
193+
194+ const result2 = memo . memo . buildQueryKey (
195+ functionAttributeUnion ,
196+ [ { id : '2' } ] ,
197+ state ,
198+ ) ;
199+ expect ( result2 ) . toEqual ( { id : '2' , schema : 'groups' } ) ;
200+ } ) ;
201+
202+ test ( 'buildQueryKey returns undefined when no entity found' , ( ) => {
203+ const memo = new SimpleMemoCache ( ) ;
204+
205+ const state = {
206+ entities : {
207+ User : { } ,
208+ Group : { } ,
209+ } ,
210+ indexes : { } ,
211+ } ;
212+
213+ // No entity exists with id '999'
214+ const result = memo . memo . buildQueryKey (
215+ stringAttributeUnion ,
216+ [ { id : '999' } ] ,
217+ state ,
218+ ) ;
219+ expect ( result ) . toBeUndefined ( ) ;
220+ } ) ;
221+
222+ test ( 'buildQueryKey returns undefined when no args provided' , ( ) => {
223+ const memo = new SimpleMemoCache ( ) ;
224+
225+ const state = {
226+ entities : { } ,
227+ indexes : { } ,
228+ } ;
229+
230+ // No args provided
231+ const result = memo . memo . buildQueryKey ( stringAttributeUnion , [ ] , state ) ;
232+ expect ( result ) . toBeUndefined ( ) ;
233+ } ) ;
234+ } ) ;
235+
76236describe ( 'complex case' , ( ) => {
77237 test ( 'works with undefined' , ( ) => {
78238 const response = {
0 commit comments