Skip to content

Reset cache between tests #1309

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
merged 3 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module.exports = {
},
globals: {
'ts-jest': {
tsconfig: 'test/tsconfig.json'
tsconfig: 'test/tsconfig.json',
diagnostics: false,
}
}
},
}
46 changes: 27 additions & 19 deletions test/use-swr-cache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import useSWR, { createCache, SWRConfig } from 'swr'
import { sleep, createKey, nextTick, focusOn } from './utils'

describe('useSWR - cache', () => {
let provider

beforeEach(() => {
provider = new Map()
})

it('should be able to update the cache', async () => {
const fetcher = _key => 'res:' + _key
const keys = [createKey(), createKey()]
Expand All @@ -15,21 +21,20 @@ describe('useSWR - cache', () => {
return <div onClick={() => setIndex(1)}>{data}</div>
}

const customCache = new Map()
const { cache } = createCache(customCache)
const { cache } = createCache(provider)
const { container } = render(
<SWRConfig value={{ cache }}>
<Section />
</SWRConfig>
)
await screen.findByText(fetcher(keys[0]))

expect(customCache.get(keys[1])).toBe(undefined)
expect(provider.get(keys[1])).toBe(undefined)
fireEvent.click(container.firstElementChild)
await act(() => sleep(10))

expect(customCache.get(keys[0])).toBe(fetcher(keys[0]))
expect(customCache.get(keys[1])).toBe(fetcher(keys[1]))
expect(provider.get(keys[0])).toBe(fetcher(keys[0]))
expect(provider.get(keys[1])).toBe(fetcher(keys[1]))
})

it('should be able to read from the initial cache with updates', async () => {
Expand All @@ -44,8 +49,8 @@ describe('useSWR - cache', () => {
return <div>{data}</div>
}

const customCache = new Map([[key, 'cached value']])
const { cache } = createCache(customCache)
provider = new Map([[key, 'cached value']])
const { cache } = createCache(provider)
render(
<SWRConfig value={{ cache }}>
<Page />
Expand All @@ -64,8 +69,8 @@ describe('useSWR - cache', () => {
return <div>{data}</div>
}

const customCache = new Map([[key, 'cached value']])
const { cache, mutate } = createCache(customCache)
provider = new Map([[key, 'cached value']])
const { cache, mutate } = createCache(provider)
render(
<SWRConfig value={{ cache }}>
<Page />
Expand All @@ -78,10 +83,10 @@ describe('useSWR - cache', () => {

it('should support multi-level cache', async () => {
const key = createKey()
const customCache1 = new Map([[key, '1']])
const { cache: cache1 } = createCache(customCache1)
const customCache2 = new Map([[key, '2']])
const { cache: cache2 } = createCache(customCache2)
const provider1 = new Map([[key, '1']])
const { cache: cache1 } = createCache(provider1)
const provider2 = new Map([[key, '2']])
const { cache: cache2 } = createCache(provider2)

// Nested components with the same cache key can get different values.
function Foo() {
Expand Down Expand Up @@ -110,10 +115,10 @@ describe('useSWR - cache', () => {

it('should support isolated cache', async () => {
const key = createKey()
const customCache1 = new Map([[key, '1']])
const { cache: cache1 } = createCache(customCache1)
const customCache2 = new Map([[key, '2']])
const { cache: cache2 } = createCache(customCache2)
const provider1 = new Map([[key, '1']])
const { cache: cache1 } = createCache(provider1)
const provider2 = new Map([[key, '2']])
const { cache: cache2 } = createCache(provider2)

// Nested components with the same cache key can get different values.
function Foo() {
Expand All @@ -140,7 +145,7 @@ describe('useSWR - cache', () => {

it('should honor createCache provider options', async () => {
const key = createKey()
const provider = new Map([[key, 0]])
provider = new Map([[key, 0]])
const { cache } = createCache(provider, {
setupOnFocus() {
/* do nothing */
Expand Down Expand Up @@ -177,7 +182,6 @@ describe('useSWR - cache', () => {

it('should work with revalidateOnFocus', async () => {
const key = createKey()
const provider = new Map()
const { cache } = createCache(provider)
let value = 0
function Foo() {
Expand All @@ -201,4 +205,8 @@ describe('useSWR - cache', () => {
await focusOn(window)
screen.getByText('1')
})

it('should clear cache between tests', async () => {
expect(provider.size).toBe(0)
})
})