|
4 | 4 | describe,
|
5 | 5 | expect,
|
6 | 6 | expectTypeOf,
|
| 7 | + it, |
7 | 8 | test,
|
8 | 9 | vi,
|
9 | 10 | } from 'vitest'
|
@@ -1427,4 +1428,70 @@ describe('queryObserver', () => {
|
1427 | 1428 | const result = observer.getCurrentResult()
|
1428 | 1429 | expect(result.isEnabled).toBe(true)
|
1429 | 1430 | })
|
| 1431 | + |
| 1432 | + describe('StrictMode behavior', () => { |
| 1433 | + it('should deduplicate calls to queryFn', async () => { |
| 1434 | + const key = queryKey() |
| 1435 | + const queryFn = vi.fn(async () => { |
| 1436 | + await sleep(50) |
| 1437 | + return 'data' |
| 1438 | + }) |
| 1439 | + const observer = new QueryObserver(queryClient, { |
| 1440 | + queryKey: key, |
| 1441 | + queryFn, |
| 1442 | + }) |
| 1443 | + |
| 1444 | + const unsubscribe1 = observer.subscribe(vi.fn()) |
| 1445 | + |
| 1446 | + await vi.advanceTimersByTimeAsync(5) |
| 1447 | + unsubscribe1() |
| 1448 | + |
| 1449 | + // replicate strict mode behavior |
| 1450 | + await vi.advanceTimersByTimeAsync(5) |
| 1451 | + const unsubscribe2 = observer.subscribe(vi.fn()) |
| 1452 | + |
| 1453 | + await vi.advanceTimersByTimeAsync(40) |
| 1454 | + |
| 1455 | + expect(queryClient.getQueryState(key)).toMatchObject({ |
| 1456 | + status: 'success', |
| 1457 | + data: 'data', |
| 1458 | + }) |
| 1459 | + |
| 1460 | + expect(queryFn).toHaveBeenCalledTimes(1) |
| 1461 | + |
| 1462 | + unsubscribe2() |
| 1463 | + }) |
| 1464 | + |
| 1465 | + it('should resolve with data when signal was consumed', async () => { |
| 1466 | + const key = queryKey() |
| 1467 | + const queryFn = vi.fn(async ({ signal }) => { |
| 1468 | + await sleep(50) |
| 1469 | + return 'data' + String(signal) |
| 1470 | + }) |
| 1471 | + const observer = new QueryObserver(queryClient, { |
| 1472 | + queryKey: key, |
| 1473 | + queryFn, |
| 1474 | + }) |
| 1475 | + |
| 1476 | + const unsubscribe1 = observer.subscribe(vi.fn()) |
| 1477 | + |
| 1478 | + await vi.advanceTimersByTimeAsync(5) |
| 1479 | + unsubscribe1() |
| 1480 | + |
| 1481 | + // replicate strict mode behavior |
| 1482 | + await vi.advanceTimersByTimeAsync(5) |
| 1483 | + const unsubscribe2 = observer.subscribe(vi.fn()) |
| 1484 | + |
| 1485 | + await vi.advanceTimersByTimeAsync(50) |
| 1486 | + |
| 1487 | + expect(queryClient.getQueryState(key)).toMatchObject({ |
| 1488 | + status: 'success', |
| 1489 | + data: 'data[object AbortSignal]', |
| 1490 | + }) |
| 1491 | + |
| 1492 | + expect(queryFn).toHaveBeenCalledTimes(2) |
| 1493 | + |
| 1494 | + unsubscribe2() |
| 1495 | + }) |
| 1496 | + }) |
1430 | 1497 | })
|
0 commit comments