Skip to content

Commit fc7b18e

Browse files
committed
fix: Collection.remove
1 parent 92bd01c commit fc7b18e

File tree

3 files changed

+89
-165
lines changed

3 files changed

+89
-165
lines changed

packages/endpoint/src/schemas/Collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export default class CollectionSchema<
143143
toJSON() {
144144
return {
145145
key: this.key,
146-
schema: this.schema.schema.toJSON(),
146+
schema: this.schema.schema,
147147
};
148148
}
149149

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

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,61 @@ describe(`${schema.Collection.name} normalization`, () => {
227227
expect(result).toMatchSnapshot();
228228
expect(entities).toMatchSnapshot();
229229
});
230+
231+
test('remove works with Union members', () => {
232+
const init = {
233+
entities: {
234+
[collectionUnion.key]: {
235+
'{"fakeFilter":false}': ['1', '2'],
236+
},
237+
User: {
238+
'1': {
239+
id: '1',
240+
type: 'users',
241+
},
242+
},
243+
Group: {
244+
'2': {
245+
id: '2',
246+
type: 'groups',
247+
},
248+
},
249+
},
250+
entitiesMeta: {
251+
[collectionUnion.key]: {
252+
'{"fakeFilter":false}': {
253+
date: 1557831718135,
254+
expiresAt: Infinity,
255+
fetchedAt: 0,
256+
},
257+
},
258+
User: {
259+
'1': {
260+
date: 1557831718135,
261+
expiresAt: Infinity,
262+
fetchedAt: 0,
263+
},
264+
},
265+
Group: {
266+
'2': {
267+
date: 1557831718135,
268+
expiresAt: Infinity,
269+
fetchedAt: 0,
270+
},
271+
},
272+
},
273+
indexes: {},
274+
};
275+
const state = normalize(
276+
collectionUnion.remove,
277+
{ id: '1', type: 'users' },
278+
[{ fakeFilter: false }],
279+
init,
280+
);
281+
expect(
282+
state.entities[collectionUnion.key]['{"fakeFilter":false}'],
283+
).toEqual(['2']);
284+
});
230285
});
231286

232287
test('normalizes push onto the end', () => {
@@ -343,7 +398,9 @@ describe(`${schema.Collection.name} normalization`, () => {
343398
[{ userId: '1' }],
344399
init,
345400
);
346-
expect(state).toMatchSnapshot();
401+
expect(state.entities[User.schema.todos.key]['{"userId":"1"}']).toEqual([
402+
'6',
403+
]);
347404
});
348405

349406
test('normalizes remove from collection with multiple items', () => {
@@ -415,7 +472,10 @@ describe(`${schema.Collection.name} normalization`, () => {
415472
[{ userId: '1' }],
416473
init,
417474
);
418-
expect(state).toMatchSnapshot();
475+
expect(state.entities[User.schema.todos.key]['{"userId":"1"}']).toEqual([
476+
'5',
477+
'7',
478+
]);
419479
});
420480

421481
test('normalizes remove non-existent item from collection', () => {
@@ -469,7 +529,9 @@ describe(`${schema.Collection.name} normalization`, () => {
469529
[{ userId: '1' }],
470530
init,
471531
);
472-
expect(state).toMatchSnapshot();
532+
expect(state.entities[User.schema.todos.key]['{"userId":"1"}']).toEqual([
533+
'5',
534+
]);
473535
});
474536

475537
describe('push should add only to collections matching filterArgumentKeys', () => {

packages/endpoint/src/schemas/__tests__/__snapshots__/Collection.test.ts.snap

Lines changed: 23 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -178,29 +178,18 @@ exports[`CollectionSchema normalization normalizes push onto the end 1`] = `
178178
}
179179
`;
180180

181-
exports[`CollectionSchema normalization normalizes remove from collection 1`] = `
181+
exports[`CollectionSchema normalization normalizes top level collections (no args) 1`] = `
182182
{
183183
"entities": {
184184
"Todo": {
185185
"5": {
186186
"id": "5",
187187
"title": "finish collections",
188188
},
189-
"6": {
190-
"id": "6",
191-
"title": "another todo",
192-
},
193-
},
194-
"User": {
195-
"1": {
196-
"id": "1",
197-
"todos": "{"userId":"1"}",
198-
"username": "bob",
199-
},
200189
},
201190
"[Todo]": {
202191
"{"userId":"1"}": [
203-
"6",
192+
"5",
204193
],
205194
},
206195
},
@@ -211,18 +200,6 @@ exports[`CollectionSchema normalization normalizes remove from collection 1`] =
211200
"expiresAt": Infinity,
212201
"fetchedAt": 0,
213202
},
214-
"6": {
215-
"date": 1557831718135,
216-
"expiresAt": Infinity,
217-
"fetchedAt": 0,
218-
},
219-
},
220-
"User": {
221-
"1": {
222-
"date": 1557831718135,
223-
"expiresAt": Infinity,
224-
"fetchedAt": 0,
225-
},
226203
},
227204
"[Todo]": {
228205
"{"userId":"1"}": {
@@ -233,40 +210,22 @@ exports[`CollectionSchema normalization normalizes remove from collection 1`] =
233210
},
234211
},
235212
"indexes": {},
236-
"result": [
237-
"5",
238-
],
213+
"result": "{"userId":"1"}",
239214
}
240215
`;
241216

242-
exports[`CollectionSchema normalization normalizes remove from collection with multiple items 1`] = `
217+
exports[`CollectionSchema normalization normalizes top level collections 1`] = `
243218
{
244219
"entities": {
245220
"Todo": {
246221
"5": {
247222
"id": "5",
248223
"title": "finish collections",
249224
},
250-
"6": {
251-
"id": "6",
252-
"title": "another todo",
253-
},
254-
"7": {
255-
"id": "7",
256-
"title": "third todo",
257-
},
258-
},
259-
"User": {
260-
"1": {
261-
"id": "1",
262-
"todos": "{"userId":"1"}",
263-
"username": "bob",
264-
},
265225
},
266226
"[Todo]": {
267227
"{"userId":"1"}": [
268228
"5",
269-
"7",
270229
],
271230
},
272231
},
@@ -277,23 +236,6 @@ exports[`CollectionSchema normalization normalizes remove from collection with m
277236
"expiresAt": Infinity,
278237
"fetchedAt": 0,
279238
},
280-
"6": {
281-
"date": 1557831718135,
282-
"expiresAt": Infinity,
283-
"fetchedAt": 0,
284-
},
285-
"7": {
286-
"date": 1557831718135,
287-
"expiresAt": Infinity,
288-
"fetchedAt": 0,
289-
},
290-
},
291-
"User": {
292-
"1": {
293-
"date": 1557831718135,
294-
"expiresAt": Infinity,
295-
"fetchedAt": 0,
296-
},
297239
},
298240
"[Todo]": {
299241
"{"userId":"1"}": {
@@ -304,46 +246,35 @@ exports[`CollectionSchema normalization normalizes remove from collection with m
304246
},
305247
},
306248
"indexes": {},
307-
"result": [
308-
"6",
309-
],
249+
"result": "{"userId":"1"}",
310250
}
311251
`;
312252

313-
exports[`CollectionSchema normalization normalizes remove non-existent item from collection 1`] = `
253+
exports[`CollectionSchema normalization polymorphism remove works with Union members 1`] = `
314254
{
315255
"entities": {
316-
"Todo": {
317-
"5": {
318-
"id": "5",
319-
"title": "finish collections",
320-
},
321-
"99": {
322-
"id": "99",
323-
"title": "non-existent todo",
256+
"Group": {
257+
"2": {
258+
"id": "2",
259+
"type": "groups",
324260
},
325261
},
326262
"User": {
327263
"1": {
328264
"id": "1",
329-
"todos": "{"userId":"1"}",
330-
"username": "bob",
265+
"type": "users",
331266
},
332267
},
333-
"[Todo]": {
334-
"{"userId":"1"}": [
335-
"5",
268+
"[User;Group]": {
269+
"{"fakeFilter":false}": [
270+
"1",
271+
"2",
336272
],
337273
},
338274
},
339275
"entitiesMeta": {
340-
"Todo": {
341-
"5": {
342-
"date": 1557831718135,
343-
"expiresAt": Infinity,
344-
"fetchedAt": 0,
345-
},
346-
"99": {
276+
"Group": {
277+
"2": {
347278
"date": 1557831718135,
348279
"expiresAt": Infinity,
349280
"fetchedAt": 0,
@@ -356,8 +287,8 @@ exports[`CollectionSchema normalization normalizes remove non-existent item from
356287
"fetchedAt": 0,
357288
},
358289
},
359-
"[Todo]": {
360-
"{"userId":"1"}": {
290+
"[User;Group]": {
291+
"{"fakeFilter":false}": {
361292
"date": 1557831718135,
362293
"expiresAt": Infinity,
363294
"fetchedAt": 0,
@@ -366,80 +297,11 @@ exports[`CollectionSchema normalization normalizes remove non-existent item from
366297
},
367298
"indexes": {},
368299
"result": [
369-
"99",
370-
],
371-
}
372-
`;
373-
374-
exports[`CollectionSchema normalization normalizes top level collections (no args) 1`] = `
375-
{
376-
"entities": {
377-
"Todo": {
378-
"5": {
379-
"id": "5",
380-
"title": "finish collections",
381-
},
382-
},
383-
"[Todo]": {
384-
"{"userId":"1"}": [
385-
"5",
386-
],
387-
},
388-
},
389-
"entitiesMeta": {
390-
"Todo": {
391-
"5": {
392-
"date": 1557831718135,
393-
"expiresAt": Infinity,
394-
"fetchedAt": 0,
395-
},
396-
},
397-
"[Todo]": {
398-
"{"userId":"1"}": {
399-
"date": 1557831718135,
400-
"expiresAt": Infinity,
401-
"fetchedAt": 0,
402-
},
403-
},
404-
},
405-
"indexes": {},
406-
"result": "{"userId":"1"}",
407-
}
408-
`;
409-
410-
exports[`CollectionSchema normalization normalizes top level collections 1`] = `
411-
{
412-
"entities": {
413-
"Todo": {
414-
"5": {
415-
"id": "5",
416-
"title": "finish collections",
417-
},
418-
},
419-
"[Todo]": {
420-
"{"userId":"1"}": [
421-
"5",
422-
],
423-
},
424-
},
425-
"entitiesMeta": {
426-
"Todo": {
427-
"5": {
428-
"date": 1557831718135,
429-
"expiresAt": Infinity,
430-
"fetchedAt": 0,
431-
},
432-
},
433-
"[Todo]": {
434-
"{"userId":"1"}": {
435-
"date": 1557831718135,
436-
"expiresAt": Infinity,
437-
"fetchedAt": 0,
438-
},
300+
{
301+
"id": "1",
302+
"schema": "users",
439303
},
440-
},
441-
"indexes": {},
442-
"result": "{"userId":"1"}",
304+
],
443305
}
444306
`;
445307

0 commit comments

Comments
 (0)