@@ -30,6 +30,10 @@ import {
3030 screen ,
3131 waitFor ,
3232} from '@testing-library/react'
33+ import {
34+ createRenderStream ,
35+ SyncScreen ,
36+ } from '@testing-library/react-render-stream/pure'
3337import userEvent from '@testing-library/user-event'
3438import { HttpResponse , http } from 'msw'
3539import { useEffect , useState } from 'react'
@@ -1778,6 +1782,10 @@ describe('hooks tests', () => {
17781782 withoutTestLifecycles : true ,
17791783 } )
17801784
1785+ const { takeRender, render, getCurrentRender } = createRenderStream ( {
1786+ snapshotDOM : true ,
1787+ } )
1788+
17811789 const checkNumQueries = ( count : number ) => {
17821790 const cacheEntries = Object . keys ( storeRef . store . getState ( ) . api . queries )
17831791 const queries = cacheEntries . length
@@ -1786,71 +1794,68 @@ describe('hooks tests', () => {
17861794 expect ( queries ) . toBe ( count )
17871795 }
17881796
1789- const checkPageRows = ( type : string , ids : number [ ] ) => {
1790- expect ( screen . getByText ( `Type: ${ type } ` ) ) . toBeTruthy ( )
1797+ const checkPageRows = (
1798+ withinDOM : ( ) => SyncScreen ,
1799+ type : string ,
1800+ ids : number [ ] ,
1801+ ) => {
1802+ expect ( withinDOM ( ) . getByText ( `Type: ${ type } ` ) ) . toBeTruthy ( )
17911803 for ( const id of ids ) {
1792- expect ( screen . getByText ( `Pokemon ${ id } ` ) ) . toBeTruthy ( )
1804+ expect ( withinDOM ( ) . getByText ( `Pokemon ${ id } ` ) ) . toBeTruthy ( )
17931805 }
17941806 }
17951807
1796- async function waitForFetch ( ) {
1797- await waitFor ( ( ) =>
1798- expect ( screen . getByTestId ( 'isFetching' ) . textContent ) . toBe ( 'true' ) ,
1799- )
1800- await waitFor ( ( ) =>
1801- expect ( screen . getByTestId ( 'isFetching' ) . textContent ) . toBe ( 'false' ) ,
1802- )
1808+ async function waitForFetch ( handleExtraMiddleRender = false ) {
1809+ {
1810+ const { withinDOM } = await takeRender ( )
1811+ expect ( withinDOM ( ) . getByTestId ( 'isFetching' ) . textContent ) . toBe ( 'true' )
1812+ }
1813+
1814+ // We seem to do an extra render when fetching an uninitialized entry
1815+ if ( handleExtraMiddleRender ) {
1816+ {
1817+ const { withinDOM } = await takeRender ( )
1818+ expect ( withinDOM ( ) . getByTestId ( 'isFetching' ) . textContent ) . toBe (
1819+ 'true' ,
1820+ )
1821+ }
1822+ }
1823+
1824+ {
1825+ // Second fetch complete
1826+ const { withinDOM } = await takeRender ( )
1827+ expect ( withinDOM ( ) . getByTestId ( 'isFetching' ) . textContent ) . toBe (
1828+ 'false' ,
1829+ )
1830+ }
18031831 }
18041832
18051833 const utils = render ( < PokemonList /> , { wrapper : storeRef . wrapper } )
1806- expect ( screen . getByTestId ( 'data' ) . textContent ) . toBe ( '' )
18071834 checkNumQueries ( 1 )
1808-
1809- await waitFor ( ( ) =>
1810- expect ( screen . getByTestId ( 'isUninitialized' ) . textContent ) . toBe ( 'false' ) ,
1811- )
1812-
1813- // Initial load
1814- await waitForFetch ( )
1835+ await waitForFetch ( true )
18151836 checkNumQueries ( 1 )
1816- checkPageRows ( 'fire' , [ 0 ] )
1817-
1818- act ( ( ) => {
1819- fireEvent . click ( screen . getByTestId ( 'nextPage' ) )
1820- } )
1837+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'fire' , [ 0 ] )
18211838
1839+ fireEvent . click ( screen . getByTestId ( 'nextPage' ) , { } )
18221840 await waitForFetch ( )
1841+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'fire' , [ 0 , 1 ] )
18231842
1824- // Added one page
1825- checkPageRows ( 'fire' , [ 0 , 1 ] )
1826-
1827- act ( ( ) => {
1828- fireEvent . click ( screen . getByTestId ( 'nextPage' ) )
1829- } )
1843+ fireEvent . click ( screen . getByTestId ( 'nextPage' ) )
18301844 await waitForFetch ( )
1831-
1832- checkPageRows ( 'fire' , [ 0 , 1 , 2 ] )
1845+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'fire' , [ 0 , 1 , 2 ] )
18331846
18341847 utils . rerender ( < PokemonList arg = "water" initialPageParam = { 3 } /> )
1835-
1836- await waitForFetch ( )
1837-
1848+ await waitForFetch ( true )
18381849 checkNumQueries ( 2 )
1839- checkPageRows ( 'water' , [ 3 ] )
1850+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'water' , [ 3 ] )
18401851
1841- act ( ( ) => {
1842- fireEvent . click ( screen . getByTestId ( 'nextPage' ) )
1843- } )
1852+ fireEvent . click ( screen . getByTestId ( 'nextPage' ) )
18441853 await waitForFetch ( )
1854+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'water' , [ 3 , 4 ] )
18451855
1846- checkPageRows ( 'water' , [ 3 , 4 ] )
1847-
1848- act ( ( ) => {
1849- fireEvent . click ( screen . getByTestId ( 'prevPage' ) )
1850- } )
1856+ fireEvent . click ( screen . getByTestId ( 'prevPage' ) )
18511857 await waitForFetch ( )
1852-
1853- checkPageRows ( 'water' , [ 2 , 3 , 4 ] )
1858+ checkPageRows ( getCurrentRender ( ) . withinDOM , 'water' , [ 2 , 3 , 4 ] )
18541859 } )
18551860 } )
18561861
0 commit comments