Skip to content
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

chore(angular-query): add type tests for injectQuery #7947

Merged
merged 1 commit into from
Aug 24, 2024
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
@@ -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
Loading