@@ -179,4 +179,74 @@ describe('useInfinitDataLoader', () => {
179179 { nextPage : 3 , data : 'Page 2 data' } ,
180180 ] )
181181 } )
182+
183+ it ( 'should get the first and loadMore one page on mount while not enabled then enabled then reload' , async ( ) => {
184+ const { setCanResolve, initialProps, resetCounter } =
185+ getPrerequisite ( 'test4' )
186+ const localInitialProps = {
187+ ...initialProps ,
188+ config : {
189+ ...config ,
190+ enabled : false ,
191+ } ,
192+ }
193+ const { result, rerender } = renderHook (
194+ props => useInfiniteDataLoader ( props . key , props . method , props . config ) ,
195+ {
196+ initialProps : localInitialProps ,
197+ wrapper,
198+ } ,
199+ )
200+ expect ( result . current . data ) . toBe ( undefined )
201+ expect ( result . current . isLoading ) . toBe ( false )
202+ expect ( initialProps . method ) . toHaveBeenCalledTimes ( 0 )
203+ rerender ( localInitialProps )
204+ expect ( result . current . data ) . toBe ( undefined )
205+ expect ( result . current . isLoading ) . toBe ( false )
206+ expect ( initialProps . method ) . toHaveBeenCalledTimes ( 0 )
207+ rerender ( { ...localInitialProps , config : { ...config , enabled : true } } )
208+ expect ( result . current . data ) . toBe ( undefined )
209+ await waitFor ( ( ) => expect ( result . current . isLoading ) . toBe ( true ) )
210+ expect ( initialProps . method ) . toHaveBeenCalledTimes ( 1 )
211+ expect ( initialProps . method ) . toHaveBeenCalledWith ( {
212+ page : 1 ,
213+ } )
214+ setCanResolve ( true )
215+ await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) )
216+ setCanResolve ( false )
217+ expect ( initialProps . method ) . toHaveBeenCalledTimes ( 1 )
218+ expect ( result . current . data ) . toStrictEqual ( [
219+ { nextPage : 2 , data : 'Page 1 data' } ,
220+ ] )
221+ expect ( result . current . isLoading ) . toBe ( false )
222+ act ( ( ) => {
223+ result . current . loadMore ( )
224+ } )
225+ await waitFor ( ( ) => expect ( result . current . isLoading ) . toBe ( true ) )
226+ expect ( result . current . data ) . toStrictEqual ( [
227+ { nextPage : 2 , data : 'Page 1 data' } ,
228+ ] )
229+ expect ( initialProps . method ) . toHaveBeenCalledTimes ( 2 )
230+ expect ( initialProps . method ) . toHaveBeenCalledWith ( {
231+ page : 2 ,
232+ } )
233+ setCanResolve ( true )
234+ await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) )
235+ expect ( result . current . data ) . toStrictEqual ( [
236+ { nextPage : 2 , data : 'Page 1 data' } ,
237+ { nextPage : 3 , data : 'Page 2 data' } ,
238+ ] )
239+ setCanResolve ( false )
240+ resetCounter ( )
241+ act ( ( ) => {
242+ result . current . reload ( ) . catch ( ( ) => null )
243+ } )
244+ await waitFor ( ( ) => expect ( result . current . isLoading ) . toBe ( true ) )
245+ setCanResolve ( true )
246+ await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBe ( true ) )
247+ expect ( result . current . data ) . toStrictEqual ( [
248+ { nextPage : 2 , data : 'Page 1 data' } ,
249+ { nextPage : 3 , data : 'Page 2 data' } ,
250+ ] )
251+ } )
182252} )
0 commit comments