Skip to content

Commit 41afef1

Browse files
Katarina Antonkaciakmaciak
authored andcommitted
test(useSubscritpion): add unit test for abort controller
resolves #72
1 parent 368fb04 commit 41afef1

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/__tests__/use-subscription.spec.tsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
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';
55
import { QueryClient, QueryClientProvider, QueryCache } from 'react-query';
66

77
import {
@@ -531,6 +531,28 @@ describe('useSubscription', () => {
531531
});
532532
});
533533

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+
534556
describe('options', () => {
535557
describe('enabled', () => {
536558
it('should be idle while enabled = false', async () => {

0 commit comments

Comments
 (0)