Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
262 changes: 132 additions & 130 deletions packages/angular-query-experimental/src/__tests__/inject-query.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,177 +3,179 @@ import { sleep } from '@tanstack/query-test-utils'
import { injectQuery, queryOptions } from '..'
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 },
}))
describe('injectQuery', () => {
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 }>>()
})
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
})

it('TData should be defined when passed through queryOptions', () => {
const options = () =>
queryOptions({
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', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix test title naming mismatch (useQueryinjectQuery).

Line 37 mentions useQuery, but this suite validates injectQuery; update the title to avoid misleading test output.

✏️ Suggested text fix
-      it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
+      it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into injectQuery', () => {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into injectQuery', () => {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/angular-query-experimental/src/__tests__/inject-query.test-d.ts` at
line 37, Update the test title string in the it(...) block that currently reads
"should be possible to define a different TData than TQueryFnData using select
with queryOptions spread into useQuery" to replace "useQuery" with "injectQuery"
so the test description matches the actual API under test (the it(...) block for
injectQuery in the inject-query.test-d.ts file).

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: {
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,
}
},
})
const { data } = injectQuery(options)
}))

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

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),
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>>()
})

const query = injectQuery(() => ({
...options,
select: (data) => data > 1,
}))
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,
}))

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

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 }>>()
describe('structuralSharing', () => {
it('should be able to use structuralSharing with unknown types', () => {
// https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
injectQuery(() => ({
queryKey: ['key'],
queryFn: () => 5,
structuralSharing: (oldData, newData) => {
expectTypeOf(oldData).toBeUnknown()
expectTypeOf(newData).toBeUnknown()
return newData
},
}))
})
})
})

it('TData should have undefined in the union when initialData is NOT provided', () => {
const { data } = injectQuery(() => ({
describe('Discriminated union return type', () => {
test('data should be possibly undefined by default', () => {
const query = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
queryFn: () => sleep(0).then(() => 'Some data'),
}))

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

it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
const { data } = injectQuery(() => ({
test('data should be defined when query is success', () => {
const query = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
initialData: () => undefined as { wow: boolean } | undefined,
queryFn: () => sleep(0).then(() => 'Some data'),
}))

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

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

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

describe('structuralSharing', () => {
it('should be able to use structuralSharing with unknown types', () => {
// https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
injectQuery(() => ({
test('data should be undefined when query is pending', () => {
const query = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => 5,
structuralSharing: (oldData, newData) => {
expectTypeOf(oldData).toBeUnknown()
expectTypeOf(newData).toBeUnknown()
return newData
},
queryFn: () => sleep(0).then(() => 'Some data'),
}))
})
})
})

describe('Discriminated union return type', () => {
test('data should be possibly undefined by default', () => {
const query = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => sleep(0).then(() => 'Some data'),
}))

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

test('data should be defined when query is success', () => {
const query = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => sleep(0).then(() => 'Some data'),
}))

if (query.isSuccess()) {
expectTypeOf(query.data).toEqualTypeOf<Signal<string>>()
}
})

test('error should be null when query is success', () => {
const query = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => sleep(0).then(() => 'Some data'),
}))

if (query.isSuccess()) {
expectTypeOf(query.error).toEqualTypeOf<Signal<null>>()
}
})

test('data should be undefined when query is pending', () => {
const query = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => sleep(0).then(() => 'Some data'),
}))

if (query.isPending()) {
expectTypeOf(query.data).toEqualTypeOf<Signal<undefined>>()
}
})
if (query.isPending()) {
expectTypeOf(query.data).toEqualTypeOf<Signal<undefined>>()
}
})

test('error should be defined when query is error', () => {
const query = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => sleep(0).then(() => 'Some data'),
}))
test('error should be defined when query is error', () => {
const query = injectQuery(() => ({
queryKey: ['key'],
queryFn: () => sleep(0).then(() => 'Some data'),
}))

if (query.isError()) {
expectTypeOf(query.error).toEqualTypeOf<Signal<Error>>()
}
if (query.isError()) {
expectTypeOf(query.error).toEqualTypeOf<Signal<Error>>()
}
})
})
})
Loading
Loading