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
@@ -1,4 +1,5 @@
import { assertType, describe, expectTypeOf, it, test } from 'vitest'
import { queryKey } from '@tanstack/query-test-utils'
import { QueryClient, dataTagSymbol } from '@tanstack/query-core'
import { infiniteQueryOptions } from '../infinite-query-options'
import { injectInfiniteQuery } from '../inject-infinite-query'
Expand All @@ -11,9 +12,10 @@ import type {

describe('infiniteQueryOptions', () => {
it('should not allow excess properties', () => {
const key = queryKey()
assertType(
infiniteQueryOptions({
queryKey: ['key'],
queryKey: key,
queryFn: () => Promise.resolve('data'),
getNextPageParam: () => 1,
initialPageParam: 1,
Expand All @@ -23,8 +25,9 @@ describe('infiniteQueryOptions', () => {
)
})
it('should infer types for callbacks', () => {
const key = queryKey()
infiniteQueryOptions({
queryKey: ['key'],
queryKey: key,
queryFn: () => Promise.resolve('data'),
staleTime: 1000,
getNextPageParam: () => 1,
Expand All @@ -35,8 +38,9 @@ describe('infiniteQueryOptions', () => {
})
})
it('should work when passed to useInfiniteQuery', () => {
const key = queryKey()
const options = infiniteQueryOptions({
queryKey: ['key'],
queryKey: key,
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
Expand All @@ -51,8 +55,9 @@ describe('infiniteQueryOptions', () => {
})

it('should work when passed to fetchInfiniteQuery', async () => {
const key = queryKey()
const options = infiniteQueryOptions({
queryKey: ['key'],
queryKey: key,
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
Expand All @@ -63,61 +68,66 @@ describe('infiniteQueryOptions', () => {
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
})
it('should tag the queryKey with the result type of the QueryFn', () => {
const { queryKey } = infiniteQueryOptions({
queryKey: ['key'],
const key = queryKey()
const { queryKey: tagged } = infiniteQueryOptions({
queryKey: key,
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
})

expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
})
it('should tag the queryKey even if no promise is returned', () => {
const { queryKey } = infiniteQueryOptions({
queryKey: ['key'],
const key = queryKey()
const { queryKey: tagged } = infiniteQueryOptions({
queryKey: key,
queryFn: () => 'string',
getNextPageParam: () => 1,
initialPageParam: 1,
})

expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
})
it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
const { queryKey } = infiniteQueryOptions({
queryKey: ['key'],
const key = queryKey()
const { queryKey: tagged } = infiniteQueryOptions({
queryKey: key,
queryFn: () => Promise.resolve('string'),
select: (data) => data.pages,
getNextPageParam: () => 1,
initialPageParam: 1,
})

expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
})
it('should return the proper type when passed to getQueryData', () => {
const { queryKey } = infiniteQueryOptions({
queryKey: ['key'],
const key = queryKey()
const { queryKey: tagged } = infiniteQueryOptions({
queryKey: key,
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
})

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

expectTypeOf(data).toEqualTypeOf<
InfiniteData<string, unknown> | undefined
>()
})
it('should properly type when passed to setQueryData', () => {
const { queryKey } = infiniteQueryOptions({
queryKey: ['key'],
const key = queryKey()
const { queryKey: tagged } = infiniteQueryOptions({
queryKey: key,
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
})

const queryClient = new QueryClient()
const data = queryClient.setQueryData(queryKey, (prev) => {
const data = queryClient.setQueryData(tagged, (prev) => {
expectTypeOf(prev).toEqualTypeOf<
InfiniteData<string, unknown> | undefined
>()
Expand All @@ -130,9 +140,10 @@ describe('infiniteQueryOptions', () => {
})

test('should not be allowed to be passed to non-infinite query functions', () => {
const key = queryKey()
const queryClient = new QueryClient()
const options = infiniteQueryOptions({
queryKey: ['key'],
queryKey: key,
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
Expand All @@ -156,9 +167,10 @@ describe('infiniteQueryOptions', () => {
})

test('allow optional initialData function', () => {
const key = queryKey()
const initialData: { example: boolean } | undefined = { example: true }
const queryOptions = infiniteQueryOptions({
queryKey: ['example'],
queryKey: key,
queryFn: () => initialData,
initialData: initialData
? () => ({ pages: [initialData], pageParams: [] })
Expand All @@ -174,9 +186,10 @@ describe('infiniteQueryOptions', () => {
})

test('allow optional initialData object', () => {
const key = queryKey()
const initialData: { example: boolean } | undefined = { example: true }
const queryOptions = infiniteQueryOptions({
queryKey: ['example'],
queryKey: key,
queryFn: () => initialData,
initialData: initialData
? { pages: [initialData], pageParams: [] }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { describe, expect, it } from 'vitest'
import { queryKey } from '@tanstack/query-test-utils'

import { infiniteQueryOptions } from '../infinite-query-options'
import type { CreateInfiniteQueryOptions } from '../types'

describe('infiniteQueryOptions', () => {
it('should return the object received as a parameter without any modification.', () => {
const key = queryKey()
const object: CreateInfiniteQueryOptions = {
queryKey: ['key'],
queryKey: key,
queryFn: () => Promise.resolve(5),
getNextPageParam: () => null,
initialPageParam: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestBed } from '@angular/core/testing'
import { afterEach, beforeEach, describe, expectTypeOf, test, vi } from 'vitest'
import { provideZonelessChangeDetection } from '@angular/core'
import { sleep } from '@tanstack/query-test-utils'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import { QueryClient, injectInfiniteQuery, provideTanStackQuery } from '..'
import type { InfiniteData } from '@tanstack/query-core'

Expand All @@ -24,9 +24,10 @@ describe('injectInfiniteQuery', () => {
})

test('should narrow type after isSuccess', () => {
const key = queryKey()
const query = TestBed.runInInjectionContext(() => {
return injectInfiniteQuery(() => ({
queryKey: ['infiniteQuery'],
queryKey: key,
queryFn: ({ pageParam }) =>
sleep(0).then(() => 'data on page ' + pageParam),
initialPageParam: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestBed } from '@angular/core/testing'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { Injector, provideZonelessChangeDetection } from '@angular/core'
import { sleep } from '@tanstack/query-test-utils'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import { QueryClient, injectInfiniteQuery, provideTanStackQuery } from '..'
import { expectSignals } from './test-utils'

Expand All @@ -24,9 +24,10 @@ describe('injectInfiniteQuery', () => {
})

test('should properly execute infinite query', async () => {
const key = queryKey()
const query = TestBed.runInInjectionContext(() => {
return injectInfiniteQuery(() => ({
queryKey: ['infiniteQuery'],
queryKey: key,
queryFn: ({ pageParam }) =>
sleep(10).then(() => 'data on page ' + pageParam),
initialPageParam: 0,
Expand Down Expand Up @@ -64,9 +65,10 @@ describe('injectInfiniteQuery', () => {

describe('injection context', () => {
test('throws NG0203 with descriptive error outside injection context', () => {
const key = queryKey()
expect(() => {
injectInfiniteQuery(() => ({
queryKey: ['injectionContextError'],
queryKey: key,
queryFn: ({ pageParam }) =>
sleep(0).then(() => 'data on page ' + pageParam),
initialPageParam: 0,
Expand All @@ -76,9 +78,10 @@ describe('injectInfiniteQuery', () => {
})

test('can be used outside injection context when passing an injector', () => {
const key = queryKey()
const query = injectInfiniteQuery(
() => ({
queryKey: ['manualInjector'],
queryKey: key,
queryFn: ({ pageParam }) =>
sleep(0).then(() => 'data on page ' + pageParam),
initialPageParam: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestBed } from '@angular/core/testing'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { Injector, provideZonelessChangeDetection } from '@angular/core'
import { sleep } from '@tanstack/query-test-utils'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import {
QueryClient,
injectIsFetching,
Expand Down Expand Up @@ -29,9 +29,10 @@ describe('injectIsFetching', () => {
})

test('Returns number of fetching queries', async () => {
const key = queryKey()
const isFetching = TestBed.runInInjectionContext(() => {
injectQuery(() => ({
queryKey: ['isFetching1'],
queryKey: key,
queryFn: () => sleep(100).then(() => 'Some data'),
}))
return injectIsFetching()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { TestBed } from '@angular/core/testing'
import { Injector, provideZonelessChangeDetection } from '@angular/core'
import { sleep } from '@tanstack/query-test-utils'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import {
QueryClient,
injectIsMutating,
Expand Down Expand Up @@ -29,9 +29,10 @@ describe('injectIsMutating', () => {
})

test('should properly return isMutating state', async () => {
const key = queryKey()
const [mutation, isMutating] = TestBed.runInInjectionContext(() => [
injectMutation(() => ({
mutationKey: ['isMutating1'],
mutationKey: key,
mutationFn: (params: { par1: string }) => sleep(10).then(() => params),
})),
injectIsMutating(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { TestBed } from '@angular/core/testing'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { By } from '@angular/platform-browser'
import { sleep } from '@tanstack/query-test-utils'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import {
QueryClient,
injectMutation,
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('injectMutationState', () => {

describe('injectMutationState', () => {
test('should return variables after calling mutate 1', () => {
const mutationKey = ['mutation']
const mutationKey = queryKey()
const variables = 'foo123'

const mutation = TestBed.runInInjectionContext(() => {
Expand All @@ -60,8 +60,8 @@ describe('injectMutationState', () => {
})

test('reactive options should update injectMutationState', () => {
const mutationKey1 = ['mutation1']
const mutationKey2 = ['mutation2']
const mutationKey1 = queryKey()
const mutationKey2 = queryKey()
const variables1 = 'foo123'
const variables2 = 'bar234'

Expand Down Expand Up @@ -98,7 +98,7 @@ describe('injectMutationState', () => {

test('should return variables after calling mutate 2', () => {
queryClient.clear()
const mutationKey = ['mutation']
const mutationKey = queryKey()
const variables = 'bar234'

const mutation = TestBed.runInInjectionContext(() => {
Expand Down
Loading
Loading