|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
2 | | -import { ref } from 'vue-demi' |
| 2 | +import { ref, unref } from 'vue-demi' |
3 | 3 | import { QueryClient as QueryClientOrigin } from '@tanstack/query-core' |
4 | 4 | import { QueryClient } from '../queryClient' |
5 | 5 | import { infiniteQueryOptions } from '../infiniteQueryOptions' |
| 6 | +import { queryOptions } from '../queryOptions' |
6 | 7 |
|
7 | 8 | vi.mock('@tanstack/query-core', async () => { |
8 | 9 | const actual = await vi.importActual<{ |
@@ -338,6 +339,57 @@ describe('QueryCache', () => { |
338 | 339 | }) |
339 | 340 | }) |
340 | 341 |
|
| 342 | + describe('query', () => { |
| 343 | + it('should properly unwrap queryKey', () => { |
| 344 | + const queryClient = new QueryClient() |
| 345 | + |
| 346 | + queryClient.query({ |
| 347 | + queryKey: queryKeyRef, |
| 348 | + }) |
| 349 | + |
| 350 | + expect(QueryClientOrigin.prototype.query).toBeCalledWith({ |
| 351 | + queryKey: queryKeyUnref, |
| 352 | + }) |
| 353 | + }) |
| 354 | + |
| 355 | + it('should properly unwrap enabled, staleTime, and select', () => { |
| 356 | + const queryClient = new QueryClient() |
| 357 | + const enabled = () => false |
| 358 | + const staleTime = () => 1000 |
| 359 | + const select = (data: string) => data.length |
| 360 | + |
| 361 | + queryClient.query({ |
| 362 | + queryKey: queryKeyRef, |
| 363 | + enabled: ref(enabled), |
| 364 | + staleTime: ref(staleTime), |
| 365 | + select: ref(select), |
| 366 | + }) |
| 367 | + |
| 368 | + expect(QueryClientOrigin.prototype.query).toBeCalledWith({ |
| 369 | + queryKey: queryKeyUnref, |
| 370 | + enabled, |
| 371 | + staleTime, |
| 372 | + select, |
| 373 | + }) |
| 374 | + }) |
| 375 | + |
| 376 | + it('should properly unwrap queryOptions getter', () => { |
| 377 | + const queryClient = new QueryClient() |
| 378 | + |
| 379 | + const options = queryOptions(() => ({ |
| 380 | + queryKey: queryKeyRef, |
| 381 | + queryFn: fn, |
| 382 | + })) |
| 383 | + |
| 384 | + queryClient.query(options) |
| 385 | + |
| 386 | + expect(QueryClientOrigin.prototype.query).toBeCalledWith({ |
| 387 | + queryKey: queryKeyUnref, |
| 388 | + queryFn: fn, |
| 389 | + }) |
| 390 | + }) |
| 391 | + }) |
| 392 | + |
341 | 393 | describe('prefetchQuery', () => { |
342 | 394 | it('should properly unwrap parameters', () => { |
343 | 395 | const queryClient = new QueryClient() |
@@ -387,6 +439,80 @@ describe('QueryCache', () => { |
387 | 439 | }) |
388 | 440 | }) |
389 | 441 |
|
| 442 | + describe('infiniteQuery', () => { |
| 443 | + it('should properly unwrap queryKey, initialPageParam, pages, and select', () => { |
| 444 | + const queryClient = new QueryClient() |
| 445 | + const getNextPageParam = () => 1 |
| 446 | + const select = (data: { pages: Array<string> }) => data.pages.length |
| 447 | + |
| 448 | + queryClient.infiniteQuery({ |
| 449 | + queryKey: queryKeyRef, |
| 450 | + initialPageParam: ref(0), |
| 451 | + pages: ref(2), |
| 452 | + getNextPageParam: ref(getNextPageParam), |
| 453 | + select: ref(select), |
| 454 | + }) |
| 455 | + |
| 456 | + expect(QueryClientOrigin.prototype.infiniteQuery).toBeCalledWith( |
| 457 | + expect.objectContaining({ |
| 458 | + queryKey: queryKeyUnref, |
| 459 | + initialPageParam: 0, |
| 460 | + pages: 2, |
| 461 | + getNextPageParam, |
| 462 | + select, |
| 463 | + }), |
| 464 | + ) |
| 465 | + }) |
| 466 | + |
| 467 | + it('should properly unwrap getNextPageParam when using infiniteQueryOptions', () => { |
| 468 | + const queryClient = new QueryClient() |
| 469 | + const getNextPageParam = () => 12 |
| 470 | + |
| 471 | + const options = infiniteQueryOptions({ |
| 472 | + queryKey: queryKeyRef, |
| 473 | + initialPageParam: ref(0), |
| 474 | + getNextPageParam: ref(getNextPageParam), |
| 475 | + }) |
| 476 | + |
| 477 | + queryClient.infiniteQuery({ |
| 478 | + ...unref(options), |
| 479 | + enabled: true, |
| 480 | + staleTime: 0, |
| 481 | + pages: 1, |
| 482 | + }) |
| 483 | + |
| 484 | + expect(QueryClientOrigin.prototype.infiniteQuery).toBeCalledWith( |
| 485 | + expect.objectContaining({ |
| 486 | + queryKey: queryKeyUnref, |
| 487 | + initialPageParam: 0, |
| 488 | + pages: 1, |
| 489 | + getNextPageParam, |
| 490 | + }), |
| 491 | + ) |
| 492 | + }) |
| 493 | + |
| 494 | + it('should properly unwrap options getter', () => { |
| 495 | + const queryClient = new QueryClient() |
| 496 | + const getNextPageParam = () => 12 |
| 497 | + |
| 498 | + queryClient.infiniteQuery(() => ({ |
| 499 | + queryKey: queryKeyRef, |
| 500 | + initialPageParam: ref(0), |
| 501 | + pages: ref(1), |
| 502 | + getNextPageParam: ref(getNextPageParam), |
| 503 | + })) |
| 504 | + |
| 505 | + expect(QueryClientOrigin.prototype.infiniteQuery).toBeCalledWith( |
| 506 | + expect.objectContaining({ |
| 507 | + queryKey: queryKeyUnref, |
| 508 | + initialPageParam: 0, |
| 509 | + pages: 1, |
| 510 | + getNextPageParam, |
| 511 | + }), |
| 512 | + ) |
| 513 | + }) |
| 514 | + }) |
| 515 | + |
390 | 516 | describe('prefetchInfiniteQuery', () => { |
391 | 517 | it('should properly unwrap parameters', () => { |
392 | 518 | const queryClient = new QueryClient() |
|
0 commit comments