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
22 changes: 21 additions & 1 deletion packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest'
import { waitFor } from '@testing-library/react'
import { QueryObserver, isCancelledError } from '..'
import { QueryObserver, dehydrate, hydrate, isCancelledError } from '..'
import {
createQueryClient,
mockOnlineManagerIsOnline,
Expand Down Expand Up @@ -389,6 +389,26 @@ describe('query', () => {
expect(query.state.fetchStatus).toBe('idle') // not be loading any longer
})

test('should reset to default state when created from hydration', async () => {
const client = createQueryClient()
await client.prefetchQuery({
queryKey: ['string'],
queryFn: () => Promise.resolve('string'),
})

const dehydrated = dehydrate(client)

const hydrationClient = createQueryClient()
hydrate(hydrationClient, dehydrated)

expect(hydrationClient.getQueryData(['string'])).toBe('string')

const query = hydrationClient.getQueryCache().find({ queryKey: ['string'] })
query?.reset()

expect(hydrationClient.getQueryData(['string'])).toBe(undefined)
})

test('should be able to refetch a cancelled query', async () => {
const key = queryKey()

Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ export class Query<
this.#cache = config.cache
this.queryKey = config.queryKey
this.queryHash = config.queryHash
this.#initialState = config.state || getDefaultState(this.options)
this.state = this.#initialState
this.#initialState = getDefaultState(this.options)
this.state = config.state ?? this.#initialState
this.scheduleGc()
}
get meta(): QueryMeta | undefined {
Expand Down