Skip to content

Add test for usePaginatedQuery state #174

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 1 commit into from
Feb 29, 2020
Merged
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
39 changes: 38 additions & 1 deletion src/tests/usePaginatedQuery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ describe('usePaginatedQuery', () => {
rendered.getByText('Data 2')
await waitForElement(() => rendered.getByText('Data 3'))
})

it('should use initialData only on the first page, then use previous page data while fetching the next page', async () => {
function Page() {
const [page, setPage] = React.useState(1)

const { resolvedData } = usePaginatedQuery(
['data', { page }],
async (queryName, { page }) => {
sleep(1000)
await sleep(1000)
return page
},
{ initialData: 0 }
Expand Down Expand Up @@ -84,4 +85,40 @@ describe('usePaginatedQuery', () => {
rendered.getByText('Data 3')
await waitForElement(() => rendered.getByText('Data 4'))
})

// See https://github.com/tannerlinsley/react-query/issues/169
it('should not trigger unnecessary loading state', async () => {
function Page() {
const [page, setPage] = React.useState(1)

const { resolvedData, status } = usePaginatedQuery(
['data', { page }],
async (queryName, { page }) => {
await sleep(1000)
return page
},
{ initialData: 0 }
)

return (
<div>
<h1 data-testid="title">Data {resolvedData}</h1>
<h1 data-testid="status">{status}</h1>
<button onClick={() => setPage(page + 1)}>next</button>
</div>
)
}

const rendered = render(<Page />)

rendered.getByText('Data 0')

fireEvent.click(rendered.getByText('next'))
fireEvent.click(rendered.getByText('next'))
fireEvent.click(rendered.getByText('next'))

await waitForElement(() => rendered.getByTestId('status'))
expect(rendered.getByTestId('status').textContent).toBe('success')
expect(rendered.getByTestId('status').textContent).not.toBe('loading')
})
})