@@ -32,6 +32,8 @@ import {
3232 waitFor ,
3333} from '@testing-library/react'
3434import { userEvent } from '@testing-library/user-event'
35+ import type { SyncScreen } from '@testing-library/react-render-stream/pure'
36+ import { createRenderStream } from '@testing-library/react-render-stream/pure'
3537import { HttpResponse , http } from 'msw'
3638import { useEffect , useState } from 'react'
3739
@@ -1784,6 +1786,10 @@ describe('hooks tests', () => {
17841786 withoutTestLifecycles : true ,
17851787 } )
17861788
1789+ const { takeRender, render, getCurrentRender } = createRenderStream ( {
1790+ snapshotDOM : true ,
1791+ } )
1792+
17871793 const checkNumQueries = ( count : number ) => {
17881794 const cacheEntries = Object . keys ( storeRef . store . getState ( ) . api . queries )
17891795 const queries = cacheEntries . length
@@ -1792,71 +1798,68 @@ describe('hooks tests', () => {
17921798 expect ( queries ) . toBe ( count )
17931799 }
17941800
1795- const checkPageRows = ( type : string , ids : number [ ] ) => {
1796- expect ( screen . getByText ( `Type: ${ type } ` ) ) . toBeTruthy ( )
1801+ const checkPageRows = (
1802+ withinDOM : ( ) => SyncScreen ,
1803+ type : string ,
1804+ ids : number [ ] ,
1805+ ) => {
1806+ expect ( withinDOM ( ) . getByText ( `Type: ${ type } ` ) ) . toBeTruthy ( )
17971807 for ( const id of ids ) {
1798- expect ( screen . getByText ( `Pokemon ${ id } ` ) ) . toBeTruthy ( )
1808+ expect ( withinDOM ( ) . getByText ( `Pokemon ${ id } ` ) ) . toBeTruthy ( )
17991809 }
18001810 }
18011811
1802- async function waitForFetch ( ) {
1803- await waitFor ( ( ) =>
1804- expect ( screen . getByTestId ( 'isFetching' ) . textContent ) . toBe ( 'true' ) ,
1805- )
1806- await waitFor ( ( ) =>
1807- expect ( screen . getByTestId ( 'isFetching' ) . textContent ) . toBe ( 'false' ) ,
1808- )
1812+ async function waitForFetch ( handleExtraMiddleRender = false ) {
1813+ {
1814+ const { withinDOM } = await takeRender ( )
1815+ expect ( withinDOM ( ) . getByTestId ( 'isFetching' ) . textContent ) . toBe ( 'true' )
1816+ }
1817+
1818+ // We seem to do an extra render when fetching an uninitialized entry
1819+ if ( handleExtraMiddleRender ) {
1820+ {
1821+ const { withinDOM } = await takeRender ( )
1822+ expect ( withinDOM ( ) . getByTestId ( 'isFetching' ) . textContent ) . toBe (
1823+ 'true' ,
1824+ )
1825+ }
1826+ }
1827+
1828+ {
1829+ // Second fetch complete
1830+ const { withinDOM } = await takeRender ( )
1831+ expect ( withinDOM ( ) . getByTestId ( 'isFetching' ) . textContent ) . toBe (
1832+ 'false' ,
1833+ )
1834+ }
18091835 }
18101836
18111837 const utils = render ( < PokemonList /> , { wrapper : storeRef . wrapper } )
1812- expect ( screen . getByTestId ( 'data' ) . textContent ) . toBe ( '' )
18131838 checkNumQueries ( 1 )
1814-
1815- await waitFor ( ( ) =>
1816- expect ( screen . getByTestId ( 'isUninitialized' ) . textContent ) . toBe ( 'false' ) ,
1817- )
1818-
1819- // Initial load
1820- await waitForFetch ( )
1839+ await waitForFetch ( true )
18211840 checkNumQueries ( 1 )
1822- checkPageRows ( 'fire' , [ 0 ] )
1823-
1824- act ( ( ) => {
1825- fireEvent . click ( screen . getByTestId ( 'nextPage' ) )
1826- } )
1841+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'fire' , [ 0 ] )
18271842
1843+ fireEvent . click ( screen . getByTestId ( 'nextPage' ) , { } )
18281844 await waitForFetch ( )
1845+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'fire' , [ 0 , 1 ] )
18291846
1830- // Added one page
1831- checkPageRows ( 'fire' , [ 0 , 1 ] )
1832-
1833- act ( ( ) => {
1834- fireEvent . click ( screen . getByTestId ( 'nextPage' ) )
1835- } )
1847+ fireEvent . click ( screen . getByTestId ( 'nextPage' ) )
18361848 await waitForFetch ( )
1837-
1838- checkPageRows ( 'fire' , [ 0 , 1 , 2 ] )
1849+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'fire' , [ 0 , 1 , 2 ] )
18391850
18401851 utils . rerender ( < PokemonList arg = "water" initialPageParam = { 3 } /> )
1841-
1842- await waitForFetch ( )
1843-
1852+ await waitForFetch ( true )
18441853 checkNumQueries ( 2 )
1845- checkPageRows ( 'water' , [ 3 ] )
1854+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'water' , [ 3 ] )
18461855
1847- act ( ( ) => {
1848- fireEvent . click ( screen . getByTestId ( 'nextPage' ) )
1849- } )
1856+ fireEvent . click ( screen . getByTestId ( 'nextPage' ) )
18501857 await waitForFetch ( )
1858+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'water' , [ 3 , 4 ] )
18511859
1852- checkPageRows ( 'water' , [ 3 , 4 ] )
1853-
1854- act ( ( ) => {
1855- fireEvent . click ( screen . getByTestId ( 'prevPage' ) )
1856- } )
1860+ fireEvent . click ( screen . getByTestId ( 'prevPage' ) )
18571861 await waitForFetch ( )
1858-
1859- checkPageRows ( 'water' , [ 2 , 3 , 4 ] )
1862+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'water' , [ 2 , 3 , 4 ] )
18601863 } )
18611864 } )
18621865
0 commit comments