Skip to content

Commit 28177f5

Browse files
test(query-devtools/Devtools): add test for restoring previous query options after 'Trigger Loading' is clicked (#10701)
* test(query-devtools/Devtools): add test for restoring previous query options after 'Trigger Loading' is clicked * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 5dce547 commit 28177f5

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,39 @@ describe('Devtools', () => {
561561
'pending',
562562
)
563563
})
564+
565+
it('should restore the previous query options when "Restore Loading" is clicked after "Trigger Loading"', async () => {
566+
const queryFn = vi.fn(() => Promise.resolve('original'))
567+
queryClient.prefetchQuery({
568+
queryKey: ['action-restore-loading'],
569+
queryFn,
570+
})
571+
await vi.advanceTimersByTimeAsync(0)
572+
expect(queryFn).toHaveBeenCalledTimes(1)
573+
574+
const rendered = renderDevtools({ initialIsOpen: true })
575+
576+
fireEvent.click(
577+
rendered.getByLabelText(/Query key \["action-restore-loading"\]/),
578+
)
579+
580+
// First click puts the query into a pending state with `data: undefined`
581+
// and stashes the original options in `fetchMeta.__previousQueryOptions`.
582+
fireEvent.click(rendered.getByText('Trigger Loading'))
583+
expect(
584+
queryClient.getQueryState(['action-restore-loading'])?.status,
585+
).toBe('pending')
586+
587+
// Second click runs `restoreQueryAfterLoadingOrError`, which cancels the
588+
// never-resolving fetch and refetches with the stashed options.
589+
fireEvent.click(rendered.getByText('Restore Loading'))
590+
await vi.advanceTimersByTimeAsync(0)
591+
592+
expect(queryFn).toHaveBeenCalledTimes(2)
593+
expect(queryClient.getQueryData(['action-restore-loading'])).toBe(
594+
'original',
595+
)
596+
})
564597
})
565598

566599
describe('mutation details', () => {

0 commit comments

Comments
 (0)