Skip to content

Commit

Permalink
add clear test case, fix empty key
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jun 6, 2022
1 parent 5357d34 commit 6b5cba6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
3 changes: 1 addition & 2 deletions _internal/utils/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ export async function internalMutate<Data>(
const revalidate = options.revalidate !== false
const rollbackOnError = options.rollbackOnError !== false

if (!_key) return

const KEYS = (SWRGlobalState.get(cache) as GlobalState)[6]

// If 2nd arg is key filter, return the mutation results of filtered keys
Expand All @@ -63,6 +61,7 @@ export async function internalMutate<Data>(
return await Promise.all(matchedKeys.map(mutateByKey))
} else {
const [serializedKey] = serialize(_key)
if (!serializedKey) return
return await mutateByKey(serializedKey)
}

Expand Down
6 changes: 5 additions & 1 deletion core/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,11 @@ export const useSWRHandler = <Data = any, Error = any>(
// By using `bind` we don't need to modify the size of the rest arguments.
// Due to https://github.com/microsoft/TypeScript/issues/37181, we have to
// cast it to any for now.
internalMutate.bind(UNDEFINED, cache, keyRef.current) as any,
internalMutate.bind(
UNDEFINED,
cache,
(k: string) => k === keyRef.current
) as any,
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)
Expand Down
50 changes: 50 additions & 0 deletions test/use-swr-local-mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1558,4 +1558,54 @@ describe('useSWR - local mutation', () => {

expect(mutationOneResults).toEqual(['value-first-g0'])
})

it('should remove all key value pairs when clear cache through key filter', async () => {
const key = createKey()
const mutationOneResults = []

function Page() {
const { data: data1 } = useSWR(key + 'first')
const { data: data2 } = useSWR(key + 'second')
const { mutate } = useSWRConfig()
return (
<div>
<span
data-testid="mutator-filter-all"
onClick={async () => {
const promises = ['first', 'second'].map(async name => {
await mutate(key + name, `value-${name}`, false)
})
await Promise.all(promises)
}}
/>
<span
data-testid="clear-all"
onClick={async () => {
const res = await mutate(() => true, undefined, false)
mutationOneResults.push(...res)
}}
/>
<p>first:{data1}</p>
<p>second:{data2}</p>
</div>
)
}
renderWithConfig(<Page />)

// add and mutate `first` and `second`
fireEvent.click(screen.getByTestId('mutator-filter-all'))
await nextTick()

await screen.findByText('first:value-first')
await screen.findByText('second:value-second')

// reset all keys to undefined
fireEvent.click(screen.getByTestId('clear-all'))
await nextTick()

await screen.findByText('first:')
await screen.findByText('second:')

expect(mutationOneResults).toEqual([undefined])
})
})

0 comments on commit 6b5cba6

Please sign in to comment.