Repository: tannerlinsley/react-query (ts)

Issue:
queryInstance.clearInterval() is not called on Unmount
When a component unmounts `queryInstance.unsubscribe()` tries to clear intervals here:
https://github.com/tannerlinsley/react-query/blob/master/src/core/queryInstance.js#L73
Note that `query.instances.length===0`

BUG:
`query.clearIntervals()` does no clear any intervals because the array it iterates over is empty.
https://github.com/tannerlinsley/react-query/blob/master/src/core/query.js#L138

Pull request description:
fix: (queryInstance.clearInterval) is not called on unmount
Fix #711 

Patch:
diff --git a/src/core/queryInstance.js b/src/core/queryInstance.js
index 22ce7ee9d..6e3d92529 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) {


Test patch:
diff --git a/src/core/tests/queryCache.test.js b/src/core/tests/queryCache.test.js
index a379aa94d..9868fef23 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')