diff --git a/packages/query-core/src/__tests__/queryCache.test.tsx b/packages/query-core/src/__tests__/queryCache.test.tsx index 6edb5598f7..821efa8061 100644 --- a/packages/query-core/src/__tests__/queryCache.test.tsx +++ b/packages/query-core/src/__tests__/queryCache.test.tsx @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' import { queryKey, sleep } from '@tanstack/query-test-utils' -import { QueryCache, QueryClient, QueryObserver } from '..' +import { QueryCache, QueryClient, QueryObserver, hashKey } from '..' describe('queryCache', () => { let queryClient: QueryClient @@ -352,6 +352,31 @@ describe('queryCache', () => { }) }) + describe('build', () => { + test('should compute queryHash from queryKey when queryHash is not provided', () => { + const key = queryKey() + + const query = queryCache.build(queryClient, { + queryKey: key, + }) + + expect(query.queryHash).toBe(hashKey(key)) + }) + + test('should use provided queryHash instead of computing it', () => { + const key = queryKey() + const customHash = 'custom-hash' + + const query = queryCache.build(queryClient, { + queryKey: key, + queryHash: customHash, + }) + + expect(query.queryHash).toBe(customHash) + expect(query.queryHash).not.toBe(hashKey(key)) + }) + }) + describe('QueryCache.add', () => { test('should not try to add a query already added to the cache', async () => { const key = queryKey()