From 2cfd16413d0d076a31e756f50e8bc18d68a48d3e Mon Sep 17 00:00:00 2001 From: Otacilio Lacerda Date: Wed, 8 Jul 2020 15:11:28 +0200 Subject: [PATCH] fix: (queryInstance.clearInterval) is not called on unmount (#717) --- src/core/queryInstance.js | 2 +- src/core/tests/queryCache.test.js | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/core/queryInstance.js b/src/core/queryInstance.js index 22ce7ee9dd..6e3d925295 100644 --- a/src/core/queryInstance.js +++ b/src/core/queryInstance.js @@ -70,7 +70,7 @@ export function makeQueryInstance(query, onStateUpdate) { query.instances = query.instances.filter(d => d.id !== instance.id) if (!query.instances.length) { - query.clearIntervals() + instance.clearInterval() query.cancel() if (!isServer) { diff --git a/src/core/tests/queryCache.test.js b/src/core/tests/queryCache.test.js index a379aa94d1..9868fef230 100644 --- a/src/core/tests/queryCache.test.js +++ b/src/core/tests/queryCache.test.js @@ -72,7 +72,7 @@ describe('queryCache', () => { expect(callback).toHaveBeenCalled() }) - test('should notify subsribers when new query with initialData is added', async () => { + test('should notify subscribers when new query with initialData is added', async () => { const callback = jest.fn() queryCache.subscribe(callback) @@ -156,6 +156,20 @@ describe('queryCache', () => { expect(query.state.isStale).toBe(false) }) + test('query interval is cleared when unsubscribed to a refetchInterval query', async () => { + const queryKey = 'key' + const fetchData = () => Promise.resolve('data') + await queryCache.prefetchQuery(queryKey, fetchData, { cacheTime: 0, refetchInterval: 1 }) + const query = queryCache.getQuery(queryKey) + const instance = query.subscribe() + instance.updateConfig(query.config) + expect(instance.refetchIntervalId).not.toBeUndefined() + instance.unsubscribe() + expect(instance.refetchIntervalId).toBeUndefined() + await sleep(10) + expect(queryCache.getQuery(queryKey)).toBeUndefined() + }) + test('query is garbage collected when unsubscribed to', async () => { const queryKey = 'key' const fetchData = () => Promise.resolve('data')