-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
test(react-query/useSuspenseQueries): use precise time in 'advanceTimersByTimeAsync', and add 'expect' using 'toBeInTheDocument' #9450
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
Changes from all commits
6a76820
5456e5a
8c7a1ed
bdf6688
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -179,10 +179,10 @@ describe('useSuspenseQueries', () => { | |||||
</React.Suspense>, | ||||||
) | ||||||
|
||||||
rendered.getByText('loading...') | ||||||
expect(rendered.getByText('loading...')).toBeInTheDocument() | ||||||
expect(spy).not.toHaveBeenCalled() | ||||||
await act(() => vi.advanceTimersByTimeAsync(30)) | ||||||
rendered.getByText('data') | ||||||
expect(rendered.getByText('data')).toBeInTheDocument() | ||||||
expect(spy).toHaveBeenCalled() | ||||||
}) | ||||||
}) | ||||||
|
@@ -241,9 +241,9 @@ describe('useSuspenseQueries 2', () => { | |||||
</React.Suspense>, | ||||||
) | ||||||
|
||||||
rendered.getByText('loading') | ||||||
await act(() => vi.advanceTimersByTimeAsync(21)) | ||||||
rendered.getByText('data: 1,2') | ||||||
expect(rendered.getByText('loading')).toBeInTheDocument() | ||||||
await act(() => vi.advanceTimersByTimeAsync(20)) | ||||||
expect(rendered.getByText('data: 1,2')).toBeInTheDocument() | ||||||
expect(results).toEqual(['1', '2', 'loading']) | ||||||
}) | ||||||
|
||||||
|
@@ -296,10 +296,10 @@ describe('useSuspenseQueries 2', () => { | |||||
</React.Suspense>, | ||||||
) | ||||||
|
||||||
rendered.getByText('loading') | ||||||
expect(rendered.getByText('loading')).toBeInTheDocument() | ||||||
expect(refs.length).toBe(2) | ||||||
await act(() => vi.advanceTimersByTimeAsync(21)) | ||||||
rendered.getByText('data: 1,2') | ||||||
await act(() => vi.advanceTimersByTimeAsync(20)) | ||||||
expect(rendered.getByText('data: 1,2')).toBeInTheDocument() | ||||||
expect(refs[0]).toBe(refs[1]) | ||||||
}) | ||||||
|
||||||
|
@@ -311,7 +311,7 @@ describe('useSuspenseQueries 2', () => { | |||||
|
||||||
const { data } = useSuspenseQuery({ | ||||||
queryKey: [id], | ||||||
queryFn: () => Promise.resolve(`Data ${id}`), | ||||||
queryFn: () => sleep(10).then(() => `Data ${id}`), | ||||||
}) | ||||||
|
||||||
// defensive guard here | ||||||
|
@@ -335,8 +335,8 @@ describe('useSuspenseQueries 2', () => { | |||||
) | ||||||
|
||||||
expect(rendered.getByText('loading')).toBeInTheDocument() | ||||||
await act(() => vi.advanceTimersByTimeAsync(0)) | ||||||
rendered.getByText('Data 0') | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
expect(rendered.getByText('Data 0')).toBeInTheDocument() | ||||||
|
||||||
// go offline | ||||||
document.dispatchEvent(new CustomEvent('offline')) | ||||||
|
@@ -348,7 +348,7 @@ describe('useSuspenseQueries 2', () => { | |||||
document.dispatchEvent(new CustomEvent('online')) | ||||||
|
||||||
fireEvent.click(rendered.getByText('fetch')) | ||||||
await act(() => vi.advanceTimersByTimeAsync(0)) | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
// query should resume | ||||||
expect(rendered.getByText('Data 1')).toBeInTheDocument() | ||||||
}) | ||||||
|
@@ -363,15 +363,11 @@ describe('useSuspenseQueries 2', () => { | |||||
const [fail, setFail] = React.useState(false) | ||||||
const { data } = useSuspenseQuery({ | ||||||
queryKey: [key, fail], | ||||||
queryFn: async () => { | ||||||
await sleep(10) | ||||||
|
||||||
if (fail) { | ||||||
throw new Error('Suspense Error Bingo') | ||||||
} else { | ||||||
queryFn: () => | ||||||
sleep(10).then(() => { | ||||||
if (fail) throw new Error('Suspense Error Bingo') | ||||||
return 'data' | ||||||
} | ||||||
}, | ||||||
}), | ||||||
retry: 0, | ||||||
}) | ||||||
|
||||||
|
@@ -393,12 +389,12 @@ describe('useSuspenseQueries 2', () => { | |||||
</ErrorBoundary>, | ||||||
) | ||||||
|
||||||
rendered.getByText('Loading...') | ||||||
await act(() => vi.advanceTimersByTimeAsync(11)) | ||||||
rendered.getByText('rendered: data') | ||||||
expect(rendered.getByText('Loading...')).toBeInTheDocument() | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
expect(rendered.getByText('rendered: data')).toBeInTheDocument() | ||||||
fireEvent.click(rendered.getByText('trigger fail')) | ||||||
await act(() => vi.advanceTimersByTimeAsync(11)) | ||||||
rendered.getByText('error boundary') | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
expect(rendered.getByText('error boundary')).toBeInTheDocument() | ||||||
expect(consoleMock.mock.calls[0]?.[1]).toStrictEqual( | ||||||
new Error('Suspense Error Bingo'), | ||||||
) | ||||||
|
@@ -413,10 +409,7 @@ describe('useSuspenseQueries 2', () => { | |||||
const [isPending, startTransition] = React.useTransition() | ||||||
const { data } = useSuspenseQuery({ | ||||||
queryKey: [key, count], | ||||||
queryFn: async () => { | ||||||
await sleep(10) | ||||||
return 'data' + count | ||||||
}, | ||||||
queryFn: async () => sleep(10).then(() => 'data' + count), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I missed this. I gonna fix this next time |
||||||
}) | ||||||
|
||||||
return ( | ||||||
|
@@ -438,11 +431,11 @@ describe('useSuspenseQueries 2', () => { | |||||
) | ||||||
|
||||||
expect(rendered.getByText('Loading...')).toBeInTheDocument() | ||||||
await act(() => vi.advanceTimersByTimeAsync(11)) | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
expect(rendered.getByText('data0')).toBeInTheDocument() | ||||||
fireEvent.click(rendered.getByText('inc')) | ||||||
expect(rendered.getByText('Pending...')).toBeInTheDocument() | ||||||
await act(() => vi.advanceTimersByTimeAsync(11)) | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
expect(rendered.getByText('data1')).toBeInTheDocument() | ||||||
}) | ||||||
|
||||||
|
@@ -490,12 +483,12 @@ describe('useSuspenseQueries 2', () => { | |||||
<App />, | ||||||
) | ||||||
|
||||||
rendered.getByText('Loading...') | ||||||
await act(() => vi.advanceTimersByTimeAsync(11)) | ||||||
rendered.getByText('data0') | ||||||
expect(rendered.getByText('Loading...')).toBeInTheDocument() | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
expect(rendered.getByText('data0')).toBeInTheDocument() | ||||||
fireEvent.click(rendered.getByText('inc')) | ||||||
await act(() => vi.advanceTimersByTimeAsync(11)) | ||||||
rendered.getByText('data1') | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
expect(rendered.getByText('data1')).toBeInTheDocument() | ||||||
expect(queryFnCount).toBe(2) | ||||||
}) | ||||||
|
||||||
|
@@ -514,10 +507,7 @@ describe('useSuspenseQueries 2', () => { | |||||
const [isPending, startTransition] = React.useTransition() | ||||||
const { data } = useSuspenseQuery({ | ||||||
queryKey: [key, count], | ||||||
queryFn: async () => { | ||||||
await sleep(10) | ||||||
return 'data' + count | ||||||
}, | ||||||
queryFn: async () => sleep(10).then(() => 'data' + count), | ||||||
}) | ||||||
|
||||||
return ( | ||||||
|
@@ -539,11 +529,11 @@ describe('useSuspenseQueries 2', () => { | |||||
) | ||||||
|
||||||
expect(rendered.getByText('Loading...')).toBeInTheDocument() | ||||||
await act(() => vi.advanceTimersByTimeAsync(11)) | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
expect(rendered.getByText('data0')).toBeInTheDocument() | ||||||
fireEvent.click(rendered.getByText('inc')) | ||||||
expect(rendered.getByText('Pending...')).toBeInTheDocument() | ||||||
await act(() => vi.advanceTimersByTimeAsync(11)) | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
expect(rendered.getByText('data1')).toBeInTheDocument() | ||||||
}) | ||||||
|
||||||
|
@@ -557,8 +547,9 @@ describe('useSuspenseQueries 2', () => { | |||||
function Page() { | ||||||
useSuspenseQuery({ | ||||||
queryKey: key, | ||||||
queryFn: () => { | ||||||
queryFn: async () => { | ||||||
count++ | ||||||
await sleep(10) | ||||||
return Promise.reject(new Error('Query failed')) | ||||||
}, | ||||||
gcTime: 0, | ||||||
|
@@ -584,8 +575,8 @@ describe('useSuspenseQueries 2', () => { | |||||
|
||||||
const rendered = renderWithClient(queryClient, <App />) | ||||||
|
||||||
await act(() => vi.advanceTimersByTimeAsync(0)) | ||||||
rendered.getByText('There was an error!') | ||||||
await act(() => vi.advanceTimersByTimeAsync(10)) | ||||||
expect(rendered.getByText('There was an error!')).toBeInTheDocument() | ||||||
expect(count).toBe(1) | ||||||
consoleMock.mockRestore() | ||||||
}) | ||||||
|
@@ -613,10 +604,7 @@ describe('useSuspenseQueries 2', () => { | |||||
function Component() { | ||||||
const { data } = useSuspenseQuery({ | ||||||
queryKey: key, | ||||||
queryFn: async () => { | ||||||
await sleep(3000) | ||||||
return 'data' | ||||||
}, | ||||||
queryFn: async () => sleep(3000).then(() => 'data'), | ||||||
gcTime: 1000, | ||||||
}) | ||||||
|
||||||
|
@@ -639,14 +627,11 @@ describe('useSuspenseQueries 2', () => { | |||||
|
||||||
const rendered = renderWithClient(queryClient, <App />) | ||||||
|
||||||
await act(() => vi.advanceTimersByTimeAsync(200)) | ||||||
rendered.getByText('loading') | ||||||
// unmount while still fetching | ||||||
expect(rendered.getByText('loading')).toBeInTheDocument() | ||||||
fireEvent.click(rendered.getByText('hide')) | ||||||
await act(() => vi.advanceTimersByTimeAsync(800)) | ||||||
rendered.getByText('page2') | ||||||
expect(rendered.getByText('page2')).toBeInTheDocument() | ||||||
// wait for query to be resolved | ||||||
await act(() => vi.advanceTimersByTimeAsync(2000)) | ||||||
Comment on lines
-642
to
-649
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍👍👍 |
||||||
await act(() => vi.advanceTimersByTimeAsync(3000)) | ||||||
expect(queryClient.getQueryData(key)).toBe('data') | ||||||
// wait for gc | ||||||
await act(() => vi.advanceTimersByTimeAsync(1000)) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍