Skip to content

test(query-core): add test case for query #9145

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
76 changes: 75 additions & 1 deletion packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import {
sleep,
} from '@tanstack/query-test-utils'
import {
Query,
QueryClient,
QueryObserver,
dehydrate,
hydrate,
isCancelledError,
} from '..'
import { hashQueryKeyByOptions } from '../utils'
import { mockOnlineManagerIsOnline, setIsServer } from './utils'
import type { QueryCache, QueryFunctionContext, QueryObserverResult } from '..'
import type {
QueryCache,
QueryFunctionContext,
QueryKey,
QueryObserverResult,
} from '..'

describe('query', () => {
let queryClient: QueryClient
Expand Down Expand Up @@ -1019,4 +1026,71 @@ describe('query', () => {
await vi.advanceTimersByTimeAsync(10)
expect(query.state.status).toBe('error')
})

test('should use persister if provided', async () => {
const key = queryKey()

await queryClient.prefetchQuery({
queryKey: key,
queryFn: () => 'data',
persister: () => Promise.resolve('persisted data'),
})

const query = queryCache.find({ queryKey: key })!
expect(query.state.data).toBe('persisted data')
})

test('should use queryFn from observer if not provided in options', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

we could also assert here that query.options.queryFn is set to the one of the observer at the end.

const key = queryKey()
const observer = new QueryObserver(queryClient, {
queryKey: key,
queryFn: () => Promise.resolve('data'),
})

const query = new Query({
client: queryClient,
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
})

query.addObserver(observer)

await query.fetch()
const result = await query.state.data
expect(result).toBe('data')
})

test('should log error when queryKey is not an array', async () => {
const consoleMock = vi.spyOn(console, 'error')
const key: unknown = 'string-key'

await queryClient.prefetchQuery({
queryKey: key as QueryKey,
queryFn: () => 'data',
})

expect(consoleMock).toHaveBeenCalledWith(
"As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']",
)

consoleMock.mockRestore()
})

test('should call initialData function when it is a function', () => {
const key = queryKey()
const initialDataFn = vi.fn(() => 'initial data')

const query = new Query({
client: queryClient,
queryKey: key,
queryHash: hashQueryKeyByOptions(key),
options: {
queryFn: () => 'data',
initialData: initialDataFn,
},
})

expect(initialDataFn).toHaveBeenCalledTimes(1)
expect(query.state.data).toBe('initial data')
})
})
Loading