-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathInvalidate.test.ts
More file actions
197 lines (178 loc) · 5.65 KB
/
Invalidate.test.ts
File metadata and controls
197 lines (178 loc) · 5.65 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
// eslint-env jest
import { INVALID, Schema, normalize } from '@data-client/normalizr';
import { IDEntity } from '__tests__/new';
import { fromJS } from 'immutable';
import { SimpleMemoCache, fromJSEntities } from './denormalize';
import { schema } from '../..';
import Entity from '../Entity';
let dateSpy: jest.SpyInstance;
beforeAll(() => {
dateSpy = jest
.spyOn(global.Date, 'now')
.mockImplementation(() => new Date('2019-05-14T11:01:58.135Z').valueOf());
});
afterAll(() => {
dateSpy.mockRestore();
});
describe(`${schema.Invalidate.name} normalization`, () => {
test('throws if not given an entity', () => {
// @ts-expect-error
expect(() => new schema.Invalidate()).toThrow();
});
test('normalizes an object', () => {
class User extends IDEntity {}
expect(
normalize(new schema.Invalidate(User), { id: '1', type: 'users' }),
).toMatchSnapshot();
});
test('normalizes already processed entities', () => {
class MyEntity extends IDEntity {}
expect(normalize(new schema.Invalidate(MyEntity), '1')).toMatchSnapshot();
expect(
normalize(new schema.Array(new schema.Invalidate(MyEntity)), ['1', '2']),
).toMatchSnapshot();
});
test('does not query', () => {
class User extends IDEntity {}
expect(
new schema.Invalidate(User).queryKey([{ id: 5 }], () => undefined, {
getEntity: () => undefined,
getIndex: () => undefined,
}),
).toBeUndefined();
});
it('should throw a custom error if data does not include pk', () => {
class MyEntity extends Entity {
readonly name: string = '';
readonly secondthing: string = '';
pk() {
return this.name;
}
}
function normalizeBad() {
normalize(new schema.Invalidate(MyEntity), { secondthing: 'hi' });
}
expect(normalizeBad).toThrowErrorMatchingSnapshot();
});
it('should throw a custom error if data does not include pk (serializes pk)', () => {
class MyEntity extends Entity {
readonly name: string = '';
readonly secondthing: string = '';
pk() {
return `${this.name}`;
}
}
function normalizeBad() {
normalize(new schema.Invalidate(MyEntity), { secondthing: 'hi' });
}
expect(normalizeBad).toThrowErrorMatchingSnapshot();
});
});
describe(`${schema.Invalidate.name} denormalization`, () => {
class User extends IDEntity {
readonly username: string = '';
readonly type: string = '';
}
const entities = {
User: {
'1': User.fromJS({ id: '1', username: 'Janey', type: 'users' }),
},
};
test('denormalizes an object in the same manner as the Entity', () => {
const user = new SimpleMemoCache().denormalize(
new schema.Invalidate(User),
'1',
entities,
);
expect(user).not.toEqual(expect.any(Symbol));
if (typeof user === 'symbol') return;
expect(user).toBeDefined();
expect(user).toBeInstanceOf(User);
expect(user?.username).toBe('Janey');
});
describe.each([
['direct', <T>(data: T) => data, <T>(data: T) => data],
['immutable', fromJS, fromJSEntities],
])(`input (%s)`, (_, createInput, createEntities) => {
describe.each([
[
'class',
<T extends Schema>(sch: T) => new schema.Array(sch),
<T extends Record<string, any>>(sch: T) => new schema.Object(sch),
new SimpleMemoCache().denormalize,
],
[
'object, direct',
<T extends Schema>(sch: T) => [sch],
<T extends Record<string, any>>(sch: T) => sch,
new SimpleMemoCache().denormalize,
],
])(
`schema construction (%s)`,
(_, createArray, createObject, denormalize) => {
test('denormalizes deleted entities as symbol', () => {
const user = denormalize(
new schema.Invalidate(User),
'1',
createInput({
User: { '1': INVALID },
}),
);
expect(user).toEqual(expect.any(Symbol));
expect(
denormalize(
createObject({ data: new schema.Invalidate(User) }),
createInput({ data: '1' }),
createEntities({
User: { '1': INVALID },
}),
),
).toEqual(expect.any(Symbol));
});
test('denormalize removes deleted entries in array', () => {
expect(
denormalize(
createArray(createObject({ data: new schema.Invalidate(User) })),
createInput([{ data: '1' }]),
createEntities({
User: { '1': INVALID },
}),
),
).toMatchSnapshot();
expect(
denormalize(
createArray(createObject({ data: User })),
createInput([{ data: '1' }]),
createEntities({
User: { '1': INVALID },
}),
),
).toMatchSnapshot();
});
test('denormalize sets undefined entities that are not present', () => {
expect(
denormalize(
createArray(createObject({ data: new schema.Invalidate(User) })),
createInput([{ data: '1' }]),
createEntities({}),
),
).toMatchSnapshot();
expect(
denormalize(
createArray(createObject({ data: User })),
createInput([{ data: '1' }]),
createEntities({}),
),
).toMatchSnapshot();
expect(
denormalize(
createObject({ data: User }),
createInput({ data: '1' }),
createEntities({}),
),
).toMatchSnapshot();
});
},
);
});
});