Skip to content

Commit

Permalink
chore(angular-query): add type tests for injectQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoud-dv committed Aug 24, 2024
1 parent d5a0342 commit c94a58b
Showing 1 changed file with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,125 @@
import { describe, expectTypeOf } from 'vitest'
import { injectQuery } from '..'
import { describe, expectTypeOf, it } from 'vitest'
import { injectQuery, queryOptions } from '..'
import { simpleFetcher } from './test-utils'
import type { Signal } from '@angular/core'

describe('initialData', () => {
describe('Config object overload', () => {
it('TData should always be defined when initialData is provided as an object', () => {
const { data } = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => ({ wow: true }),
initialData: { wow: true },
}))

expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
})

it('TData should be defined when passed through queryOptions', () => {
const options = () =>
queryOptions({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
initialData: {
wow: true,
},
})
const { data } = injectQuery(options)

expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
})

it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
const options = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(1),
})

const query = injectQuery(() => ({
...options,
select: (data) => data > 1,
}))

expectTypeOf(query.data).toEqualTypeOf<Signal<boolean | undefined>>()
})

it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
const { data } = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
initialData: () => ({
wow: true,
}),
}))

expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
})

it('TData should have undefined in the union when initialData is NOT provided', () => {
const { data } = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
}))

expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
})

it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
const { data } = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
initialData: () => undefined as { wow: boolean } | undefined,
}))

expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
})

it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
const query = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
initialData: () => undefined as { wow: boolean } | undefined,
}))

if (query.isSuccess()) {
expectTypeOf(query.data).toEqualTypeOf<Signal<{ wow: boolean }>>()
}
})
})

describe('structuralSharing', () => {
it('should restrict to same types', () => {
injectQuery(() => ({
queryKey: ['key'],
queryFn: () => 5,
structuralSharing: (_oldData, newData) => {
return newData
},
}))
})
})
})

describe('Discriminated union return type', () => {
test('data should be possibly undefined by default', () => {
const query = injectQuery(() => ({
Expand Down

0 comments on commit c94a58b

Please sign in to comment.