11import React from 'react' ;
22import { renderHook } from '@testing-library/react-hooks' ;
33import { Subject } from 'rxjs' ;
4- import { map , finalize } from 'rxjs/operators' ;
4+ import { finalize , filter } from 'rxjs/operators' ;
55import { QueryClient , QueryClientProvider , QueryCache } from 'react-query' ;
66
77import { useInfiniteSubscription } from '../use-infinite-subscription' ;
@@ -37,7 +37,7 @@ describe('useInfiniteSubscription', () => {
3737 const finalizeFn = jest . fn ( ) ;
3838 const subscriptionFn = jest . fn ( ( { pageParam = 0 } ) =>
3939 testSubject . asObservable ( ) . pipe (
40- map ( ( data ) => pageParam * 10 + data ) ,
40+ filter ( ( data ) => data >= pageParam * 10 && data < ( pageParam + 1 ) * 10 ) ,
4141 finalize ( finalizeFn )
4242 )
4343 ) ;
@@ -90,7 +90,7 @@ describe('useInfiniteSubscription', () => {
9090 expect ( result . current . data ) . toEqual ( mapToPages ( 1 ) ) ;
9191 expect ( result . current . status ) . toBe ( 'success' ) ;
9292
93- next ( 2 ) ;
93+ next ( 12 ) ;
9494 await waitForNextUpdate ( ) ;
9595 expect ( result . current . isFetchingNextPage ) . toBe ( false ) ;
9696 expect ( result . current . status ) . toBe ( 'success' ) ;
@@ -107,7 +107,7 @@ describe('useInfiniteSubscription', () => {
107107 await waitForNextUpdate ( ) ;
108108 expect ( result . current . isFetchingNextPage ) . toBe ( true ) ;
109109
110- next ( 3 ) ;
110+ next ( 23 ) ;
111111 await waitForNextUpdate ( ) ;
112112 expect ( result . current . isFetchingNextPage ) . toBe ( false ) ;
113113 expect ( result . current . status ) . toBe ( 'success' ) ;
@@ -153,6 +153,61 @@ describe('useInfiniteSubscription', () => {
153153 expect ( result . current . status ) . toBe ( 'success' ) ;
154154 expect ( result . current . data ) . toEqual ( mapToPages ( 2 ) ) ;
155155 } ) ;
156+
157+ test ( 'updating previously subscribed pages' , async ( ) => {
158+ const { subscriptionFn, next } = subscriptionFnFactory ( ) ;
159+
160+ const getNextPageParam = jest . fn (
161+ ( _lastPage , allPages ) => allPages . length
162+ ) ;
163+ const { result, waitForNextUpdate } = renderHook (
164+ ( ) =>
165+ useInfiniteSubscription ( testSubscriptionKey , subscriptionFn , {
166+ getNextPageParam,
167+ } ) ,
168+ { wrapper : Wrapper }
169+ ) ;
170+
171+ next ( 1 ) ;
172+ await waitForNextUpdate ( ) ;
173+ expect ( result . current . status ) . toBe ( 'success' ) ;
174+ expect ( result . current . data ) . toEqual ( mapToPages ( 1 ) ) ;
175+
176+ expect ( result . current . hasNextPage ) . toBe ( true ) ;
177+
178+ result . current . fetchNextPage ( ) ;
179+ await waitForNextUpdate ( ) ;
180+ expect ( result . current . isFetchingNextPage ) . toBe ( true ) ;
181+ expect ( result . current . data ) . toEqual ( mapToPages ( 1 ) ) ;
182+ expect ( result . current . status ) . toBe ( 'success' ) ;
183+
184+ next ( 12 ) ;
185+ await waitForNextUpdate ( ) ;
186+ expect ( result . current . isFetchingNextPage ) . toBe ( false ) ;
187+ expect ( result . current . status ) . toBe ( 'success' ) ;
188+ expect ( result . current . data ) . toEqual ( {
189+ pageParams : [ undefined , 1 ] ,
190+ pages : [ 1 , 12 ] ,
191+ } ) ;
192+
193+ next ( 2 ) ;
194+ await waitForNextUpdate ( ) ;
195+ expect ( result . current . isFetchingNextPage ) . toBe ( false ) ;
196+ expect ( result . current . status ) . toBe ( 'success' ) ;
197+ expect ( result . current . data ) . toEqual ( {
198+ pageParams : [ undefined , 1 ] ,
199+ pages : [ 2 , 12 ] ,
200+ } ) ;
201+
202+ next ( 13 ) ;
203+ await waitForNextUpdate ( ) ;
204+ expect ( result . current . isFetchingNextPage ) . toBe ( false ) ;
205+ expect ( result . current . status ) . toBe ( 'success' ) ;
206+ expect ( result . current . data ) . toEqual ( {
207+ pageParams : [ undefined , 1 ] ,
208+ pages : [ 2 , 13 ] ,
209+ } ) ;
210+ } ) ;
156211 } ) ;
157212
158213 describe ( 'getPreviousPageParam' , ( ) => {
@@ -189,7 +244,7 @@ describe('useInfiniteSubscription', () => {
189244 expect ( result . current . data ) . toEqual ( mapToPages ( 1 ) ) ;
190245 expect ( result . current . status ) . toBe ( 'success' ) ;
191246
192- next ( 2 ) ;
247+ next ( 12 ) ;
193248 await waitForNextUpdate ( ) ;
194249 expect ( result . current . isFetchingPreviousPage ) . toBe ( false ) ;
195250 expect ( result . current . status ) . toBe ( 'success' ) ;
@@ -206,7 +261,7 @@ describe('useInfiniteSubscription', () => {
206261 await waitForNextUpdate ( ) ;
207262 expect ( result . current . isFetchingPreviousPage ) . toBe ( true ) ;
208263
209- next ( 3 ) ;
264+ next ( 23 ) ;
210265 await waitForNextUpdate ( ) ;
211266 expect ( result . current . isFetchingPreviousPage ) . toBe ( false ) ;
212267 expect ( result . current . status ) . toBe ( 'success' ) ;
@@ -252,6 +307,61 @@ describe('useInfiniteSubscription', () => {
252307 expect ( result . current . status ) . toBe ( 'success' ) ;
253308 expect ( result . current . data ) . toEqual ( mapToPages ( 2 ) ) ;
254309 } ) ;
310+
311+ test ( 'updating previously subscribed pages' , async ( ) => {
312+ const { subscriptionFn, next } = subscriptionFnFactory ( ) ;
313+
314+ const getPreviousPageParam = jest . fn (
315+ ( _lastPage , allPages ) => allPages . length
316+ ) ;
317+ const { result, waitForNextUpdate } = renderHook (
318+ ( ) =>
319+ useInfiniteSubscription ( testSubscriptionKey , subscriptionFn , {
320+ getPreviousPageParam,
321+ } ) ,
322+ { wrapper : Wrapper }
323+ ) ;
324+
325+ next ( 1 ) ;
326+ await waitForNextUpdate ( ) ;
327+ expect ( result . current . status ) . toBe ( 'success' ) ;
328+ expect ( result . current . data ) . toEqual ( mapToPages ( 1 ) ) ;
329+
330+ expect ( result . current . hasPreviousPage ) . toBe ( true ) ;
331+
332+ result . current . fetchPreviousPage ( ) ;
333+ await waitForNextUpdate ( ) ;
334+ expect ( result . current . isFetchingPreviousPage ) . toBe ( true ) ;
335+ expect ( result . current . data ) . toEqual ( mapToPages ( 1 ) ) ;
336+ expect ( result . current . status ) . toBe ( 'success' ) ;
337+
338+ next ( 12 ) ;
339+ await waitForNextUpdate ( ) ;
340+ expect ( result . current . isFetchingPreviousPage ) . toBe ( false ) ;
341+ expect ( result . current . status ) . toBe ( 'success' ) ;
342+ expect ( result . current . data ) . toEqual ( {
343+ pageParams : [ 1 , undefined ] ,
344+ pages : [ 12 , 1 ] ,
345+ } ) ;
346+
347+ next ( 2 ) ;
348+ await waitForNextUpdate ( ) ;
349+ expect ( result . current . isFetchingPreviousPage ) . toBe ( false ) ;
350+ expect ( result . current . status ) . toBe ( 'success' ) ;
351+ expect ( result . current . data ) . toEqual ( {
352+ pageParams : [ 1 , undefined ] ,
353+ pages : [ 12 , 2 ] ,
354+ } ) ;
355+
356+ next ( 13 ) ;
357+ await waitForNextUpdate ( ) ;
358+ expect ( result . current . isFetchingPreviousPage ) . toBe ( false ) ;
359+ expect ( result . current . status ) . toBe ( 'success' ) ;
360+ expect ( result . current . data ) . toEqual ( {
361+ pageParams : [ 1 , undefined ] ,
362+ pages : [ 13 , 2 ] ,
363+ } ) ;
364+ } ) ;
255365 } ) ;
256366 } ) ;
257367} ) ;
0 commit comments