Skip to content
Merged
Show file tree
Hide file tree
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 @@ -17,7 +17,7 @@ describe('injectIsFetching', () => {
})
})

it('Returns number of fetching queries', fakeAsync(() => {
test('Returns number of fetching queries', fakeAsync(() => {
const isFetching = TestBed.runInInjectionContext(() => {
injectQuery(() => ({
queryKey: ['isFetching1'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('injectIsMutating', () => {
})
})

it('should properly return isMutating state', fakeAsync(() => {
test('should properly return isMutating state', fakeAsync(() => {
TestBed.runInInjectionContext(() => {
const isMutating = injectIsMutating()
const mutation = injectMutation(() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('injectQuery', () => {
})
})

it('should return pending status initially', fakeAsync(() => {
test('should return pending status initially', fakeAsync(() => {
const query = TestBed.runInInjectionContext(() => {
return injectQuery(() => ({
queryKey: ['key1'],
Expand All @@ -35,7 +35,7 @@ describe('injectQuery', () => {
flush()
}))

it('should resolve to success and update signal: injectQuery()', fakeAsync(() => {
test('should resolve to success and update signal: injectQuery()', fakeAsync(() => {
const query = TestBed.runInInjectionContext(() => {
return injectQuery(() => ({
queryKey: ['key2'],
Expand All @@ -53,7 +53,7 @@ describe('injectQuery', () => {
expect(query.isSuccess()).toBe(true)
}))

it('should reject and update signal', fakeAsync(() => {
test('should reject and update signal', fakeAsync(() => {
const query = TestBed.runInInjectionContext(() => {
return injectQuery(() => ({
retry: false,
Expand All @@ -74,7 +74,7 @@ describe('injectQuery', () => {
expect(query.failureReason()).toMatchObject({ message: 'Some error' })
}))

it('should update query on options contained signal change', fakeAsync(() => {
test('should update query on options contained signal change', fakeAsync(() => {
const key = signal(['key6', 'key7'])
const spy = vi.fn(simpleFetcher)

Expand All @@ -97,7 +97,7 @@ describe('injectQuery', () => {
flush()
}))

it('should only run query once enabled signal is set to true', fakeAsync(() => {
test('should only run query once enabled signal is set to true', fakeAsync(() => {
const spy = vi.fn(simpleFetcher)
const enabled = signal(false)

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

it('should properly execute dependant queries', fakeAsync(() => {
test('should properly execute dependant queries', fakeAsync(() => {
const query1 = TestBed.runInInjectionContext(() => {
return injectQuery(() => ({
queryKey: ['dependant1'],
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('injectQuery', () => {
)
}))

it('should use the current value for the queryKey when refetch is called', fakeAsync(() => {
test('should use the current value for the queryKey when refetch is called', fakeAsync(() => {
const fetchFn = vi.fn(simpleFetcher)
const keySignal = signal('key11')

Expand Down Expand Up @@ -200,7 +200,7 @@ describe('injectQuery', () => {
flush()
}))

it('should set state to error when queryFn returns reject promise', fakeAsync(() => {
test('should set state to error when queryFn returns reject promise', fakeAsync(() => {
const query = TestBed.runInInjectionContext(() => {
return injectQuery(() => ({
retry: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { assertType, describe, expectTypeOf } from 'vitest'
import { QueryClient } from '@tanstack/query-core'
import { dataTagSymbol } from '@tanstack/query-core'
import { queryOptions } from '../query-options'
import { injectQuery } from '../inject-query'
import type { Signal } from '@angular/core'

describe('queryOptions', () => {
test('should not allow excess properties', () => {
return queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
// @ts-expect-error this is a good error, because stallTime does not exist!
stallTime: 1000,
})
})

test('should infer types for callbacks', () => {
return queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
staleTime: 1000,
select: (data) => {
expectTypeOf(data).toEqualTypeOf<number>()
},
})
})
})

test('should work when passed to injectQuery', () => {
const options = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
})

const { data } = injectQuery(() => options)
expectTypeOf(data).toEqualTypeOf<Signal<number | undefined>>()
})

test('should work when passed to fetchQuery', () => {
const options = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
})

const data = new QueryClient().fetchQuery(options)
assertType<Promise<number>>(data)
})

test('should tag the queryKey with the result type of the QueryFn', () => {
const { queryKey } = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
})
assertType<number>(queryKey[dataTagSymbol])
})

test('should tag the queryKey even if no promise is returned', () => {
const { queryKey } = queryOptions({
queryKey: ['key'],
queryFn: () => 5,
})
assertType<number>(queryKey[dataTagSymbol])
})

test('should tag the queryKey with unknown if there is no queryFn', () => {
const { queryKey } = queryOptions({
queryKey: ['key'],
})

assertType<unknown>(queryKey[dataTagSymbol])
})

test('should return the proper type when passed to getQueryData', () => {
const { queryKey } = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
})

const queryClient = new QueryClient()
const data = queryClient.getQueryData(queryKey)

expectTypeOf(data).toEqualTypeOf<number | undefined>()
})

test('should properly type updaterFn when passed to setQueryData', () => {
const { queryKey } = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
})

const queryClient = new QueryClient()
const data = queryClient.setQueryData(queryKey, (prev) => {
expectTypeOf(prev).toEqualTypeOf<number | undefined>()
return prev
})

expectTypeOf(data).toEqualTypeOf<number | undefined>()
})

test('should properly type value when passed to setQueryData', () => {
const { queryKey } = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
})

const queryClient = new QueryClient()

// @ts-expect-error value should be a number
queryClient.setQueryData(queryKey, '5')
// @ts-expect-error value should be a number
queryClient.setQueryData(queryKey, () => '5')

const data = queryClient.setQueryData(queryKey, 5)

expectTypeOf(data).toEqualTypeOf<number | undefined>()
})
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import { signal } from '@angular/core'
import { isReactive } from '@angular/core/primitives/signals'
import { isSignal, signal } from '@angular/core'
import { describe } from 'vitest'
import { signalProxy } from '../signal-proxy'

describe('signalProxy', () => {
const inputSignal = signal({ fn: () => 'bar', baz: 'qux' })
const proxy = signalProxy(inputSignal)

it('should have computed fields', () => {
test('should have computed fields', () => {
expect(proxy.baz()).toEqual('qux')
expect(isReactive(proxy.baz)).toBe(true)
expect(isSignal(proxy.baz)).toBe(true)
})

it('should pass through functions as-is', () => {
test('should pass through functions as-is', () => {
expect(proxy.fn()).toEqual('bar')
expect(isReactive(proxy.fn)).toBe(false)
expect(isSignal(proxy.fn)).toBe(false)
})

it('supports "in" operator', () => {
test('supports "in" operator', () => {
expect('baz' in proxy).toBe(true)
expect('foo' in proxy).toBe(false)
})

it('supports "Object.keys"', () => {
test('supports "Object.keys"', () => {
expect(Object.keys(proxy)).toEqual(['fn', 'baz'])
})
})