Skip to content

Commit 95ec4aa

Browse files
committed
test(*): add unit tests for not resubscribing when re-rendered
1 parent a7f32f3 commit 95ec4aa

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/__tests__/use-infinite-subscription.spec.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ describe('useInfiniteSubscription', () => {
5555
return { pageParams: [undefined], pages: [data] };
5656
}
5757

58+
it('should not re-subscribe when re-rendered', () => {
59+
const { subscriptionFn } = subscriptionFnFactory();
60+
const { rerender } = renderHook(
61+
() => useInfiniteSubscription(['non-primitive-key'], subscriptionFn),
62+
{ wrapper: Wrapper }
63+
);
64+
expect(subscriptionFn).toHaveBeenCalledTimes(1);
65+
subscriptionFn.mockClear();
66+
67+
rerender();
68+
expect(subscriptionFn).toHaveBeenCalledTimes(0);
69+
});
70+
5871
describe('options', () => {
5972
describe('getNextPageParam', () => {
6073
test('fetching next page', async () => {

src/__tests__/use-subscription.spec.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ describe('useSubscription', () => {
196196
expect(finalizeFn).toHaveBeenCalledTimes(1);
197197
});
198198

199+
it('should not re-subscribe when re-rendered', () => {
200+
const { rerender } = renderHook(
201+
() => useSubscription(['non-primitive-key'], testSubscriptionFn),
202+
{ wrapper: Wrapper }
203+
);
204+
expect(testSubscriptionFn).toHaveBeenCalledTimes(1);
205+
testSubscriptionFn.mockClear();
206+
207+
rerender();
208+
expect(testSubscriptionFn).toHaveBeenCalledTimes(0);
209+
});
210+
199211
test('re-subscribe when mounted/unmounted/mounted', async () => {
200212
const { result, waitForNextUpdate, unmount, rerender } = renderHook(
201213
() => useSubscription(testSubscriptionKey, testSubscriptionFn),

0 commit comments

Comments
 (0)