|
1 | 1 | import React from 'react'; |
2 | 2 | import { renderHook, RenderHookResult } from '@testing-library/react-hooks'; |
3 | | -import { interval } from 'rxjs'; |
4 | | -import { map, finalize } from 'rxjs/operators'; |
| 3 | +import { interval, fromEvent } from 'rxjs'; |
| 4 | +import { map, finalize, takeUntil } from 'rxjs/operators'; |
5 | 5 | import { QueryClient, QueryClientProvider, QueryCache } from 'react-query'; |
6 | 6 |
|
7 | 7 | import { |
@@ -531,6 +531,28 @@ describe('useSubscription', () => { |
531 | 531 | }); |
532 | 532 | }); |
533 | 533 |
|
| 534 | + describe('queryFn', () => { |
| 535 | + describe('signal', () => { |
| 536 | + it('should cancel the subscription', async () => { |
| 537 | + const finalizeFn = jest.fn(); |
| 538 | + const testSubscriptionFn = jest.fn(({ signal }) => |
| 539 | + interval(testInterval).pipe( |
| 540 | + takeUntil(fromEvent(signal, 'abort')), |
| 541 | + finalize(finalizeFn) |
| 542 | + ) |
| 543 | + ); |
| 544 | + const { unmount } = renderHook( |
| 545 | + () => useSubscription(testSubscriptionKey, testSubscriptionFn), |
| 546 | + { wrapper: Wrapper } |
| 547 | + ); |
| 548 | + expect(finalizeFn).not.toHaveBeenCalled(); |
| 549 | + |
| 550 | + unmount(); |
| 551 | + expect(finalizeFn).toHaveBeenCalled(); |
| 552 | + }); |
| 553 | + }); |
| 554 | + }); |
| 555 | + |
534 | 556 | describe('options', () => { |
535 | 557 | describe('enabled', () => { |
536 | 558 | it('should be idle while enabled = false', async () => { |
|
0 commit comments