Skip to content

Commit cb57978

Browse files
committed
test(angular-query/injectQuery): add test for not fetching during restoring period when 'isRestoring' is true
1 parent 8a59b2d commit cb57978

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

packages/angular-query-experimental/src/__tests__/inject-query.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ import {
2525
} from 'vitest'
2626
import { queryKey, sleep } from '@tanstack/query-test-utils'
2727
import { lastValueFrom } from 'rxjs'
28-
import { QueryCache, QueryClient, injectQuery, provideTanStackQuery } from '..'
28+
import {
29+
QueryCache,
30+
QueryClient,
31+
injectQuery,
32+
provideIsRestoring,
33+
provideTanStackQuery,
34+
} from '..'
2935
import { setSignalInputs } from './test-utils'
3036
import type { CreateQueryOptions, OmitKeyof, QueryFunction } from '..'
3137

@@ -539,6 +545,43 @@ describe('injectQuery', () => {
539545
)
540546
})
541547

548+
describe('isRestoring', () => {
549+
test('should not fetch for the duration of the restoring period when isRestoring is true', async () => {
550+
const key = queryKey()
551+
const queryFn = vi
552+
.fn()
553+
.mockImplementation(() => sleep(10).then(() => 'data'))
554+
555+
TestBed.resetTestingModule()
556+
TestBed.configureTestingModule({
557+
providers: [
558+
provideZonelessChangeDetection(),
559+
provideTanStackQuery(queryClient),
560+
provideIsRestoring(signal(true).asReadonly()),
561+
],
562+
})
563+
564+
const query = TestBed.runInInjectionContext(() =>
565+
injectQuery(() => ({
566+
queryKey: key,
567+
queryFn,
568+
})),
569+
)
570+
571+
await vi.advanceTimersByTimeAsync(0)
572+
expect(query.status()).toBe('pending')
573+
expect(query.fetchStatus()).toBe('idle')
574+
expect(query.data()).toBeUndefined()
575+
expect(queryFn).toHaveBeenCalledTimes(0)
576+
577+
await vi.advanceTimersByTimeAsync(11)
578+
expect(query.status()).toBe('pending')
579+
expect(query.fetchStatus()).toBe('idle')
580+
expect(query.data()).toBeUndefined()
581+
expect(queryFn).toHaveBeenCalledTimes(0)
582+
})
583+
})
584+
542585
describe('injection context', () => {
543586
test('throws NG0203 with descriptive error outside injection context', () => {
544587
expect(() => {

0 commit comments

Comments
 (0)