|
1 |
| -import { vi } from 'vitest' |
| 1 | +import { expect, vi } from 'vitest' |
2 | 2 | import { QueryCache } from '../queryCache'
|
3 | 3 | import { dehydrate, hydrate } from '../hydration'
|
| 4 | +import { MutationCache } from '../mutationCache' |
4 | 5 | import {
|
5 | 6 | createQueryClient,
|
6 | 7 | executeMutation,
|
@@ -557,4 +558,114 @@ describe('dehydration and rehydration', () => {
|
557 | 558 | hydrationCache.find({ queryKey: ['string'] })?.state.fetchStatus,
|
558 | 559 | ).toBe('idle')
|
559 | 560 | })
|
| 561 | + |
| 562 | + test('should dehydrate and hydrate meta for queries', async () => { |
| 563 | + const queryCache = new QueryCache() |
| 564 | + const queryClient = createQueryClient({ queryCache }) |
| 565 | + await queryClient.prefetchQuery({ |
| 566 | + queryKey: ['meta'], |
| 567 | + queryFn: () => Promise.resolve('meta'), |
| 568 | + meta: { |
| 569 | + some: 'meta', |
| 570 | + }, |
| 571 | + }) |
| 572 | + await queryClient.prefetchQuery({ |
| 573 | + queryKey: ['no-meta'], |
| 574 | + queryFn: () => Promise.resolve('no-meta'), |
| 575 | + }) |
| 576 | + |
| 577 | + const dehydrated = dehydrate(queryClient) |
| 578 | + |
| 579 | + expect( |
| 580 | + dehydrated.queries.find((q) => q.queryHash === '["meta"]')?.meta, |
| 581 | + ).toEqual({ |
| 582 | + some: 'meta', |
| 583 | + }) |
| 584 | + |
| 585 | + expect( |
| 586 | + dehydrated.queries.find((q) => q.queryHash === '["no-meta"]')?.meta, |
| 587 | + ).toEqual(undefined) |
| 588 | + |
| 589 | + expect( |
| 590 | + Object.keys( |
| 591 | + dehydrated.queries.find((q) => q.queryHash === '["no-meta"]')!, |
| 592 | + ), |
| 593 | + ).not.toEqual(expect.arrayContaining(['meta'])) |
| 594 | + |
| 595 | + const stringified = JSON.stringify(dehydrated) |
| 596 | + |
| 597 | + // --- |
| 598 | + |
| 599 | + const parsed = JSON.parse(stringified) |
| 600 | + const hydrationCache = new QueryCache() |
| 601 | + const hydrationClient = createQueryClient({ |
| 602 | + queryCache: hydrationCache, |
| 603 | + }) |
| 604 | + hydrate(hydrationClient, parsed) |
| 605 | + expect(hydrationCache.find({ queryKey: ['meta'] })?.meta).toEqual({ |
| 606 | + some: 'meta', |
| 607 | + }) |
| 608 | + expect(hydrationCache.find({ queryKey: ['no-meta'] })?.meta).toEqual( |
| 609 | + undefined, |
| 610 | + ) |
| 611 | + }) |
| 612 | + |
| 613 | + test('should dehydrate and hydrate meta for mutations', async () => { |
| 614 | + const mutationCache = new MutationCache() |
| 615 | + const queryClient = createQueryClient({ mutationCache }) |
| 616 | + |
| 617 | + await executeMutation( |
| 618 | + queryClient, |
| 619 | + { |
| 620 | + mutationKey: ['meta'], |
| 621 | + mutationFn: () => Promise.resolve('meta'), |
| 622 | + meta: { |
| 623 | + some: 'meta', |
| 624 | + }, |
| 625 | + }, |
| 626 | + undefined, |
| 627 | + ) |
| 628 | + |
| 629 | + await executeMutation( |
| 630 | + queryClient, |
| 631 | + { |
| 632 | + mutationKey: ['no-meta'], |
| 633 | + mutationFn: () => Promise.resolve('no-meta'), |
| 634 | + }, |
| 635 | + undefined, |
| 636 | + ) |
| 637 | + |
| 638 | + const dehydrated = dehydrate(queryClient, { |
| 639 | + shouldDehydrateMutation: () => true, |
| 640 | + }) |
| 641 | + |
| 642 | + expect(Object.keys(dehydrated.mutations[0]!)).toEqual( |
| 643 | + expect.arrayContaining(['meta']), |
| 644 | + ) |
| 645 | + expect(dehydrated.mutations[0]?.meta).toEqual({ |
| 646 | + some: 'meta', |
| 647 | + }) |
| 648 | + |
| 649 | + expect(Object.keys(dehydrated.mutations[1]!)).not.toEqual( |
| 650 | + expect.arrayContaining(['meta']), |
| 651 | + ) |
| 652 | + expect(dehydrated.mutations[1]?.meta).toEqual(undefined) |
| 653 | + |
| 654 | + const stringified = JSON.stringify(dehydrated) |
| 655 | + |
| 656 | + // --- |
| 657 | + |
| 658 | + const parsed = JSON.parse(stringified) |
| 659 | + const hydrationCache = new MutationCache() |
| 660 | + const hydrationClient = createQueryClient({ |
| 661 | + mutationCache: hydrationCache, |
| 662 | + }) |
| 663 | + hydrate(hydrationClient, parsed) |
| 664 | + expect(hydrationCache.find({ mutationKey: ['meta'] })?.meta).toEqual({ |
| 665 | + some: 'meta', |
| 666 | + }) |
| 667 | + expect(hydrationCache.find({ mutationKey: ['no-meta'] })?.meta).toEqual( |
| 668 | + undefined, |
| 669 | + ) |
| 670 | + }) |
560 | 671 | })
|
0 commit comments