File size: 2,597 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
import { TestBed } from '@angular/core/testing'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { Injector, provideZonelessChangeDetection } from '@angular/core'
import { sleep } from '@tanstack/query-test-utils'
import { QueryClient, injectInfiniteQuery, provideTanStackQuery } from '..'
import { expectSignals } from './test-utils'
describe('injectInfiniteQuery', () => {
let queryClient: QueryClient
beforeEach(() => {
queryClient = new QueryClient()
vi.useFakeTimers()
TestBed.configureTestingModule({
providers: [
provideZonelessChangeDetection(),
provideTanStackQuery(queryClient),
],
})
})
afterEach(() => {
vi.useRealTimers()
})
test('should properly execute infinite query', async () => {
const query = TestBed.runInInjectionContext(() => {
return injectInfiniteQuery(() => ({
queryKey: ['infiniteQuery'],
queryFn: ({ pageParam }) =>
sleep(10).then(() => 'data on page ' + pageParam),
initialPageParam: 0,
getNextPageParam: () => 12,
}))
})
expectSignals(query, {
data: undefined,
status: 'pending',
})
await vi.advanceTimersByTimeAsync(11)
expectSignals(query, {
data: {
pageParams: [0],
pages: ['data on page 0'],
},
status: 'success',
})
void query.fetchNextPage()
await vi.advanceTimersByTimeAsync(11)
expectSignals(query, {
data: {
pageParams: [0, 12],
pages: ['data on page 0', 'data on page 12'],
},
status: 'success',
})
})
describe('injection context', () => {
test('throws NG0203 with descriptive error outside injection context', () => {
expect(() => {
injectInfiniteQuery(() => ({
queryKey: ['injectionContextError'],
queryFn: ({ pageParam }) =>
sleep(0).then(() => 'data on page ' + pageParam),
initialPageParam: 0,
getNextPageParam: () => 12,
}))
}).toThrowError(/NG0203(.*?)injectInfiniteQuery/)
})
test('can be used outside injection context when passing an injector', () => {
const query = injectInfiniteQuery(
() => ({
queryKey: ['manualInjector'],
queryFn: ({ pageParam }) =>
sleep(0).then(() => 'data on page ' + pageParam),
initialPageParam: 0,
getNextPageParam: () => 12,
}),
{
injector: TestBed.inject(Injector),
},
)
expect(query.status()).toBe('pending')
})
})
})
|