Skip to content

Commit

Permalink
fix(core): query should reset to default state even when created from…
Browse files Browse the repository at this point in the history
… hydration (#7837)
  • Loading branch information
TkDodo committed Aug 1, 2024
1 parent c7245c9 commit bbb2244
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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

0 comments on commit bbb2244

Please sign in to comment.