Skip to content

Commit 69aea62

Browse files
kandrosTkDodo
andauthored
feat(react-query-devtools): add button to clear cache (#5186)
Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 1c5165e commit 69aea62

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

packages/react-query-devtools/src/__tests__/devtools.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,43 @@ describe('ReactQueryDevtools', () => {
661661
expect(filterInput.value).toEqual('posts')
662662
})
663663

664+
it('should not show queries after clear', async () => {
665+
const { queryClient, queryCache } = createQueryClient()
666+
667+
function Page() {
668+
const query1Result = useQuery(['query-1'], async () => {
669+
return 'query-1-result'
670+
})
671+
const query2Result = useQuery(['query-2'], async () => {
672+
return 'query-2-result'
673+
})
674+
const query3Result = useQuery(['query-3'], async () => {
675+
return 'query-3-result'
676+
})
677+
678+
return (
679+
<div>
680+
<h1>
681+
{query1Result.data} {query2Result.data} {query3Result.data}{' '}
682+
</h1>
683+
</div>
684+
)
685+
}
686+
687+
renderWithClient(queryClient, <Page />)
688+
689+
fireEvent.click(
690+
screen.getByRole('button', { name: /open react query devtools/i }),
691+
)
692+
693+
expect(queryCache.getAll()).toHaveLength(3)
694+
695+
const clearButton = screen.getByLabelText(/clear/i)
696+
fireEvent.click(clearButton)
697+
698+
expect(queryCache.getAll()).toHaveLength(0)
699+
})
700+
664701
it('style should have a nonce', async () => {
665702
const { queryClient } = createQueryClient()
666703

packages/react-query-devtools/src/devtools.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ export const ReactQueryDevtoolsPanel = React.forwardRef<
597597
<Logo aria-hidden />
598598
<ScreenReader text="Close React Query Devtools" />
599599
</button>
600+
600601
<div
601602
style={{
602603
display: 'flex',
@@ -673,6 +674,18 @@ export const ReactQueryDevtoolsPanel = React.forwardRef<
673674
>
674675
{baseSort === 1 ? '⬆ Asc' : '⬇ Desc'}
675676
</Button>
677+
<Button
678+
title="Clear cache"
679+
aria-label="Clear cache"
680+
type="button"
681+
onClick={() => queryCache.clear()}
682+
style={{
683+
padding: '.3em .4em',
684+
marginRight: '.5em',
685+
}}
686+
>
687+
Clear
688+
</Button>
676689
<Button
677690
type="button"
678691
onClick={() => {
@@ -1330,5 +1343,7 @@ const QueryRow = React.memo(
13301343
},
13311344
)
13321345

1346+
QueryRow.displayName = 'QueryRow'
1347+
13331348
// eslint-disable-next-line @typescript-eslint/no-empty-function
13341349
function noop() {}

0 commit comments

Comments
 (0)