@@ -200,21 +200,65 @@ describe.each([
200200 expect ( createOutput ( value ) ) . toMatchSnapshot ( ) ;
201201 } ) ;
202202
203+ test ( 'denormalizes removes undefined or INVALID entities with polymorphism' , ( ) => {
204+ class Cat extends IDEntity {
205+ readonly type = 'Cat' ;
206+ }
207+ class Dog extends IDEntity {
208+ readonly type = 'dogs' ;
209+ }
210+ class Person extends IDEntity {
211+ readonly type = 'people' ;
212+ }
213+ const listSchema = new schema . All (
214+ {
215+ Cat : Cat ,
216+ dogs : Dog ,
217+ people : Person ,
218+ } ,
219+ input => input . type || 'dogs' ,
220+ ) ;
221+
222+ const state = createInput ( {
223+ entities : {
224+ Cat : {
225+ '1' : { id : '1' , name : 'Milo' , type : 'Cat' } ,
226+ '2' : { id : '2' , name : 'Jake' , type : 'Cat' } ,
227+ '3' : undefined ,
228+ '4' : INVALID ,
229+ } ,
230+ Dog : {
231+ '1' : { id : '1' , name : 'Rex' , type : 'dogs' } ,
232+ '2' : INVALID ,
233+ } ,
234+ Person : {
235+ '1' : { id : '1' , name : 'Alice' , type : 'people' } ,
236+ '2' : undefined ,
237+ } ,
238+ } ,
239+ indexes : { } ,
240+ } ) ;
241+ const value = new MemoCache ( MyDelegate ) . query ( listSchema , [ ] , state ) . data ;
242+ expect ( value ) . not . toEqual ( expect . any ( Symbol ) ) ;
243+ if ( typeof value === 'symbol' || value === undefined ) return ;
244+ expect ( createOutput ( value ) . length ) . toBe ( 4 ) ;
245+ expect ( createOutput ( value ) ) . toMatchSnapshot ( ) ;
246+ } ) ;
247+
203248 test ( 'denormalize maintains referential equality until entities are added' , ( ) => {
204249 class Cat extends IDEntity { }
205250 ( Cat as any ) . defaults ;
206251 const catSchema = { results : new schema . All ( Cat ) , nextPage : '' } ;
207- let state : State < unknown > = {
252+ let state : State < unknown > = createInput ( {
208253 ...initialState ,
209254 entities : {
210255 Cat : {
211- 1 : { id : '1' , name : 'Milo' } ,
212- 2 : { id : '2' , name : 'Jake' } ,
256+ '1' : { id : '1' , name : 'Milo' } ,
257+ '2' : { id : '2' , name : 'Jake' } ,
213258 } ,
214259 } ,
215- indexes : { } ,
216- } ;
217- const memo = new MemoCache ( ) ;
260+ } ) as any ;
261+ const memo = new MemoCache ( MyDelegate ) ;
218262 const value = memo . query ( catSchema , [ ] , state ) . data ;
219263
220264 expect ( createOutput ( value ) . results ?. length ) . toBe ( 2 ) ;
@@ -225,16 +269,27 @@ describe.each([
225269 ) ;
226270 expect ( value ) . toBe ( value2 ) ;
227271
228- state = {
229- ...state ,
230- entities : {
231- ...state . entities ,
232- Cat : {
233- ...state . entities . Cat ,
234- 3 : { id : '3' , name : 'Jelico' } ,
272+ if ( ImmDelegate === MyDelegate ) {
273+ state = {
274+ ...state ,
275+ entities : ( state . entities as any ) . setIn ( [ 'Cat' , '3' ] , {
276+ id : '3' ,
277+ name : 'Jelico' ,
278+ } ) ,
279+ } as any ;
280+ } else {
281+ state = {
282+ ...state ,
283+ entities : {
284+ ...state . entities ,
285+ Cat : {
286+ ...state . entities . Cat ,
287+ '3' : { id : '3' , name : 'Jelico' } ,
288+ } ,
235289 } ,
236- } ,
237- } ;
290+ } as any ;
291+ }
292+
238293 const value3 = memo . query ( catSchema , [ ] , state ) . data ;
239294 expect ( createOutput ( value3 ) . results ?. length ) . toBe ( 3 ) ;
240295 expect ( createOutput ( value3 ) . results ) . toMatchSnapshot ( ) ;
0 commit comments