-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathMemoCache.ts
More file actions
1058 lines (988 loc) · 29.2 KB
/
MemoCache.ts
File metadata and controls
1058 lines (988 loc) · 29.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// eslint-env jest
import { Entity } from '@data-client/endpoint';
import { schema as schemas } from '@data-client/endpoint';
import {
UnionResource,
CoolerArticle,
IndexedUser,
FirstUnion,
} from '__tests__/new';
import { fromJSState } from './immutable.test';
import { IQueryDelegate } from '../interface';
import MemoCache from '../memo/MemoCache';
class IDEntity extends Entity {
id = '';
pk() {
return this.id;
}
}
class Tacos extends IDEntity {
type = '';
}
let dateSpy;
beforeAll(() => {
dateSpy = jest
.spyOn(global.Date, 'now')
.mockImplementation(() => new Date('2019-05-14T11:01:58.135Z').valueOf());
});
afterAll(() => {
dateSpy.mockRestore();
});
describe('MemoCache', () => {
describe('denormalize', () => {
test('denormalizes suspends when symbol contains DELETED string', () => {
const entities = {
Tacos: {
1: Symbol('ENTITY WAS DELETED'),
},
};
expect(new MemoCache().denormalize(Tacos, '1', entities).data).toEqual(
expect.any(Symbol),
);
});
test('maintains referential equality with same results', () => {
const memo = new MemoCache();
const entities = {
Tacos: {
1: { id: '1', type: 'foo' },
2: { id: '2', type: 'bar' },
},
};
const result = ['1', '2'];
const schema = [Tacos];
const { data: first, paths: pathsFirst } = memo.denormalize(
schema,
result,
entities,
);
const { data: second, paths: pathsSecond } = memo.denormalize(
schema,
result,
entities,
);
expect(first).toBe(second);
expect(pathsFirst).toEqual(pathsSecond);
const { data: third } = memo.denormalize(schema, [...result], entities);
expect(first).not.toBe(third);
expect(first).toEqual(third);
const fourth = memo.denormalize(schema, result, {
Tacos: { ...entities.Tacos, 2: { id: '2', type: 'bar' } },
}).data;
expect(first).not.toBe(fourth);
expect(first).toEqual(fourth);
});
test('updates when results change, but entities are the same', () => {
const memo = new MemoCache();
const entities = {
Tacos: {
1: { id: '1', type: 'foo' },
2: { id: '2', type: 'bar' },
},
};
const result = { data: ['1', '2'], nextPage: 'initial' };
const schema = { data: [Tacos], nextPage: '' };
const { data: first, paths } = memo.denormalize(schema, result, entities);
const { data: second, paths: pathsSecond } = memo.denormalize(
schema,
{ ...result, nextPage: 'second' },
entities,
);
if (typeof first === 'symbol' || typeof second === 'symbol')
throw new Error();
expect(first.nextPage).toBe('initial');
expect(second.nextPage).toBe('second');
expect(first.data).toEqual(second.data);
if (!first.data || !second.data) throw Error();
for (let i = 0; i < first.data?.length; i++) {
expect(first.data[i]).toBe(second.data[i]);
}
const { data: fourth, paths: fourthPaths } = memo.denormalize(
schema,
result,
{
Tacos: { ...entities.Tacos, 2: { id: '2', type: 'bar' } },
},
);
if (typeof fourth === 'symbol' || !fourth.data) throw new Error();
expect(first).not.toBe(fourth);
expect(first).toEqual(fourth);
expect(first.data[0]).toBe(fourth.data[0]);
});
describe('nested entities', () => {
class User extends IDEntity {
name = '';
}
class Comment extends IDEntity {
comment = '';
user = User.fromJS();
static schema = {
user: User,
};
}
class Article extends IDEntity {
title = '';
body = '';
author = User.fromJS();
comments: Comment[] = [];
static schema = {
author: User,
comments: [Comment],
};
}
const entities = {
Article: {
123: {
author: '8472',
body: 'This article is great.',
comments: ['comment-123-4738'],
id: '123',
title: 'A Great Article',
},
},
Comment: {
'comment-123-4738': {
comment: 'I like it!',
id: 'comment-123-4738',
user: '10293',
},
},
User: {
10293: {
id: '10293',
name: 'Jane',
},
8472: {
id: '8472',
name: 'Paul',
},
},
};
test('maintains referential equality with nested entities', () => {
const memo = new MemoCache();
const result = { data: '123' };
const schema = { data: Article };
const first = memo.denormalize(schema, result, entities).data;
const second = memo.denormalize(schema, result, entities).data;
expect(first).toBe(second);
const third = memo.denormalize(Article, '123', entities).data;
const fourth = memo.denormalize(Article, '123', entities).data;
expect(third).toBe(fourth);
});
test('maintains responds to entity updates for distinct top-level results', () => {
const memo = new MemoCache();
const result1 = { data: '123' };
const result2 = { results: ['123'] };
const first = memo.denormalize(
{ data: Article },
result1,
entities,
).data;
const second = memo.denormalize(
{ results: [Article] },
result2,
entities,
).data;
if (
typeof first === 'symbol' ||
typeof second === 'symbol' ||
!second.results
)
throw new Error();
expect(first.data).toBe(second.results[0]);
const third = memo.denormalize(Article, '123', entities).data;
expect(third).toBe(first.data);
// now change
const nextState = {
...entities,
Article: {
123: {
...entities.Article[123],
title: 'updated article',
body: 'new body',
},
},
};
const firstChanged = memo.denormalize(
{ data: Article },
result1,
nextState,
).data;
expect(firstChanged).not.toBe(first);
const secondChanged = memo.denormalize(
{ results: [Article] },
result2,
nextState,
).data;
expect(secondChanged).not.toBe(second);
if (
typeof firstChanged === 'symbol' ||
typeof secondChanged === 'symbol'
)
throw new Error('symbol');
expect(firstChanged.data).toBe(secondChanged.results?.[0]);
});
test('handles multi-schema (summary entities)', () => {
class ArticleSummary extends IDEntity {
title = '';
body = '';
author = '';
static schema = {
comments: [Comment],
};
static key = 'Article';
}
const memo = new MemoCache();
// we have different result values to represent different endpoint inputs
const resultA = { data: '123' };
const resultB = { data: '123' };
const resultC = '123';
const firstSchema = { data: ArticleSummary };
const secondSchema = { data: Article };
const first = memo.denormalize(firstSchema, resultA, entities).data;
const second = memo.denormalize(secondSchema, resultB, entities).data;
if (
typeof first === 'symbol' ||
typeof second === 'symbol' ||
!second.data ||
!first.data
)
throw new Error();
// show distinction between how they are denormalized
expect(first.data.author).toMatchInlineSnapshot(`"8472"`);
expect(second.data.author).toMatchInlineSnapshot(`
User {
"id": "8472",
"name": "Paul",
}
`);
expect(first.data).not.toBe(second.data);
const firstWithoutChange = memo.denormalize(
firstSchema,
resultA,
entities,
).data;
expect(first).toBe(firstWithoutChange);
const third = memo.denormalize(Article, resultC, entities).data;
expect(third).toBe(second.data);
// now change
const nextState = {
...entities,
Article: {
123: {
...entities.Article[123],
title: 'updated article',
body: 'new body',
},
},
};
const firstChanged = memo.denormalize(
firstSchema,
resultA,
nextState,
).data;
expect(firstChanged).not.toBe(first);
const secondChanged = memo.denormalize(
secondSchema,
resultB,
nextState,
).data;
expect(secondChanged).not.toBe(second);
if (
typeof firstChanged === 'symbol' ||
typeof secondChanged === 'symbol' ||
!firstChanged.data ||
!secondChanged.data
)
throw new Error();
expect(firstChanged.data.author).toMatchInlineSnapshot(`"8472"`);
expect(secondChanged.data.author).toMatchInlineSnapshot(`
User {
"id": "8472",
"name": "Paul",
}
`);
});
test('entity equality changes', () => {
const memo = new MemoCache();
const result = { data: '123' };
const { data: first } = memo.denormalize(
{ data: Article },
result,
entities,
);
const { data: second } = memo.denormalize({ data: Article }, result, {
...entities,
Article: {
123: {
author: '8472',
body: 'This article is great.',
comments: ['comment-123-4738'],
id: '123',
title: 'A Great Article',
},
},
});
expect(first).not.toBe(second);
if (
typeof first === 'symbol' ||
typeof second === 'symbol' ||
!second.data ||
!first.data
)
throw new Error();
expect(first.data.author).toBe(second.data.author);
expect(first.data.comments[0]).toBe(second.data.comments[0]);
});
test('nested entity equality changes', () => {
const memo = new MemoCache();
const result = { data: '123' };
const { data: first } = memo.denormalize(
{ data: Article },
result,
entities,
);
const { data: second } = memo.denormalize({ data: Article }, result, {
...entities,
Comment: {
'comment-123-4738': {
comment: 'Updated comment!',
id: 'comment-123-4738',
user: '10293',
},
},
});
expect(first).not.toBe(second);
if (
typeof first === 'symbol' ||
typeof second === 'symbol' ||
!second.data ||
!first.data
)
throw new Error();
expect(first.data.title).toBe(second.data.title);
expect(first.data.author).toBe(second.data.author);
expect(second.data.comments[0].comment).toEqual('Updated comment!');
expect(first.data.comments[0]).not.toBe(second.data.comments[0]);
expect(first.data.comments[0].user).toBe(second.data.comments[0].user);
});
test('nested entity becomes present in entity table', () => {
const memo = new MemoCache();
const result = { data: '123' };
const emptyEntities = {
...entities,
// no Users exist
User: {},
};
const { data: first } = memo.denormalize(
{ data: Article },
result,
emptyEntities,
);
const { data: second } = memo.denormalize(
{ data: Article },
result,
emptyEntities,
);
const { data: third } = memo.denormalize(
{ data: Article },
result,
// now has users
entities,
);
if (
typeof first === 'symbol' ||
typeof second === 'symbol' ||
typeof third === 'symbol' ||
!second.data ||
!first.data ||
!third.data
)
throw new Error();
expect(first.data.title).toBe(third.data.title);
expect(first.data.author).toBeUndefined();
// maintain cache when nested value is undefined
expect(first.data).toBe(second.data);
expect(first).toBe(second);
// update value when nested value becomes defined
expect(third.data.author).toBeDefined();
expect(third.data.author.name).toEqual(expect.any(String));
expect(first).not.toBe(third);
});
test('nested entity becomes present in entity table with numbers', () => {
const memo = new MemoCache();
class User extends IDEntity {
name = '';
}
class Article extends IDEntity {
title = '';
author = User.fromJS();
static schema = {
author: User,
};
}
const result = { data: 123 };
const entities = {
Article: {
123: {
author: 8472,
id: 123,
title: 'A Great Article',
},
},
User: {
8472: {
id: 8472,
name: 'Paul',
},
},
};
const emptyEntities = {
...entities,
// no Users exist
User: {},
};
const { data: first } = memo.denormalize(
{ data: Article },
result,
emptyEntities,
);
const { data: second } = memo.denormalize(
{ data: Article },
result,
emptyEntities,
);
const { data: third } = memo.denormalize(
{ data: Article },
result,
// now has users
entities,
);
if (
typeof first === 'symbol' ||
typeof second === 'symbol' ||
typeof third === 'symbol' ||
!second.data ||
!first.data ||
!third.data
)
throw new Error();
expect(first.data.title).toBe(third.data.title);
expect(first.data.author).toBeUndefined();
// maintain cache when nested value is undefined
expect(first.data).toBe(second.data);
expect(first).toBe(second);
// update value when nested value becomes defined
expect(third.data.author).toBeDefined();
expect(third.data.author.name).toEqual(expect.any(String));
expect(first).not.toBe(third);
});
});
test('denormalizes plain object with no entities', () => {
const memo = new MemoCache();
const input = {
firstThing: { five: 5, seven: 42 },
secondThing: { cars: 'fifo' },
};
const schema = {
firstThing: { five: 0, seven: 0 },
secondThing: { cars: '' },
};
const { data: first } = memo.denormalize(schema, input, {});
expect(first).toEqual(input);
// should maintain referential equality
const { data: second } = memo.denormalize(schema, input, {});
expect(second).toBe(first);
});
test('passthrough for null schema and an object input', () => {
const memo = new MemoCache();
const input = {
firstThing: { five: 5, seven: 42 },
secondThing: { cars: 'never' },
};
const { data } = memo.denormalize(null, input, {});
expect(data).toBe(input);
});
test('passthrough for null schema and an number input', () => {
const memo = new MemoCache();
const input = 5;
const { data } = memo.denormalize(null, input, {});
expect(data).toBe(input);
});
test('passthrough for undefined schema and an object input', () => {
const memo = new MemoCache();
const input = {
firstThing: { five: 5, seven: 42 },
secondThing: { cars: 'never' },
};
const { data } = memo.denormalize(undefined, input, {});
expect(data).toBe(input);
});
describe('null inputs when expecting entities', () => {
class User extends IDEntity {}
class Comment extends IDEntity {
comment = '';
static schema = {
user: User,
};
}
class Article extends IDEntity {
title = '';
body = '';
author = User.fromJS({});
comments = [];
static schema = {
author: User,
comments: [Comment],
};
}
test('handles null at top level', () => {
const memo = new MemoCache();
const denorm = memo.denormalize({ data: Article }, null, {}).data;
expect(denorm).toEqual(null);
});
test('handles undefined at top level', () => {
const memo = new MemoCache();
const denorm = memo.denormalize({ data: Article }, undefined, {}).data;
expect(denorm).toEqual(undefined);
});
test('handles null in nested place', () => {
const memo = new MemoCache();
const input = {
data: { id: '5', title: 'hehe', author: null, comments: [] },
};
const denorm = memo.denormalize({ data: Article }, input, {}).data;
expect(denorm).toMatchInlineSnapshot(`
{
"data": Article {
"author": null,
"body": "",
"comments": [],
"id": "5",
"title": "hehe",
},
}
`);
});
});
});
describe('buildQueryKey', () => {
it('should work with Object', () => {
const schema = new schemas.Object({
data: new schemas.Object({
article: CoolerArticle,
}),
});
expect(
new MemoCache().buildQueryKey(schema, [{ id: 5 }], {
entities: {
[CoolerArticle.key]: { '5': {} },
},
indexes: {},
}),
).toEqual({
data: { article: 5 },
});
});
it('should work with number argument', () => {
const schema = new schemas.Object({
data: new schemas.Object({
article: CoolerArticle,
}),
});
expect(
new MemoCache().buildQueryKey(schema, [5], {
entities: {
[CoolerArticle.key]: { '5': {} },
},
indexes: {},
}),
).toEqual({
data: { article: '5' },
});
});
it('should work with string argument', () => {
const schema = new schemas.Object({
data: new schemas.Object({
article: CoolerArticle,
}),
});
expect(
new MemoCache().buildQueryKey(schema, ['5'], {
entities: {
[CoolerArticle.key]: { '5': {} },
},
indexes: {},
}),
).toEqual({
data: { article: '5' },
});
});
it('should be undefined with Array', () => {
const schema = {
data: new schemas.Array(CoolerArticle),
};
expect(
new MemoCache().buildQueryKey(schema, [{ id: 5 }], {
entities: {
[CoolerArticle.key]: { '5': {} },
},
indexes: {},
}),
).toStrictEqual({
data: undefined,
});
const schema2 = {
data: [CoolerArticle],
};
expect(
new MemoCache().buildQueryKey(schema2, [{ id: 5 }], {
entities: {
[CoolerArticle.key]: { '5': {} },
},
indexes: {},
}),
).toStrictEqual({
data: undefined,
});
});
it('should be undefined with Values', () => {
const schema = {
data: new schemas.Values(CoolerArticle),
};
expect(
new MemoCache().buildQueryKey(schema, [{ id: 5 }], {
entities: {
[CoolerArticle.key]: { '5': {} },
},
indexes: {},
}),
).toStrictEqual({
data: undefined,
});
});
it('should be undefined with Union and type', () => {
const schema = UnionResource.get.schema;
expect(
new MemoCache().buildQueryKey(schema, [{ id: 5 }], {
entities: {
[CoolerArticle.key]: {
'5': {},
},
},
indexes: {},
}),
).toBe(undefined);
});
it('should work with Union', () => {
const schema = UnionResource.get.schema;
expect(
new MemoCache().buildQueryKey(schema, [{ id: 5, type: 'first' }], {
entities: {
[FirstUnion.key]: {
'5': {},
},
},
indexes: {},
}),
).toMatchInlineSnapshot(`
{
"id": 5,
"schema": "first",
}
`);
});
it('should work with primitive defaults', () => {
const schema = {
pagination: { next: '', previous: '' },
data: CoolerArticle,
};
expect(
new MemoCache().buildQueryKey(schema, [{ id: 5 }], {
entities: {
[CoolerArticle.key]: {
'5': {},
},
},
indexes: {},
}),
).toEqual({
pagination: { next: '', previous: '' },
data: 5,
});
});
it('should work with indexes', () => {
const schema = {
pagination: { next: '', previous: '' },
data: IndexedUser,
};
expect(
new MemoCache().buildQueryKey(schema, [{ username: 'bob' }], {
entities: {
[IndexedUser.key]: {
'5': {},
},
},
indexes: {
[IndexedUser.key]: {
username: {
bob: '5',
},
},
},
}),
).toEqual({
pagination: { next: '', previous: '' },
data: '5',
});
expect(
new MemoCache().buildQueryKey(
schema,
[{ username: 'bob', mary: 'five' }],
{
entities: {
[IndexedUser.key]: {
'5': {},
},
},
indexes: {
[IndexedUser.key]: {
username: {
bob: '5',
},
},
},
},
),
).toEqual({
pagination: { next: '', previous: '' },
data: '5',
});
});
it('should work with indexes but none set', () => {
const schema = {
pagination: { next: '', previous: '' },
data: IndexedUser,
};
expect(
new MemoCache().buildQueryKey(schema, [{ username: 'bob' }], {
entities: {
[IndexedUser.key]: {
'5': {},
},
},
indexes: {
[IndexedUser.key]: {
username: {
charles: '5',
},
},
},
}),
).toEqual({
pagination: { next: '', previous: '' },
data: undefined,
});
expect(
new MemoCache().buildQueryKey(schema, [{ hover: 'bob' }], {
entities: {
[IndexedUser.key]: {
'5': {},
},
},
indexes: {
[IndexedUser.key]: {
username: {
charles: '5',
},
},
},
}),
).toEqual({
pagination: { next: '', previous: '' },
data: undefined,
});
});
it('should work with indexes but no indexes stored', () => {
const schema = {
pagination: { next: '', previous: '' },
data: IndexedUser,
};
expect(
new MemoCache().buildQueryKey(schema, [{ username: 'bob' }], {
entities: { [IndexedUser.key]: { '5': {} } },
indexes: {},
}),
).toEqual({
pagination: { next: '', previous: '' },
data: undefined,
});
expect(
new MemoCache().buildQueryKey(schema, [{ hover: 'bob' }], {
entities: { [IndexedUser.key]: { '5': {} } },
indexes: {},
}),
).toEqual({
pagination: { next: '', previous: '' },
data: undefined,
});
});
describe('legacy schema', () => {
class MyEntity extends CoolerArticle {
static queryKey(args: any[], unvisit: any, snapshot: IQueryDelegate) {
if (!args[0]) return;
let id: undefined | number | string;
if (['string', 'number'].includes(typeof args[0])) {
id = `${args[0]}`;
} else {
id = this.pk(args[0], undefined, '', args);
}
// Was able to infer the entity's primary key from params
if (id !== undefined && id !== '' && snapshot.getEntity(this.key, id))
return id;
}
}
it('should work with string argument', () => {
const schema = new schemas.Object({
data: new schemas.Object({
article: MyEntity,
}),
});
expect(
new MemoCache().buildQueryKey(schema, ['5'], {
entities: {
[MyEntity.key]: { '5': {} },
},
indexes: {},
}),
).toEqual({
data: { article: '5' },
});
});
it('should be undefined even when Entity.queryKey gives id if entity is not present', () => {
const schema = new schemas.Object({
data: new schemas.Object({
article: MyEntity,
}),
});
const memo = new MemoCache();
expect(
memo.buildQueryKey(schema, ['5'], { entities: {}, indexes: {} }),
).toEqual({
data: { article: undefined },
});
expect(
memo.buildQueryKey(schema, ['5'], {
entities: { [MyEntity.key]: { '5': { id: '5', title: 'hi' } } },
indexes: {},
}),
).toEqual({
data: { article: '5' },
});
});
describe('referential equality', () => {
const schema = new schemas.Object({
data: new schemas.Object({
article: MyEntity,
}),
});
const memo = new MemoCache();
const state = {
entities: { [MyEntity.key]: {} },
indexes: {},
};
const first = memo.buildQueryKey(schema, ['5'], state);
it('should maintain referential equality', () => {
expect(memo.buildQueryKey(schema, ['5'], state)).toBe(first);
});
it('should not change on index update if not used', () => {
expect(
memo.buildQueryKey(schema, ['5'], {
...state,
indexes: {
[MyEntity.key]: {},
},
}),
).toBe(first);
});
it('should be new when entity is updated', () => {
const withEntity = memo.buildQueryKey(schema, ['5'], {
entities: {
[MyEntity.key]: { '5': { id: '5', title: 'hi' } },
},
indexes: state.indexes,
});
expect(withEntity).not.toBe(first);
expect(withEntity.data).toEqual({ article: '5' });
});
it('should be the same if other entities are updated', () => {
const withEntity = memo.buildQueryKey(schema, ['5'], {
entities: {
...state.entities,
['another']: { '5': { id: '5', title: 'hi' } },
},
indexes: state.indexes,
});
expect(withEntity).toBe(first);
});
it('should be the same if other entities of the same type are updated', () => {
const withEntity = memo.buildQueryKey(schema, ['5'], {
entities: {
...state.entities,
[MyEntity.key]: {
...state.entities[MyEntity.key],
'500': { id: '500', title: 'second title' },