Repository navigation
feat: query cache provider #476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tannerlinsley
merged 6 commits into
TanStack:master
from
team-griffin:feat/use-query-cache
May 15, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
44dc8ef
feat: query cache provider
7becaad
fix: track multiple query caches
7bf45e1
test: add in-depth tests around query cache providers
3c8329d
style: format touched files
87bf958
fixup! style: format touched files
7371990
docs: correct readme description
jackmellis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import React, { useEffect } from 'react' | ||
| import { render, cleanup, waitForElement } from '@testing-library/react' | ||
| import { | ||
| ReactQueryCacheProvider, | ||
| makeQueryCache, | ||
| queryCache, | ||
| useQuery, | ||
| useQueryCache, | ||
| } from '../index' | ||
| import { sleep } from './utils' | ||
|
|
||
| describe('ReactQueryCacheProvider', () => { | ||
| afterEach(() => { | ||
| cleanup() | ||
| queryCache.clear() | ||
| }) | ||
|
|
||
| test('when not used, falls back to global cache', async () => { | ||
| function Page() { | ||
| const { data } = useQuery('test', async () => { | ||
| await sleep(10) | ||
| return 'test' | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <h1>{data}</h1> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const rendered = render(<Page />) | ||
|
|
||
| await waitForElement(() => rendered.getByText('test')) | ||
|
|
||
| expect(queryCache.getQuery('test')).toBeDefined() | ||
| }) | ||
| test('sets a specific cache for all queries to use', async () => { | ||
| const cache = makeQueryCache() | ||
|
|
||
| function Page() { | ||
| const { data } = useQuery('test', async () => { | ||
| await sleep(10) | ||
| return 'test' | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <h1>{data}</h1> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const rendered = render( | ||
| <ReactQueryCacheProvider queryCache={cache}> | ||
| <Page /> | ||
| </ReactQueryCacheProvider> | ||
| ) | ||
|
|
||
| await waitForElement(() => rendered.getByText('test')) | ||
|
|
||
| expect(queryCache.getQuery('test')).not.toBeDefined() | ||
| expect(cache.getQuery('test')).toBeDefined() | ||
| }) | ||
| test('implicitly creates a new cache for all queries to use', async () => { | ||
| function Page() { | ||
| const { data } = useQuery('test', async () => { | ||
| await sleep(10) | ||
| return 'test' | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <h1>{data}</h1> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const rendered = render( | ||
| <ReactQueryCacheProvider> | ||
| <Page /> | ||
| </ReactQueryCacheProvider> | ||
| ) | ||
|
|
||
| await waitForElement(() => rendered.getByText('test')) | ||
|
|
||
| expect(queryCache.getQuery('test')).not.toBeDefined() | ||
| }) | ||
| test('allows multiple caches to be partitioned', async () => { | ||
| const cache1 = makeQueryCache() | ||
| const cache2 = makeQueryCache() | ||
|
|
||
| function Page1() { | ||
| const { data } = useQuery('test1', async () => { | ||
| await sleep(10) | ||
| return 'test1' | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <h1>{data}</h1> | ||
| </div> | ||
| ) | ||
| } | ||
| function Page2() { | ||
| const { data } = useQuery('test2', async () => { | ||
| await sleep(10) | ||
| return 'test2' | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <h1>{data}</h1> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const rendered = render( | ||
| <> | ||
| <ReactQueryCacheProvider queryCache={cache1}> | ||
| <Page1 /> | ||
| </ReactQueryCacheProvider> | ||
| <ReactQueryCacheProvider queryCache={cache2}> | ||
| <Page2 /> | ||
| </ReactQueryCacheProvider> | ||
| </> | ||
| ) | ||
|
|
||
| await waitForElement(() => rendered.getByText('test1')) | ||
| await waitForElement(() => rendered.getByText('test2')) | ||
|
|
||
| expect(cache1.getQuery('test1')).toBeDefined() | ||
| expect(cache1.getQuery('test2')).not.toBeDefined() | ||
| expect(cache2.getQuery('test1')).not.toBeDefined() | ||
| expect(cache2.getQuery('test2')).toBeDefined() | ||
| }) | ||
| test('when cache changes, previous cache is cleaned', () => { | ||
| let caches = [] | ||
| const customCache = makeQueryCache() | ||
|
|
||
| function Page() { | ||
| const queryCache = useQueryCache() | ||
| useEffect(() => { | ||
| caches.push(queryCache) | ||
| }, [queryCache]) | ||
|
|
||
| const { data } = useQuery('test', async () => { | ||
| await sleep(10) | ||
| return 'test' | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <h1>{data}</h1> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function App({ cache }) { | ||
| return ( | ||
| <ReactQueryCacheProvider queryCache={cache}> | ||
| <Page /> | ||
| </ReactQueryCacheProvider> | ||
| ) | ||
| } | ||
|
|
||
| const rendered = render(<App />) | ||
|
|
||
| expect(caches).toHaveLength(1) | ||
| jest.spyOn(caches[0], 'clear') | ||
|
|
||
| rendered.rerender(<App cache={customCache} />) | ||
|
|
||
| expect(caches).toHaveLength(2) | ||
| expect(caches[0].clear).toHaveBeenCalled() | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.