From ea45b5331b07f88bd073ce2e3958cfe7156e62db Mon Sep 17 00:00:00 2001 From: morrys Date: Fri, 25 Sep 2020 01:36:27 +0200 Subject: [PATCH] added test dispose subscription and change return in loadquery subscribe function --- __tests__/usePreloadQuery-test.tsx | 11 +++++++++++ src/loadQuery.ts | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/__tests__/usePreloadQuery-test.tsx b/__tests__/usePreloadQuery-test.tsx index bf35818a..c4b673c7 100644 --- a/__tests__/usePreloadQuery-test.tsx +++ b/__tests__/usePreloadQuery-test.tsx @@ -798,5 +798,16 @@ describe('usePreloadQuery', () => { }, }); }); + + it('dispose subscription', () => { + const callback = jest.fn(() => {}); + const dispose = prefetched.subscribe(callback); + dispose(); + prefetched.next(environment, params, { id: '4' }); + dataSource.next(response); + dataSource.complete(); + TestRenderer.act(() => jest.runAllImmediates()); + expect(callback).not.toBeCalled(); + }); }); }); diff --git a/src/loadQuery.ts b/src/loadQuery.ts index 284d0fc8..54c71311 100644 --- a/src/loadQuery.ts +++ b/src/loadQuery.ts @@ -81,7 +81,9 @@ const internalLoadQuery = const subscribe = (callback: (value) => any): (() => void) => { listener = callback; - return dispose; + return (): void => { + listener = null; + }; }; return { next,