|
1 | 1 | import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' |
2 | 2 | import { queryKey, sleep } from '@tanstack/query-test-utils' |
3 | | -import { QueryCache, QueryClient, QueryObserver } from '..' |
| 3 | +import { QueryCache, QueryClient, QueryObserver, hashKey } from '..' |
4 | 4 |
|
5 | 5 | describe('queryCache', () => { |
6 | 6 | let queryClient: QueryClient |
@@ -352,6 +352,31 @@ describe('queryCache', () => { |
352 | 352 | }) |
353 | 353 | }) |
354 | 354 |
|
| 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 | + |
355 | 380 | describe('QueryCache.add', () => { |
356 | 381 | test('should not try to add a query already added to the cache', async () => { |
357 | 382 | const key = queryKey() |
|
0 commit comments