Skip to content

Commit 8670a49

Browse files
authored
test(query-core/queryCache): add tests for 'build' with and without provided 'queryHash' (#10252)
1 parent 67cf8b6 commit 8670a49

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

packages/query-core/src/__tests__/queryCache.test.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
22
import { queryKey, sleep } from '@tanstack/query-test-utils'
3-
import { QueryCache, QueryClient, QueryObserver } from '..'
3+
import { QueryCache, QueryClient, QueryObserver, hashKey } from '..'
44

55
describe('queryCache', () => {
66
let queryClient: QueryClient
@@ -352,6 +352,31 @@ describe('queryCache', () => {
352352
})
353353
})
354354

355+
describe('build', () => {
356+
test('should compute queryHash from queryKey when queryHash is not provided', () => {
357+
const key = queryKey()
358+
359+
const query = queryCache.build(queryClient, {
360+
queryKey: key,
361+
})
362+
363+
expect(query.queryHash).toBe(hashKey(key))
364+
})
365+
366+
test('should use provided queryHash instead of computing it', () => {
367+
const key = queryKey()
368+
const customHash = 'custom-hash'
369+
370+
const query = queryCache.build(queryClient, {
371+
queryKey: key,
372+
queryHash: customHash,
373+
})
374+
375+
expect(query.queryHash).toBe(customHash)
376+
expect(query.queryHash).not.toBe(hashKey(key))
377+
})
378+
})
379+
355380
describe('QueryCache.add', () => {
356381
test('should not try to add a query already added to the cache', async () => {
357382
const key = queryKey()

0 commit comments

Comments
 (0)