@@ -306,7 +306,7 @@ describe('useSWRInfinite', () => {
306306 expect ( container . textContent ) . toMatchInlineSnapshot ( `"page 0, "` )
307307 } )
308308
309- it ( 'should presist page size when key changes' , async ( ) => {
309+ it ( 'should persist page size when key changes' , async ( ) => {
310310 let toggle
311311
312312 function Page ( ) {
@@ -351,4 +351,55 @@ describe('useSWRInfinite', () => {
351351 await act ( ( ) => new Promise ( res => setTimeout ( res , 250 ) ) )
352352 expect ( container . textContent ) . toMatchInlineSnapshot ( `"page 0, page 1, "` )
353353 } )
354+
355+ it ( 'should persist page size when remount' , async ( ) => {
356+ let toggle
357+
358+ function Comp ( ) {
359+ const { data, size, setSize } = useSWRInfinite < string , string > (
360+ index => [ `pagetest-8` , index ] ,
361+ async ( _ , index ) => {
362+ await new Promise ( res => setTimeout ( res , 100 ) )
363+ return `page ${ index } , `
364+ }
365+ )
366+
367+ return (
368+ < div
369+ onClick = { ( ) => {
370+ // load next page
371+ setSize ( size + 1 )
372+ } }
373+ >
374+ { data }
375+ </ div >
376+ )
377+ }
378+
379+ function Page ( ) {
380+ const [ show , setShow ] = useState ( true )
381+ toggle = setShow
382+ return show ? < Comp /> : null
383+ }
384+
385+ const { container } = render ( < Page /> )
386+ expect ( container . textContent ) . toMatchInlineSnapshot ( `""` )
387+ await waitForDomChange ( { container } ) // mount
388+ expect ( container . textContent ) . toMatchInlineSnapshot ( `"page 0, "` )
389+
390+ // load next page
391+ fireEvent . click ( container . firstElementChild )
392+ await act ( ( ) => new Promise ( res => setTimeout ( res , 150 ) ) )
393+ expect ( container . textContent ) . toMatchInlineSnapshot ( `"page 0, page 1, "` )
394+
395+ // pages should be unmounted now
396+ act ( ( ) => toggle ( v => ! v ) )
397+ await act ( ( ) => new Promise ( res => setTimeout ( res , 50 ) ) )
398+ expect ( container . textContent ) . toMatchInlineSnapshot ( `""` )
399+
400+ // remount, should still have 2 pages
401+ act ( ( ) => toggle ( v => ! v ) )
402+ await act ( ( ) => new Promise ( res => setTimeout ( res , 150 ) ) )
403+ expect ( container . textContent ) . toMatchInlineSnapshot ( `"page 0, page 1, "` )
404+ } )
354405} )
0 commit comments