Skip to content

test(angular-query-experimental/inject-query): use precise time in 'advanceTimersByTimeAsync' #9435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import { QueryCache, QueryClient, injectQuery, provideTanStackQuery } from '..'
import { setSignalInputs } from './test-utils'
import type { CreateQueryOptions, OmitKeyof, QueryFunction } from '..'

const QUERY_DURATION = 100

const resolveQueries = () => vi.advanceTimersByTimeAsync(QUERY_DURATION)

describe('injectQuery', () => {
let queryCache: QueryCache
let queryClient: QueryClient
Expand Down Expand Up @@ -265,7 +261,7 @@ describe('injectQuery', () => {
const query = TestBed.runInInjectionContext(() => {
return injectQuery(() => ({
queryKey: ['key1'],
queryFn: () => sleep(0).then(() => 'Some data'),
queryFn: () => sleep(10).then(() => 'Some data'),
}))
})

Expand All @@ -280,12 +276,11 @@ describe('injectQuery', () => {
const query = TestBed.runInInjectionContext(() => {
return injectQuery(() => ({
queryKey: ['key2'],
queryFn: () => sleep(0).then(() => 'result2'),
queryFn: () => sleep(10).then(() => 'result2'),
}))
})

await resolveQueries()

await vi.advanceTimersByTimeAsync(11)
expect(query.status()).toBe('success')
expect(query.data()).toBe('result2')
expect(query.isPending()).toBe(false)
Expand All @@ -300,12 +295,11 @@ describe('injectQuery', () => {
retry: false,
queryKey: ['key3'],
queryFn: () =>
sleep(0).then(() => Promise.reject(new Error('Some error'))),
sleep(10).then(() => Promise.reject(new Error('Some error'))),
}))
})

await resolveQueries()

await vi.advanceTimersByTimeAsync(11)
expect(query.status()).toBe('error')
expect(query.data()).toBe(undefined)
expect(query.error()).toMatchObject({ message: 'Some error' })
Expand All @@ -318,7 +312,7 @@ describe('injectQuery', () => {

test('should update query on options contained signal change', async () => {
const key = signal(['key6', 'key7'])
const spy = vi.fn(() => sleep(0).then(() => 'Some data'))
const spy = vi.fn(() => sleep(10).then(() => 'Some data'))

const query = TestBed.runInInjectionContext(() => {
return injectQuery(() => ({
Expand All @@ -327,10 +321,10 @@ describe('injectQuery', () => {
}))
})

await resolveQueries()

await vi.advanceTimersByTimeAsync(0)
expect(spy).toHaveBeenCalledTimes(1)

await vi.advanceTimersByTimeAsync(11)
expect(query.status()).toBe('success')

key.set(['key8'])
Expand All @@ -347,7 +341,7 @@ describe('injectQuery', () => {
})

test('should only run query once enabled signal is set to true', async () => {
const spy = vi.fn(() => sleep(0).then(() => 'Some data'))
const spy = vi.fn(() => sleep(10).then(() => 'Some data'))
const enabled = signal(false)

const query = TestBed.runInInjectionContext(() => {
Expand All @@ -362,7 +356,8 @@ describe('injectQuery', () => {
expect(query.status()).toBe('pending')

enabled.set(true)
await resolveQueries()

await vi.advanceTimersByTimeAsync(11)
expect(spy).toHaveBeenCalledTimes(1)
expect(query.status()).toBe('success')
})
Expand All @@ -371,7 +366,7 @@ describe('injectQuery', () => {
const query1 = TestBed.runInInjectionContext(() => {
return injectQuery(() => ({
queryKey: ['dependant1'],
queryFn: () => sleep(0).then(() => 'Some data'),
queryFn: () => sleep(10).then(() => 'Some data'),
}))
})

Expand All @@ -393,12 +388,12 @@ describe('injectQuery', () => {
expect(query2.fetchStatus()).toStrictEqual('idle')
expect(dependentQueryFn).not.toHaveBeenCalled()

await resolveQueries()
await vi.advanceTimersByTimeAsync(11)

expect(query1.data()).toStrictEqual('Some data')
expect(query2.fetchStatus()).toStrictEqual('fetching')

await vi.runAllTimersAsync()
await vi.advanceTimersByTimeAsync(1002)

expect(query2.fetchStatus()).toStrictEqual('idle')
expect(query2.status()).toStrictEqual('success')
Expand All @@ -409,7 +404,7 @@ describe('injectQuery', () => {
})

test('should use the current value for the queryKey when refetch is called', async () => {
const fetchFn = vi.fn(() => sleep(0).then(() => 'Some data'))
const fetchFn = vi.fn(() => sleep(10).then(() => 'Some data'))
const keySignal = signal('key11')

const query = TestBed.runInInjectionContext(() => {
Expand All @@ -431,10 +426,9 @@ describe('injectQuery', () => {
)
})

await resolveQueries()
await vi.advanceTimersByTimeAsync(11)

keySignal.set('key12')

TestBed.tick()

query.refetch().then(() => {
Expand All @@ -446,7 +440,7 @@ describe('injectQuery', () => {
)
})

await resolveQueries()
await vi.advanceTimersByTimeAsync(11)
})

describe('throwOnError', () => {
Expand All @@ -456,13 +450,13 @@ describe('injectQuery', () => {
return injectQuery(() => ({
queryKey: ['key12'],
queryFn: () =>
sleep(0).then(() => Promise.reject(new Error('Some error'))),
sleep(10).then(() => Promise.reject(new Error('Some error'))),
retry: false,
throwOnError: boundaryFn,
}))
})

await vi.runAllTimersAsync()

await vi.advanceTimersByTimeAsync(11)
expect(boundaryFn).toHaveBeenCalledTimes(1)
expect(boundaryFn).toHaveBeenCalledWith(
Error('Some error'),
Expand Down Expand Up @@ -505,13 +499,13 @@ describe('injectQuery', () => {
retry: false,
queryKey: ['key15'],
queryFn: () =>
sleep(0).then(() => Promise.reject(new Error('Some error'))),
sleep(10).then(() => Promise.reject(new Error('Some error'))),
}))
})

expect(query.status()).toBe('pending')

await resolveQueries()
await vi.advanceTimersByTimeAsync(11)

expect(query.status()).toBe('error')
})
Expand All @@ -537,7 +531,7 @@ describe('injectQuery', () => {
})

fixture.detectChanges()
await resolveQueries()
await vi.advanceTimersByTimeAsync(0)

expect(fixture.componentInstance.query.data()).toEqual(
'signal-input-required-test',
Expand Down
Loading