Skip to content

Commit

Permalink
Merge branch 'main' into react-query/test/usePrefetchInfiniteQuery-name
Browse files Browse the repository at this point in the history
  • Loading branch information
manudeli authored Nov 13, 2024
2 parents a8c6491 + 8d1f542 commit c181c71
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/eslint/eslint-plugin-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@ Alternatively, add `@tanstack/query` to the plugins section, and configure the r
- [@tanstack/query/no-rest-destructuring](../no-rest-destructuring)
- [@tanstack/query/stable-query-client](../stable-query-client)
- [@tanstack/query/no-unstable-deps](../no-unstable-deps)
- [@tanstack/query/infinite-query-property-order](../infinite-query-property-order)
17 changes: 17 additions & 0 deletions packages/react-query/src/__tests__/infiniteQueryOptions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, it } from 'vitest'

import { infiniteQueryOptions } from '../infiniteQueryOptions'
import type { UseInfiniteQueryOptions } from '../types'

describe('infiniteQueryOptions', () => {
it('should return the object received as a parameter without any modification.', () => {
const object: UseInfiniteQueryOptions = {
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
getNextPageParam: () => null,
initialPageParam: null,
}

expect(infiniteQueryOptions(object)).toStrictEqual(object)
})
})
14 changes: 14 additions & 0 deletions packages/react-query/src/__tests__/queryOptions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from 'vitest'
import { queryOptions } from '../queryOptions'
import type { UseQueryOptions } from '../types'

describe('queryOptions', () => {
it('should return the object received as a parameter without any modification.', () => {
const object: UseQueryOptions = {
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
} as const

expect(queryOptions(object)).toStrictEqual(object)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it, vi } from 'vitest'
import * as React from 'react'
import { QueryCache, skipToken, useSuspenseInfiniteQuery } from '..'
import { createQueryClient, queryKey, renderWithClient } from './utils'

describe('useSuspenseInfiniteQuery', () => {
const queryCache = new QueryCache()
const queryClient = createQueryClient({ queryCache })

it('should log an error when skipToken is passed as queryFn', () => {
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
const key = queryKey()

function Page() {
useSuspenseInfiniteQuery({
queryKey: key,
initialPageParam: 1,
getNextPageParam: () => 1,
// @ts-expect-error
queryFn: Math.random() >= 0 ? skipToken : () => Promise.resolve(5),
})

return null
}

function App() {
return (
<React.Suspense fallback="Loading...">
<Page />
</React.Suspense>
)
}

renderWithClient(queryClient, <App />)

expect(consoleErrorSpy).toHaveBeenCalledWith(
'skipToken is not allowed for useSuspenseInfiniteQuery',
)
consoleErrorSpy.mockRestore()
})
})
38 changes: 37 additions & 1 deletion packages/react-query/src/__tests__/useSuspenseQueries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { act, fireEvent, render, waitFor } from '@testing-library/react'
import * as React from 'react'
import { ErrorBoundary } from 'react-error-boundary'
import { useSuspenseQueries, useSuspenseQuery } from '..'
import { skipToken, useSuspenseQueries, useSuspenseQuery } from '..'
import { createQueryClient, queryKey, renderWithClient, sleep } from './utils'
import type { UseSuspenseQueryOptions } from '..'

Expand Down Expand Up @@ -658,4 +658,40 @@ describe('useSuspenseQueries 2', () => {
expect(queryClient.getQueryData(key)).toBe(undefined)
})
})

it('should log an error when skipToken is passed as queryFn', () => {
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
const key = queryKey()

function Page() {
useSuspenseQueries({
queries: [
{
queryKey: key,
// @ts-expect-error
queryFn: Math.random() >= 0 ? skipToken : () => Promise.resolve(5),
},
],
})

return null
}

function App() {
return (
<React.Suspense fallback="Loading...">
<Page />
</React.Suspense>
)
}

renderWithClient(queryClient, <App />)

expect(consoleErrorSpy).toHaveBeenCalledWith(
'skipToken is not allowed for useSuspenseQueries',
)
consoleErrorSpy.mockRestore()
})
})
33 changes: 33 additions & 0 deletions packages/react-query/src/__tests__/useSuspenseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ErrorBoundary } from 'react-error-boundary'
import {
QueryCache,
QueryErrorResetBoundary,
skipToken,
useQueryErrorResetBoundary,
useSuspenseInfiniteQuery,
useSuspenseQuery,
Expand Down Expand Up @@ -870,4 +871,36 @@ describe('useSuspenseQuery', () => {
await waitFor(() => rendered.getByText('loading'))
await waitFor(() => rendered.getByText('data: 2'))
})

it('should log an error when skipToken is passed as queryFn', () => {
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
const key = queryKey()

function Page() {
useSuspenseQuery({
queryKey: key,
// @ts-expect-error
queryFn: Math.random() >= 0 ? skipToken : () => Promise.resolve(5),
})

return null
}

function App() {
return (
<React.Suspense fallback="Loading...">
<Page />
</React.Suspense>
)
}

renderWithClient(queryClient, <App />)

expect(consoleErrorSpy).toHaveBeenCalledWith(
'skipToken is not allowed for useSuspenseQuery',
)
consoleErrorSpy.mockRestore()
})
})

0 comments on commit c181c71

Please sign in to comment.