Skip to content

Commit

Permalink
example usage of @testing-library/react-render-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Dec 7, 2024
1 parent bb31c47 commit 8a248d2
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 107 deletions.
1 change: 1 addition & 0 deletions packages/react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"devDependencies": {
"@tanstack/query-persist-client-core": "workspace:*",
"@testing-library/react": "^16.0.1",
"@testing-library/react-render-stream": "^2.0.0",
"@types/react": "npm:types-react@rc",
"@types/react-dom": "npm:types-react-dom@rc",
"@vitejs/plugin-react": "^4.3.3",
Expand Down
56 changes: 39 additions & 17 deletions packages/react-query/src/__tests__/useSuspenseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as React from 'react'
import { ErrorBoundary } from 'react-error-boundary'
import {
QueryCache,
QueryClientProvider,
QueryErrorResetBoundary,
skipToken,
useQueryErrorResetBoundary,
Expand All @@ -16,21 +17,22 @@ import type {
UseSuspenseInfiniteQueryResult,
UseSuspenseQueryResult,
} from '..'
import { createRenderStream, disableActEnvironment } from '@testing-library/react-render-stream'

describe('useSuspenseQuery', () => {
const queryCache = new QueryCache()
const queryClient = createQueryClient({ queryCache })

it('should render the correct amount of times in Suspense mode', async () => {
it('XX should render the correct amount of times in Suspense mode', async () => {
using _disabledAct = disableActEnvironment()

const key = queryKey()
const states: Array<UseSuspenseQueryResult<number>> = []

const renderStream = createRenderStream<UseSuspenseQueryResult<number> | undefined>({ snapshotDOM: true })

let count = 0
let renders = 0

function Page() {
renders++

const [stateKey, setStateKey] = React.useState(key)

const state = useSuspenseQuery({
Expand All @@ -42,7 +44,7 @@ describe('useSuspenseQuery', () => {
},
})

states.push(state)
renderStream.replaceSnapshot(state)

return (
<div>
Expand All @@ -52,22 +54,42 @@ describe('useSuspenseQuery', () => {
)
}

const rendered = renderWithClient(
queryClient,
const rendered = await renderStream.render(
<React.Suspense fallback="loading">
<Page />
</React.Suspense>,
{
wrapper: ({ children }) => <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
}
)

await waitFor(() => rendered.getByText('data: 1'))
{
const { snapshot, withinDOM } = await renderStream.takeRender()
// Page didn't render yet, so no call to `replaceSnapshot` yet
expect(snapshot).toBeUndefined() // I'd probably skip this assertion
withinDOM().getByText("loading")
}
{
const { snapshot, withinDOM } = await renderStream.takeRender()
expect(snapshot).toMatchObject({ data: 1, status: 'success' })
withinDOM().getByText("data: 1") // I'd probably skip this assertion
}
fireEvent.click(rendered.getByLabelText('toggle'))

await waitFor(() => rendered.getByText('data: 2'))

expect(renders).toBe(4)
expect(states.length).toBe(2)
expect(states[0]).toMatchObject({ data: 1, status: 'success' })
expect(states[1]).toMatchObject({ data: 2, status: 'success' })
{
const { snapshot, withinDOM } = await renderStream.takeRender()
// Page is suspended so it doesn't replace the snapshot yet
expect(snapshot).toMatchObject({ data: 1, status: 'success' }) // I'd probably skip this assertion
withinDOM().getByText("loading")
}
{
const { snapshot, withinDOM } = await renderStream.takeRender()
expect(snapshot).toMatchObject({ data: 2, status: 'success' })
withinDOM().getByText("data: 2") // I'd probably skip this assertion
}

// this would require setup of this matcher, seems that automatically only works with jest
// expect(renderStream).not.toRerender()
await expect(renderStream.takeRender).rejects.toMatchObject({message: expect.stringMatching(/Exceeded timeout/)})
})

it('should return the correct states for a successful infinite query', async () => {
Expand Down Expand Up @@ -875,7 +897,7 @@ describe('useSuspenseQuery', () => {
it('should log an error when skipToken is passed as queryFn', () => {
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
.mockImplementation(() => { })
const key = queryKey()

function Page() {
Expand Down
Loading

0 comments on commit 8a248d2

Please sign in to comment.