Skip to content

Commit 37112ff

Browse files
committed
for denorm
1 parent f229a28 commit 37112ff

38 files changed

+596
-2047
lines changed

packages/endpoint/src/schemas/__tests__/All.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import { initialState, State, Controller } from '@data-client/core';
33
import {
44
normalize,
55
MemoCache,
6-
denormalize,
6+
denormalize as plainDenormalize,
77
INVALID,
8-
PlainDelegate,
8+
Delegate as PlainDelegate,
99
} from '@data-client/normalizr';
10-
import { DelegateImmutable } from '@data-client/normalizr/immutable';
10+
import {
11+
Delegate as DelegateImmutable,
12+
denormalize as immDenormalize,
13+
} from '@data-client/normalizr/immutable';
1114
import { IDEntity } from '__tests__/new';
1215

1316
import { schema } from '../..';
@@ -103,16 +106,23 @@ describe.each([[]])(`${schema.All.name} normalization (%s)`, () => {
103106
});
104107

105108
describe.each([
106-
['direct', <T>(data: T) => data, <T>(data: T) => data, PlainDelegate],
109+
[
110+
'direct',
111+
<T>(data: T) => data,
112+
<T>(data: T) => data,
113+
PlainDelegate,
114+
plainDenormalize,
115+
],
107116
[
108117
'immutable',
109118
fromJSState,
110119
(v: any) => (typeof v?.toJS === 'function' ? v.toJS() : v),
111120
DelegateImmutable,
121+
immDenormalize,
112122
],
113123
])(
114124
`${schema.Array.name} denormalization (%s)`,
115-
(_, createInput, createOutput, MyDelegate) => {
125+
(_, createInput, createOutput, MyDelegate, denormalize) => {
116126
test('denormalizes a single entity', () => {
117127
class Cat extends IDEntity {}
118128
const state: State<unknown> = createInput({

packages/endpoint/src/schemas/__tests__/Array.test.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// eslint-env jest
2-
import { normalize, denormalize } from '@data-client/normalizr';
2+
import {
3+
normalize,
4+
denormalize as plainDenormalize,
5+
} from '@data-client/normalizr';
6+
import { denormalize as immDenormalize } from '@data-client/normalizr/immutable';
37
import { IDEntity } from '__tests__/new';
48
import { fromJS } from 'immutable';
59

@@ -138,9 +142,9 @@ describe.each([
138142
});
139143

140144
describe.each([
141-
['direct', data => data, data => data],
142-
['immutable', fromJS, fromJSEntities],
143-
])(`input (%s)`, (_, createInput, createEntities) => {
145+
['direct', data => data, data => data, plainDenormalize],
146+
['immutable', fromJS, fromJSEntities, immDenormalize],
147+
])(`input (%s)`, (_, createInput, createEntities, denormalize) => {
144148
test('denormalizes plain arrays with nothing inside', () => {
145149
class User extends IDEntity {}
146150
const entities = {
@@ -155,7 +159,6 @@ describe.each([
155159
expect(
156160
denormalize(sch, createInput({ user: '1' }), createEntities(entities)),
157161
).toMatchSnapshot();
158-
159162
expect(
160163
denormalize(sch, { user: '1', tacos: [] }, createEntities(entities)),
161164
).toMatchSnapshot();
@@ -416,9 +419,9 @@ describe.each([
416419
];
417420
const output = normalize(catList, input);
418421
expect(output).toMatchSnapshot();
419-
expect(denormalize(catList, output.result, output.entities)).toEqual(
420-
input,
421-
);
422+
expect(
423+
denormalize(catList, output.result, createEntities(output.entities)),
424+
).toEqual(input);
422425
});
423426
});
424427
});

packages/endpoint/src/schemas/__tests__/Entity.test.ts

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// eslint-env jest
2-
import { normalize, denormalize } from '@data-client/normalizr';
3-
import { INVALID } from '@data-client/normalizr';
2+
import { normalize, INVALID } from '@data-client/normalizr';
3+
import { denormalize as plainDenormalize } from '@data-client/normalizr';
4+
import { denormalize as immDenormalize } from '@data-client/normalizr/immutable';
45
import { Temporal } from '@js-temporal/polyfill';
56
import { IDEntity } from '__tests__/new';
67

@@ -632,8 +633,10 @@ describe(`${Entity.name} denormalization`, () => {
632633
'1': { id: '1', name: 'foo' },
633634
},
634635
};
635-
expect(denormalize(Tacos, '1', entities)).toMatchSnapshot();
636-
expect(denormalize(Tacos, '1', fromJSEntities(entities))).toMatchSnapshot();
636+
expect(plainDenormalize(Tacos, '1', entities)).toMatchSnapshot();
637+
expect(
638+
immDenormalize(Tacos, '1', fromJSEntities(entities)),
639+
).toMatchSnapshot();
637640
});
638641

639642
class Food extends Entity {}
@@ -654,13 +657,13 @@ describe(`${Entity.name} denormalization`, () => {
654657
},
655658
};
656659

657-
const de1 = denormalize(Menu, '1', entities);
660+
const de1 = plainDenormalize(Menu, '1', entities);
658661
expect(de1).toMatchSnapshot();
659-
expect(denormalize(Menu, '1', fromJSEntities(entities))).toEqual(de1);
662+
expect(immDenormalize(Menu, '1', fromJSEntities(entities))).toEqual(de1);
660663

661-
const de2 = denormalize(Menu, '2', entities);
664+
const de2 = plainDenormalize(Menu, '2', entities);
662665
expect(de2).toMatchSnapshot();
663-
expect(denormalize(Menu, '2', fromJSEntities(entities))).toEqual(de2);
666+
expect(immDenormalize(Menu, '2', fromJSEntities(entities))).toEqual(de2);
664667
});
665668

666669
test('denormalizes deep entities while maintaining referential equality', () => {
@@ -687,7 +690,7 @@ describe(`${Entity.name} denormalization`, () => {
687690

688691
test('denormalizes to undefined when validate() returns string', () => {
689692
class MyTacos extends Tacos {
690-
static validate(entity) {
693+
static validate(entity: any) {
691694
if (!Object.hasOwn(entity, 'name')) return 'no name';
692695
}
693696
}
@@ -696,8 +699,10 @@ describe(`${Entity.name} denormalization`, () => {
696699
'1': { id: '1' },
697700
},
698701
};
699-
expect(denormalize(MyTacos, '1', entities)).toEqual(expect.any(Symbol));
700-
expect(denormalize(MyTacos, '1', fromJSEntities(entities))).toEqual(
702+
expect(plainDenormalize(MyTacos, '1', entities)).toEqual(
703+
expect.any(Symbol),
704+
);
705+
expect(immDenormalize(MyTacos, '1', fromJSEntities(entities))).toEqual(
701706
expect.any(Symbol),
702707
);
703708
});
@@ -712,11 +717,15 @@ describe(`${Entity.name} denormalization`, () => {
712717
},
713718
};
714719

715-
expect(denormalize(Menu, '1', entities)).toMatchSnapshot();
716-
expect(denormalize(Menu, '1', fromJSEntities(entities))).toMatchSnapshot();
720+
expect(plainDenormalize(Menu, '1', entities)).toMatchSnapshot();
721+
expect(
722+
immDenormalize(Menu, '1', fromJSEntities(entities)),
723+
).toMatchSnapshot();
717724

718-
expect(denormalize(Menu, '2', entities)).toMatchSnapshot();
719-
expect(denormalize(Menu, '2', fromJSEntities(entities))).toMatchSnapshot();
725+
expect(plainDenormalize(Menu, '2', entities)).toMatchSnapshot();
726+
expect(
727+
immDenormalize(Menu, '2', fromJSEntities(entities)),
728+
).toMatchSnapshot();
720729
});
721730

722731
it('should handle optional schema entries Entity', () => {
@@ -735,7 +744,7 @@ describe(`${Entity.name} denormalization`, () => {
735744
const schema = MyEntity;
736745

737746
expect(
738-
denormalize(schema, 'bob', {
747+
plainDenormalize(schema, 'bob', {
739748
MyEntity: { bob: { name: 'bob', secondthing: 'hi' } },
740749
}),
741750
).toMatchInlineSnapshot(`
@@ -763,7 +772,7 @@ describe(`${Entity.name} denormalization`, () => {
763772
const schema = MyEntity;
764773

765774
expect(
766-
denormalize(schema, 'bob', {
775+
plainDenormalize(schema, 'bob', {
767776
MyEntity: { bob: { name: 'bob', secondthing: 'hi', blarb: null } },
768777
}),
769778
).toMatchInlineSnapshot(`
@@ -787,11 +796,15 @@ describe(`${Entity.name} denormalization`, () => {
787796
},
788797
};
789798

790-
expect(denormalize(Menu, '1', entities)).toMatchSnapshot();
791-
expect(denormalize(Menu, '1', fromJSEntities(entities))).toMatchSnapshot();
799+
expect(plainDenormalize(Menu, '1', entities)).toMatchSnapshot();
800+
expect(
801+
immDenormalize(Menu, '1', fromJSEntities(entities)),
802+
).toMatchSnapshot();
792803

793-
expect(denormalize(Menu, '2', entities)).toMatchSnapshot();
794-
expect(denormalize(Menu, '2', fromJSEntities(entities))).toMatchSnapshot();
804+
expect(plainDenormalize(Menu, '2', entities)).toMatchSnapshot();
805+
expect(
806+
immDenormalize(Menu, '2', fromJSEntities(entities)),
807+
).toMatchSnapshot();
795808
});
796809

797810
test('can denormalize already partially denormalized data', () => {
@@ -805,8 +818,10 @@ describe(`${Entity.name} denormalization`, () => {
805818
},
806819
};
807820

808-
expect(denormalize(Menu, '1', entities)).toMatchSnapshot();
809-
expect(denormalize(Menu, '1', fromJSEntities(entities))).toMatchSnapshot();
821+
expect(plainDenormalize(Menu, '1', entities)).toMatchSnapshot();
822+
expect(
823+
immDenormalize(Menu, '1', fromJSEntities(entities)),
824+
).toMatchSnapshot();
810825
});
811826

812827
class User extends IDEntity {
@@ -854,14 +869,14 @@ describe(`${Entity.name} denormalization`, () => {
854869
},
855870
};
856871

857-
expect(denormalize(Report, '123', entities)).toMatchSnapshot();
872+
expect(plainDenormalize(Report, '123', entities)).toMatchSnapshot();
858873
expect(
859-
denormalize(Report, '123', fromJSEntities(entities)),
874+
immDenormalize(Report, '123', fromJSEntities(entities)),
860875
).toMatchSnapshot();
861876

862-
expect(denormalize(User, '456', entities)).toMatchSnapshot();
877+
expect(plainDenormalize(User, '456', entities)).toMatchSnapshot();
863878
expect(
864-
denormalize(User, '456', fromJSEntities(entities)),
879+
immDenormalize(User, '456', fromJSEntities(entities)),
865880
).toMatchSnapshot();
866881
});
867882

@@ -1000,7 +1015,7 @@ describe(`${Entity.name} denormalization`, () => {
10001015

10011016
describe('optional entities', () => {
10021017
it('should be marked as found even when optional is not there', () => {
1003-
const denormalized = denormalize(WithOptional, 'abc', {
1018+
const denormalized = plainDenormalize(WithOptional, 'abc', {
10041019
[WithOptional.key]: {
10051020
abc: {
10061021
id: 'abc',
@@ -1025,7 +1040,7 @@ describe(`${Entity.name} denormalization`, () => {
10251040
});
10261041

10271042
it('should be marked as found when nested entity is missing', () => {
1028-
const denormalized = denormalize(WithOptional, 'abc', {
1043+
const denormalized = plainDenormalize(WithOptional, 'abc', {
10291044
[WithOptional.key]: {
10301045
abc: WithOptional.fromJS({
10311046
id: 'abc',
@@ -1051,7 +1066,7 @@ describe(`${Entity.name} denormalization`, () => {
10511066
});
10521067

10531068
it('should be marked as deleted when required entity is deleted symbol', () => {
1054-
const denormalized = denormalize(WithOptional, 'abc', {
1069+
const denormalized = plainDenormalize(WithOptional, 'abc', {
10551070
[WithOptional.key]: {
10561071
abc: {
10571072
id: 'abc',
@@ -1068,7 +1083,7 @@ describe(`${Entity.name} denormalization`, () => {
10681083
});
10691084

10701085
it('should be non-required deleted members should not result in deleted indicator', () => {
1071-
const denormalized = denormalize(WithOptional, 'abc', {
1086+
const denormalized = plainDenormalize(WithOptional, 'abc', {
10721087
[WithOptional.key]: {
10731088
abc: WithOptional.fromJS({
10741089
id: 'abc',
@@ -1096,7 +1111,7 @@ describe(`${Entity.name} denormalization`, () => {
10961111
});
10971112

10981113
it('should be deleted when both are true in different parts of schema', () => {
1099-
const denormalized = denormalize(
1114+
const denormalized = plainDenormalize(
11001115
new schema.Object({ data: WithOptional, other: ArticleEntity }),
11011116
{ data: 'abc' },
11021117
{

0 commit comments

Comments
 (0)