|
1 | 1 | import { describe, expect, test, vi } from 'vitest'
|
2 | 2 | import { QueriesObserver } from '@tanstack/query-core'
|
3 |
| -import { persistQueryClientSubscribe } from '../persist' |
| 3 | +import { |
| 4 | + persistQueryClientRestore, |
| 5 | + persistQueryClientSubscribe, |
| 6 | +} from '../persist' |
4 | 7 | import {
|
5 | 8 | createMockPersister,
|
6 | 9 | createQueryClient,
|
@@ -63,3 +66,97 @@ describe('persistQueryClientSave', () => {
|
63 | 66 | unsubscribe()
|
64 | 67 | })
|
65 | 68 | })
|
| 69 | + |
| 70 | +describe('persistQueryClientRestore', () => { |
| 71 | + test('should rethrow exceptions in `restoreClient`', async () => { |
| 72 | + const consoleMock = vi |
| 73 | + .spyOn(console, 'error') |
| 74 | + .mockImplementation(() => undefined) |
| 75 | + |
| 76 | + const consoleWarn = vi |
| 77 | + .spyOn(console, 'warn') |
| 78 | + .mockImplementation(() => undefined) |
| 79 | + |
| 80 | + const queryClient = createQueryClient() |
| 81 | + |
| 82 | + const restoreError = new Error('Error restoring client') |
| 83 | + |
| 84 | + const persister = createSpyPersister() |
| 85 | + |
| 86 | + persister.restoreClient = () => Promise.reject(restoreError) |
| 87 | + |
| 88 | + await expect( |
| 89 | + persistQueryClientRestore({ |
| 90 | + queryClient, |
| 91 | + persister, |
| 92 | + }), |
| 93 | + ).rejects.toBe(restoreError) |
| 94 | + |
| 95 | + expect(consoleMock).toHaveBeenCalledTimes(1) |
| 96 | + expect(consoleWarn).toHaveBeenCalledTimes(1) |
| 97 | + expect(consoleMock).toHaveBeenNthCalledWith(1, restoreError) |
| 98 | + |
| 99 | + consoleMock.mockRestore() |
| 100 | + consoleWarn.mockRestore() |
| 101 | + }) |
| 102 | + |
| 103 | + test('should rethrow exceptions in `removeClient` before `restoreClient`', async () => { |
| 104 | + const consoleMock = vi |
| 105 | + .spyOn(console, 'error') |
| 106 | + .mockImplementation(() => undefined) |
| 107 | + |
| 108 | + const consoleWarn = vi |
| 109 | + .spyOn(console, 'warn') |
| 110 | + .mockImplementation(() => undefined) |
| 111 | + |
| 112 | + const queryClient = createQueryClient() |
| 113 | + |
| 114 | + const restoreError = new Error('Error restoring client') |
| 115 | + const removeError = new Error('Error removing client') |
| 116 | + |
| 117 | + const persister = createSpyPersister() |
| 118 | + |
| 119 | + persister.restoreClient = () => Promise.reject(restoreError) |
| 120 | + persister.removeClient = () => Promise.reject(removeError) |
| 121 | + |
| 122 | + await expect( |
| 123 | + persistQueryClientRestore({ |
| 124 | + queryClient, |
| 125 | + persister, |
| 126 | + }), |
| 127 | + ).rejects.toBe(removeError) |
| 128 | + |
| 129 | + expect(consoleMock).toHaveBeenCalledTimes(1) |
| 130 | + expect(consoleWarn).toHaveBeenCalledTimes(1) |
| 131 | + expect(consoleMock).toHaveBeenNthCalledWith(1, restoreError) |
| 132 | + |
| 133 | + consoleMock.mockRestore() |
| 134 | + consoleWarn.mockRestore() |
| 135 | + }) |
| 136 | + |
| 137 | + test('should rethrow error in `removeClient`', async () => { |
| 138 | + const queryClient = createQueryClient() |
| 139 | + |
| 140 | + const persister = createSpyPersister() |
| 141 | + const removeError = new Error('Error removing client') |
| 142 | + |
| 143 | + persister.removeClient = () => Promise.reject(removeError) |
| 144 | + persister.restoreClient = () => { |
| 145 | + return Promise.resolve({ |
| 146 | + buster: 'random-buster', |
| 147 | + clientState: { |
| 148 | + mutations: [], |
| 149 | + queries: [], |
| 150 | + }, |
| 151 | + timestamp: new Date().getTime(), |
| 152 | + }) |
| 153 | + } |
| 154 | + |
| 155 | + await expect( |
| 156 | + persistQueryClientRestore({ |
| 157 | + queryClient, |
| 158 | + persister, |
| 159 | + }), |
| 160 | + ).rejects.toBe(removeError) |
| 161 | + }) |
| 162 | +}) |
0 commit comments