Skip to content

Commit

Permalink
Rewrite infinite query test to use render-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Nov 29, 2024
1 parent aec2719 commit 4ee0f7f
Showing 1 changed file with 50 additions and 45 deletions.
95 changes: 50 additions & 45 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import {
screen,
waitFor,
} from '@testing-library/react'
import {
createRenderStream,
SyncScreen,
} from '@testing-library/react-render-stream/pure'
import userEvent from '@testing-library/user-event'
import { HttpResponse, http } from 'msw'
import { useEffect, useState } from 'react'
Expand Down Expand Up @@ -1778,6 +1782,10 @@ describe('hooks tests', () => {
withoutTestLifecycles: true,
})

const { takeRender, render, getCurrentRender } = createRenderStream({
snapshotDOM: true,
})

const checkNumQueries = (count: number) => {
const cacheEntries = Object.keys(storeRef.store.getState().api.queries)
const queries = cacheEntries.length
Expand All @@ -1786,71 +1794,68 @@ describe('hooks tests', () => {
expect(queries).toBe(count)
}

const checkPageRows = (type: string, ids: number[]) => {
expect(screen.getByText(`Type: ${type}`)).toBeTruthy()
const checkPageRows = (
withinDOM: () => SyncScreen,
type: string,
ids: number[],
) => {
expect(withinDOM().getByText(`Type: ${type}`)).toBeTruthy()
for (const id of ids) {
expect(screen.getByText(`Pokemon ${id}`)).toBeTruthy()
expect(withinDOM().getByText(`Pokemon ${id}`)).toBeTruthy()
}
}

async function waitForFetch() {
await waitFor(() =>
expect(screen.getByTestId('isFetching').textContent).toBe('true'),
)
await waitFor(() =>
expect(screen.getByTestId('isFetching').textContent).toBe('false'),
)
async function waitForFetch(handleExtraMiddleRender = false) {
{
const { withinDOM } = await takeRender()
expect(withinDOM().getByTestId('isFetching').textContent).toBe('true')
}

// We seem to do an extra render when fetching an uninitialized entry
if (handleExtraMiddleRender) {
{
const { withinDOM } = await takeRender()
expect(withinDOM().getByTestId('isFetching').textContent).toBe(
'true',
)
}
}

{
// Second fetch complete
const { withinDOM } = await takeRender()
expect(withinDOM().getByTestId('isFetching').textContent).toBe(
'false',
)
}
}

const utils = render(<PokemonList />, { wrapper: storeRef.wrapper })
expect(screen.getByTestId('data').textContent).toBe('')
checkNumQueries(1)

await waitFor(() =>
expect(screen.getByTestId('isUninitialized').textContent).toBe('false'),
)

// Initial load
await waitForFetch()
await waitForFetch(true)
checkNumQueries(1)
checkPageRows('fire', [0])

act(() => {
fireEvent.click(screen.getByTestId('nextPage'))
})
checkPageRows(getCurrentRender().withinDOM, 'fire', [0])

fireEvent.click(screen.getByTestId('nextPage'), {})
await waitForFetch()
checkPageRows(getCurrentRender().withinDOM, 'fire', [0, 1])

// Added one page
checkPageRows('fire', [0, 1])

act(() => {
fireEvent.click(screen.getByTestId('nextPage'))
})
fireEvent.click(screen.getByTestId('nextPage'))
await waitForFetch()

checkPageRows('fire', [0, 1, 2])
checkPageRows(getCurrentRender().withinDOM, 'fire', [0, 1, 2])

utils.rerender(<PokemonList arg="water" initialPageParam={3} />)

await waitForFetch()

await waitForFetch(true)
checkNumQueries(2)
checkPageRows('water', [3])
checkPageRows(getCurrentRender().withinDOM, 'water', [3])

act(() => {
fireEvent.click(screen.getByTestId('nextPage'))
})
fireEvent.click(screen.getByTestId('nextPage'))
await waitForFetch()
checkPageRows(getCurrentRender().withinDOM, 'water', [3, 4])

checkPageRows('water', [3, 4])

act(() => {
fireEvent.click(screen.getByTestId('prevPage'))
})
fireEvent.click(screen.getByTestId('prevPage'))
await waitForFetch()

checkPageRows('water', [2, 3, 4])
checkPageRows(getCurrentRender().withinDOM, 'water', [2, 3, 4])
})
})

Expand Down

0 comments on commit 4ee0f7f

Please sign in to comment.