react-code-dataset
/
react-query
/packages
/angular-query-persist-client
/src
/__tests__
/with-persist-query-client.test.ts
| import { describe, expect, test, vi } from 'vitest' | |
| import { | |
| QueryClient, | |
| injectQuery, | |
| provideTanStackQuery, | |
| } from '@tanstack/angular-query-experimental' | |
| import { persistQueryClientSave } from '@tanstack/query-persist-client-core' | |
| import { | |
| Component, | |
| effect, | |
| provideZonelessChangeDetection, | |
| } from '@angular/core' | |
| import { render, screen, waitFor } from '@testing-library/angular' | |
| import { queryKey, sleep } from '@tanstack/query-test-utils' | |
| import { withPersistQueryClient } from '../with-persist-query-client' | |
| import type { | |
| PersistedClient, | |
| Persister, | |
| } from '@tanstack/query-persist-client-core' | |
| const createMockPersister = (): Persister => { | |
| let storedState: PersistedClient | undefined | |
| return { | |
| persistClient(persistClient: PersistedClient) { | |
| storedState = persistClient | |
| }, | |
| async restoreClient() { | |
| await sleep(10) | |
| return storedState | |
| }, | |
| removeClient() { | |
| storedState = undefined | |
| }, | |
| } | |
| } | |
| const createMockErrorPersister = ( | |
| removeClient: Persister['removeClient'], | |
| ): [Error, Persister] => { | |
| const error = new Error('restore failed') | |
| return [ | |
| error, | |
| { | |
| async persistClient() { | |
| // noop | |
| }, | |
| async restoreClient() { | |
| await sleep(10) | |
| throw error | |
| }, | |
| removeClient, | |
| }, | |
| ] | |
| } | |
| describe('withPersistQueryClient', () => { | |
| test('restores cache from persister', async () => { | |
| const key = queryKey() | |
| const states: Array<{ | |
| status: string | |
| fetchStatus: string | |
| data: string | undefined | |
| }> = [] | |
| const queryClient = new QueryClient() | |
| await queryClient.prefetchQuery({ | |
| queryKey: key, | |
| queryFn: () => Promise.resolve('hydrated'), | |
| }) | |
| const persister = createMockPersister() | |
| await persistQueryClientSave({ queryClient, persister }) | |
| queryClient.clear() | |
| ({ | |
| template: ` | |
| <div> | |
| <h1>{{ state.data() }}</h1> | |
| <h2>fetchStatus: {{ state.fetchStatus() }}</h2> | |
| </div> | |
| `, | |
| }) | |
| class Page { | |
| state = injectQuery(() => ({ | |
| queryKey: key, | |
| queryFn: async () => { | |
| await sleep(10) | |
| return 'fetched' | |
| }, | |
| })) | |
| _ = effect(() => { | |
| states.push({ | |
| status: this.state.status(), | |
| fetchStatus: this.state.fetchStatus(), | |
| data: this.state.data(), | |
| }) | |
| }) | |
| } | |
| render(Page, { | |
| providers: [ | |
| provideZonelessChangeDetection(), | |
| provideTanStackQuery( | |
| queryClient, | |
| withPersistQueryClient({ persistOptions: { persister } }), | |
| ), | |
| ], | |
| }) | |
| await waitFor(() => screen.getByText('fetchStatus: idle')) | |
| await waitFor(() => screen.getByText('hydrated')) | |
| await waitFor(() => screen.getByText('fetched')) | |
| expect(states).toHaveLength(3) | |
| expect(states[0]).toMatchObject({ | |
| status: 'pending', | |
| fetchStatus: 'idle', | |
| data: undefined, | |
| }) | |
| expect(states[1]).toMatchObject({ | |
| status: 'success', | |
| fetchStatus: 'fetching', | |
| data: 'hydrated', | |
| }) | |
| expect(states[2]).toMatchObject({ | |
| status: 'success', | |
| fetchStatus: 'idle', | |
| data: 'fetched', | |
| }) | |
| }) | |
| test.todo( | |
| '(Once injectQueries is functional) verify that injectQueries transitions to an idle state', | |
| ) | |
| test('should show initialData while restoring', async () => { | |
| const key = queryKey() | |
| const states: Array<{ | |
| status: string | |
| fetchStatus: string | |
| data: string | undefined | |
| }> = [] | |
| const queryClient = new QueryClient() | |
| await queryClient.prefetchQuery({ | |
| queryKey: key, | |
| queryFn: () => Promise.resolve('hydrated'), | |
| }) | |
| const persister = createMockPersister() | |
| await persistQueryClientSave({ queryClient, persister }) | |
| queryClient.clear() | |
| ({ | |
| template: ` | |
| <div> | |
| <h1>{{ state.data() }}</h1> | |
| <h2>fetchStatus: {{ state.fetchStatus() }}</h2> | |
| </div> | |
| `, | |
| }) | |
| class Page { | |
| state = injectQuery(() => ({ | |
| queryKey: key, | |
| queryFn: async () => { | |
| await sleep(10) | |
| return 'fetched' | |
| }, | |
| initialData: 'initial', | |
| // make sure that initial data is older than the hydration data | |
| // otherwise initialData would be newer and takes precedence | |
| initialDataUpdatedAt: 1, | |
| })) | |
| _ = effect(() => { | |
| states.push({ | |
| status: this.state.status(), | |
| fetchStatus: this.state.fetchStatus(), | |
| data: this.state.data(), | |
| }) | |
| }) | |
| } | |
| render(Page, { | |
| providers: [ | |
| provideZonelessChangeDetection(), | |
| provideTanStackQuery( | |
| queryClient, | |
| withPersistQueryClient({ persistOptions: { persister } }), | |
| ), | |
| ], | |
| }) | |
| await waitFor(() => screen.getByText('fetched')) | |
| expect(states).toHaveLength(3) | |
| expect(states[0]).toMatchObject({ | |
| status: 'success', | |
| fetchStatus: 'idle', | |
| data: 'initial', | |
| }) | |
| expect(states[1]).toMatchObject({ | |
| status: 'success', | |
| fetchStatus: 'fetching', | |
| data: 'hydrated', | |
| }) | |
| expect(states[2]).toMatchObject({ | |
| status: 'success', | |
| fetchStatus: 'idle', | |
| data: 'fetched', | |
| }) | |
| }) | |
| test('should not refetch after restoring when data is fresh', async () => { | |
| const key = queryKey() | |
| const states: Array<{ | |
| status: string | |
| fetchStatus: string | |
| data: string | undefined | |
| }> = [] | |
| const queryClient = new QueryClient() | |
| await queryClient.prefetchQuery({ | |
| queryKey: key, | |
| queryFn: () => Promise.resolve('hydrated'), | |
| }) | |
| const persister = createMockPersister() | |
| await persistQueryClientSave({ queryClient, persister }) | |
| queryClient.clear() | |
| let fetched = false | |
| ({ | |
| template: ` | |
| <div> | |
| <h1>data: {{ state.data() ?? 'null' }}</h1> | |
| <h2>fetchStatus: {{ state.fetchStatus() }}</h2> | |
| </div> | |
| `, | |
| }) | |
| class Page { | |
| state = injectQuery(() => ({ | |
| queryKey: key, | |
| queryFn: async () => { | |
| fetched = true | |
| await sleep(10) | |
| return 'fetched' | |
| }, | |
| staleTime: Infinity, | |
| })) | |
| _ = effect(() => { | |
| states.push({ | |
| status: this.state.status(), | |
| fetchStatus: this.state.fetchStatus(), | |
| data: this.state.data(), | |
| }) | |
| }) | |
| } | |
| render(Page, { | |
| providers: [ | |
| provideZonelessChangeDetection(), | |
| provideTanStackQuery( | |
| queryClient, | |
| withPersistQueryClient({ persistOptions: { persister } }), | |
| ), | |
| ], | |
| }) | |
| await waitFor(() => screen.getByText('data: null')) | |
| await waitFor(() => screen.getByText('data: hydrated')) | |
| expect(states).toHaveLength(2) | |
| expect(fetched).toBe(false) | |
| expect(states[0]).toMatchObject({ | |
| status: 'pending', | |
| fetchStatus: 'idle', | |
| data: undefined, | |
| }) | |
| expect(states[1]).toMatchObject({ | |
| status: 'success', | |
| fetchStatus: 'idle', | |
| data: 'hydrated', | |
| }) | |
| }) | |
| test('should call onSuccess after successful restoring', async () => { | |
| const key = queryKey() | |
| const queryClient = new QueryClient() | |
| await queryClient.prefetchQuery({ | |
| queryKey: key, | |
| queryFn: () => Promise.resolve('hydrated'), | |
| }) | |
| const persister = createMockPersister() | |
| await persistQueryClientSave({ queryClient, persister }) | |
| queryClient.clear() | |
| ({ | |
| template: ` | |
| <div> | |
| <h1>{{ state.data() }}</h1> | |
| <h2>fetchStatus: {{ state.fetchStatus() }}</h2> | |
| </div> | |
| `, | |
| }) | |
| class Page { | |
| state = injectQuery(() => ({ | |
| queryKey: key, | |
| queryFn: async () => { | |
| await sleep(10) | |
| return 'fetched' | |
| }, | |
| })) | |
| } | |
| const onSuccess = vi.fn() | |
| render(Page, { | |
| providers: [ | |
| provideZonelessChangeDetection(), | |
| provideTanStackQuery( | |
| queryClient, | |
| withPersistQueryClient({ | |
| persistOptions: { persister }, | |
| onSuccess, | |
| }), | |
| ), | |
| ], | |
| }) | |
| expect(onSuccess).toHaveBeenCalledTimes(0) | |
| await waitFor(() => screen.getByText('fetched')) | |
| expect(onSuccess).toHaveBeenCalledTimes(1) | |
| }) | |
| test('should remove cache after non-successful restoring', async () => { | |
| const key = queryKey() | |
| const onErrorMock = vi | |
| .spyOn(console, 'error') | |
| .mockImplementation(() => undefined) | |
| const queryClient = new QueryClient() | |
| const removeClient = vi.fn() | |
| const [error, persister] = createMockErrorPersister(removeClient) | |
| const onSuccess = vi.fn() | |
| const onError = vi.fn() | |
| ({ | |
| template: ` | |
| <div> | |
| <h1>{{ state.data() }}</h1> | |
| <h2>fetchStatus: {{ state.fetchStatus() }}</h2> | |
| </div> | |
| `, | |
| }) | |
| class Page { | |
| state = injectQuery(() => ({ | |
| queryKey: key, | |
| queryFn: async () => { | |
| await sleep(10) | |
| return 'fetched' | |
| }, | |
| })) | |
| } | |
| render(Page, { | |
| providers: [ | |
| provideZonelessChangeDetection(), | |
| provideTanStackQuery( | |
| queryClient, | |
| withPersistQueryClient({ | |
| persistOptions: { persister }, | |
| onSuccess, | |
| onError, | |
| }), | |
| ), | |
| ], | |
| }) | |
| await waitFor(() => screen.getByText('fetched')) | |
| expect(removeClient).toHaveBeenCalledTimes(1) | |
| expect(onSuccess).toHaveBeenCalledTimes(0) | |
| expect(onError).toHaveBeenCalledTimes(1) | |
| expect(onErrorMock).toHaveBeenCalledTimes(1) | |
| expect(onErrorMock).toHaveBeenNthCalledWith(1, error) | |
| onErrorMock.mockRestore() | |
| }) | |
| }) | |