From ce305c36e877b4fc8041c12c774fdf0f65a5bd6c Mon Sep 17 00:00:00 2001 From: morrys Date: Thu, 24 Sep 2020 23:36:58 +0200 Subject: [PATCH 1/3] test issue 115 --- __tests__/usePreloadQuery-test.tsx | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/__tests__/usePreloadQuery-test.tsx b/__tests__/usePreloadQuery-test.tsx index 52c853d3..bf35818a 100644 --- a/__tests__/usePreloadQuery-test.tsx +++ b/__tests__/usePreloadQuery-test.tsx @@ -749,5 +749,54 @@ describe('usePreloadQuery', () => { TestRenderer.act(() => jest.runAllImmediates()); expect(renderer.toJSON()).toEqual('Error Boundary'); }); + + it('issue-115', () => { + prefetched.next(environment, params, { id: '4' }); + + let data; + function Component(props) { + data = usePreloadedQuery(props.prefetched); + if (data.error) { + return 'Error Boundary'; + } + if (data.props && data.props.node) { + return data.props.node.name; + } + return data.props; + } + const renderer = TestRenderer.create( + + + , + ); + // Ensure that useEffect runs + TestRenderer.act(() => jest.runAllImmediates()); + expect(renderer.toJSON()).toEqual(null); + expect(data).toEqual(loadingData); + + dataSource.next(response); + dataSource.complete(); + TestRenderer.act(() => jest.runAllImmediates()); + expect(renderer.toJSON()).toEqual('Zuck'); + expect(data.props).toEqual({ + node: { + id: '4', + name: 'Zuck', + }, + }); + + TestRenderer.act(() => renderer.unmount()); + TestRenderer.act(() => jest.runAllImmediates()); + prefetched.next(environment, params, { id: '4' }); + dataSource.next(response); + dataSource.complete(); + TestRenderer.act(() => jest.runAllImmediates()); + expect((prefetched.getValue(environment) as any).props).toEqual({ + node: { + id: '4', + name: 'Zuck', + }, + }); + }); }); }); From 4be49a4103d5e3a38880407d1c2d08a60bdf6713 Mon Sep 17 00:00:00 2001 From: morrys Date: Thu, 24 Sep 2020 23:43:19 +0200 Subject: [PATCH 2/3] fix issue 115 When dispose is made it is necessary to recreate the queryFetcher and reset the loadQuery data as in the initial state. This ensures that the next call is made as if a new loadQuery were created. --- src/loadQuery.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/loadQuery.ts b/src/loadQuery.ts index 13d11e78..284d0fc8 100644 --- a/src/loadQuery.ts +++ b/src/loadQuery.ts @@ -8,11 +8,10 @@ const internalLoadQuery = promise = false, ): LoadQuery => { let data: RenderProps | null | Promise = null; - let listener; + let listener = undefined; + let queryFetcher = new QueryFetcher(true); - const queryFetcher = new QueryFetcher(true); - - const prev = { + let prev = { environment: null, gqlQuery: null, variables: null, @@ -22,7 +21,16 @@ const internalLoadQuery = const dispose = (): void => { queryFetcher.dispose(); - listener = null; + queryFetcher = new QueryFetcher(true); + listener = undefined; + data = null; + prev = { + environment: null, + gqlQuery: null, + variables: null, + options: null, + query: null, + }; }; const next = ( From ea45b5331b07f88bd073ce2e3958cfe7156e62db Mon Sep 17 00:00:00 2001 From: morrys Date: Fri, 25 Sep 2020 01:36:27 +0200 Subject: [PATCH 3/3] 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,