|
1 | 1 | // eslint-env jest |
2 | | -import { initialState } from '@data-client/core'; |
| 2 | +import { initialState, State } from '@data-client/core'; |
3 | 3 | import { normalize, denormalize, MemoCache } from '@data-client/normalizr'; |
4 | 4 | import { ArticleResource, IDEntity } from '__tests__/new'; |
5 | 5 | import { Record } from 'immutable'; |
@@ -227,6 +227,250 @@ describe(`${schema.Collection.name} normalization`, () => { |
227 | 227 | expect(result).toMatchSnapshot(); |
228 | 228 | expect(entities).toMatchSnapshot(); |
229 | 229 | }); |
| 230 | + |
| 231 | + test('remove works with Union members', () => { |
| 232 | + const collectionPK = collectionUnion.pk({}, {}, '', [ |
| 233 | + { completed: false }, |
| 234 | + ]); |
| 235 | + const init: State<unknown> = { |
| 236 | + ...initialState, |
| 237 | + entities: { |
| 238 | + [collectionUnion.key]: { |
| 239 | + [collectionPK]: [ |
| 240 | + { id: '1', schema: 'users' }, |
| 241 | + { id: '2', schema: 'groups' }, |
| 242 | + { id: '3', schema: 'groups' }, |
| 243 | + ], |
| 244 | + }, |
| 245 | + User: { |
| 246 | + '1': { |
| 247 | + id: '1', |
| 248 | + type: 'users', |
| 249 | + }, |
| 250 | + }, |
| 251 | + Group: { |
| 252 | + '2': { |
| 253 | + id: '2', |
| 254 | + type: 'groups', |
| 255 | + }, |
| 256 | + '3': { |
| 257 | + id: '3', |
| 258 | + type: 'groups', |
| 259 | + }, |
| 260 | + }, |
| 261 | + }, |
| 262 | + entitiesMeta: { |
| 263 | + [collectionUnion.key]: { |
| 264 | + [collectionPK]: { |
| 265 | + date: 1557831718135, |
| 266 | + expiresAt: Infinity, |
| 267 | + fetchedAt: 0, |
| 268 | + }, |
| 269 | + }, |
| 270 | + User: { |
| 271 | + '1': { |
| 272 | + date: 1557831718135, |
| 273 | + expiresAt: Infinity, |
| 274 | + fetchedAt: 0, |
| 275 | + }, |
| 276 | + }, |
| 277 | + Group: { |
| 278 | + '2': { |
| 279 | + date: 1557831718135, |
| 280 | + expiresAt: Infinity, |
| 281 | + fetchedAt: 0, |
| 282 | + }, |
| 283 | + '3': { |
| 284 | + date: 1557831718135, |
| 285 | + expiresAt: Infinity, |
| 286 | + fetchedAt: 0, |
| 287 | + }, |
| 288 | + }, |
| 289 | + }, |
| 290 | + indexes: {}, |
| 291 | + }; |
| 292 | + const state = normalize( |
| 293 | + collectionUnion.remove, |
| 294 | + { id: '1', type: 'users' }, |
| 295 | + [{ completed: false }], |
| 296 | + init, |
| 297 | + ); |
| 298 | + expect(state.entities[collectionUnion.key]?.[collectionPK]).toEqual([ |
| 299 | + { id: '2', schema: 'groups' }, |
| 300 | + { id: '3', schema: 'groups' }, |
| 301 | + ]); |
| 302 | + }); |
| 303 | + |
| 304 | + test('remove works updates the correct collections', () => { |
| 305 | + const init = { |
| 306 | + entities: { |
| 307 | + [collectionUnion.key]: { |
| 308 | + // Collection for userId: '1', completed: true |
| 309 | + '{"userId":"1","completed":"true"}': [ |
| 310 | + { id: '1', schema: 'users' }, |
| 311 | + { id: '2', schema: 'groups' }, |
| 312 | + ], |
| 313 | + // Collection for userId: '1', completed: false |
| 314 | + '{"userId":"1","completed":"false"}': [ |
| 315 | + { id: '3', schema: 'users' }, |
| 316 | + { id: '4', schema: 'groups' }, |
| 317 | + ], |
| 318 | + // Collection for userId: '1' (no completed filter) |
| 319 | + '{"userId":"1"}': [ |
| 320 | + { id: '1', schema: 'users' }, |
| 321 | + { id: '3', schema: 'users' }, |
| 322 | + ], |
| 323 | + // Collection for userId: '2' |
| 324 | + '{"userId":"2"}': [{ id: '5', schema: 'users' }], |
| 325 | + // Collection for userId: '1', completed: true, priority: high (additional arg) |
| 326 | + '{"userId":"1","completed":"true","priority":"high"}': [ |
| 327 | + { id: '1', schema: 'users' }, |
| 328 | + { id: '2', schema: 'groups' }, |
| 329 | + ], |
| 330 | + // Collection for userId: '1', completed: true, priority: low (additional arg) |
| 331 | + '{"userId":"1","completed":"true","priority":"low"}': [ |
| 332 | + { id: '3', schema: 'users' }, |
| 333 | + { id: '4', schema: 'groups' }, |
| 334 | + ], |
| 335 | + }, |
| 336 | + User: { |
| 337 | + '1': { |
| 338 | + id: '1', |
| 339 | + type: 'users', |
| 340 | + userId: '1', |
| 341 | + completed: true, |
| 342 | + priority: 'high', |
| 343 | + }, |
| 344 | + '3': { |
| 345 | + id: '3', |
| 346 | + type: 'users', |
| 347 | + userId: '1', |
| 348 | + completed: false, |
| 349 | + priority: 'low', |
| 350 | + }, |
| 351 | + '5': { |
| 352 | + id: '5', |
| 353 | + type: 'users', |
| 354 | + userId: '2', |
| 355 | + completed: false, |
| 356 | + }, |
| 357 | + }, |
| 358 | + Group: { |
| 359 | + '2': { |
| 360 | + id: '2', |
| 361 | + type: 'groups', |
| 362 | + userId: '1', |
| 363 | + completed: true, |
| 364 | + priority: 'high', |
| 365 | + }, |
| 366 | + '4': { |
| 367 | + id: '4', |
| 368 | + type: 'groups', |
| 369 | + userId: '1', |
| 370 | + completed: false, |
| 371 | + priority: 'low', |
| 372 | + }, |
| 373 | + }, |
| 374 | + }, |
| 375 | + entitiesMeta: { |
| 376 | + [collectionUnion.key]: { |
| 377 | + '{"userId":"1","completed":"true"}': { |
| 378 | + date: 1557831718135, |
| 379 | + expiresAt: Infinity, |
| 380 | + fetchedAt: 0, |
| 381 | + }, |
| 382 | + '{"userId":"1","completed":"false"}': { |
| 383 | + date: 1557831718135, |
| 384 | + expiresAt: Infinity, |
| 385 | + fetchedAt: 0, |
| 386 | + }, |
| 387 | + '{"userId":"1"}': { |
| 388 | + date: 1557831718135, |
| 389 | + expiresAt: Infinity, |
| 390 | + fetchedAt: 0, |
| 391 | + }, |
| 392 | + '{"userId":"2"}': { |
| 393 | + date: 1557831718135, |
| 394 | + expiresAt: Infinity, |
| 395 | + fetchedAt: 0, |
| 396 | + }, |
| 397 | + '{"userId":"1","completed":"true","priority":"high"}': { |
| 398 | + date: 1557831718135, |
| 399 | + expiresAt: Infinity, |
| 400 | + fetchedAt: 0, |
| 401 | + }, |
| 402 | + '{"userId":"1","completed":"true","priority":"low"}': { |
| 403 | + date: 1557831718135, |
| 404 | + expiresAt: Infinity, |
| 405 | + fetchedAt: 0, |
| 406 | + }, |
| 407 | + }, |
| 408 | + User: { |
| 409 | + '1': { date: 1557831718135, expiresAt: Infinity, fetchedAt: 0 }, |
| 410 | + '3': { date: 1557831718135, expiresAt: Infinity, fetchedAt: 0 }, |
| 411 | + '5': { date: 1557831718135, expiresAt: Infinity, fetchedAt: 0 }, |
| 412 | + }, |
| 413 | + Group: { |
| 414 | + '2': { date: 1557831718135, expiresAt: Infinity, fetchedAt: 0 }, |
| 415 | + '4': { date: 1557831718135, expiresAt: Infinity, fetchedAt: 0 }, |
| 416 | + }, |
| 417 | + }, |
| 418 | + indexes: {}, |
| 419 | + }; |
| 420 | + |
| 421 | + // Remove a union member with userId: '1' and completed: true |
| 422 | + // This should remove from collections that match userId: '1' and optionally have completed: true |
| 423 | + const state = normalize( |
| 424 | + collectionUnion.remove, |
| 425 | + { id: '1', type: 'users' }, |
| 426 | + [{ userId: '1', completed: true }], |
| 427 | + init, |
| 428 | + ); |
| 429 | + |
| 430 | + // Should remove from collections that match the args: |
| 431 | + // - '{"userId":"1","completed":"true"}' (exact match) |
| 432 | + // - '{"userId":"1"}' (partial match - has userId: '1', missing completed) |
| 433 | + // Should NOT remove from: |
| 434 | + // - '{"userId":"1","completed":"false"}' (conflicting completed value) |
| 435 | + // - '{"userId":"2"}' (different userId) |
| 436 | + // - '{"userId":"1","completed":"true","priority":"high"}' (has additional priority arg) |
| 437 | + // - '{"userId":"1","completed":"true","priority":"low"}' (has additional priority arg) |
| 438 | + expect( |
| 439 | + state.entities[collectionUnion.key][ |
| 440 | + '{"userId":"1","completed":"true"}' |
| 441 | + ], |
| 442 | + ).toEqual([{ id: '2', schema: 'groups' }]); |
| 443 | + expect( |
| 444 | + state.entities[collectionUnion.key][ |
| 445 | + '{"userId":"1","completed":"false"}' |
| 446 | + ], |
| 447 | + ).toEqual([ |
| 448 | + { id: '3', schema: 'users' }, |
| 449 | + { id: '4', schema: 'groups' }, |
| 450 | + ]); |
| 451 | + expect(state.entities[collectionUnion.key]['{"userId":"1"}']).toEqual([ |
| 452 | + { id: '3', schema: 'users' }, |
| 453 | + ]); |
| 454 | + expect(state.entities[collectionUnion.key]['{"userId":"2"}']).toEqual([ |
| 455 | + { id: '5', schema: 'users' }, |
| 456 | + ]); |
| 457 | + expect( |
| 458 | + state.entities[collectionUnion.key][ |
| 459 | + '{"userId":"1","completed":"true","priority":"high"}' |
| 460 | + ], |
| 461 | + ).toEqual([ |
| 462 | + { id: '1', schema: 'users' }, |
| 463 | + { id: '2', schema: 'groups' }, |
| 464 | + ]); // Should remain unchanged (has additional priority arg) |
| 465 | + expect( |
| 466 | + state.entities[collectionUnion.key][ |
| 467 | + '{"userId":"1","completed":"true","priority":"low"}' |
| 468 | + ], |
| 469 | + ).toEqual([ |
| 470 | + { id: '3', schema: 'users' }, |
| 471 | + { id: '4', schema: 'groups' }, |
| 472 | + ]); // Should remain unchanged (has additional priority arg) |
| 473 | + }); |
230 | 474 | }); |
231 | 475 |
|
232 | 476 | test('normalizes push onto the end', () => { |
@@ -343,7 +587,9 @@ describe(`${schema.Collection.name} normalization`, () => { |
343 | 587 | [{ userId: '1' }], |
344 | 588 | init, |
345 | 589 | ); |
346 | | - expect(state).toMatchSnapshot(); |
| 590 | + expect(state.entities[User.schema.todos.key]['{"userId":"1"}']).toEqual([ |
| 591 | + '6', |
| 592 | + ]); |
347 | 593 | }); |
348 | 594 |
|
349 | 595 | test('normalizes remove from collection with multiple items', () => { |
@@ -415,7 +661,10 @@ describe(`${schema.Collection.name} normalization`, () => { |
415 | 661 | [{ userId: '1' }], |
416 | 662 | init, |
417 | 663 | ); |
418 | | - expect(state).toMatchSnapshot(); |
| 664 | + expect(state.entities[User.schema.todos.key]['{"userId":"1"}']).toEqual([ |
| 665 | + '5', |
| 666 | + '7', |
| 667 | + ]); |
419 | 668 | }); |
420 | 669 |
|
421 | 670 | test('normalizes remove non-existent item from collection', () => { |
@@ -469,7 +718,9 @@ describe(`${schema.Collection.name} normalization`, () => { |
469 | 718 | [{ userId: '1' }], |
470 | 719 | init, |
471 | 720 | ); |
472 | | - expect(state).toMatchSnapshot(); |
| 721 | + expect(state.entities[User.schema.todos.key]['{"userId":"1"}']).toEqual([ |
| 722 | + '5', |
| 723 | + ]); |
473 | 724 | }); |
474 | 725 |
|
475 | 726 | describe('push should add only to collections matching filterArgumentKeys', () => { |
|
0 commit comments