@@ -526,8 +526,8 @@ describe('react => usePagination', () => {
526
526
let cache = queryCache ( getter ( page , pageSize ) ) ;
527
527
expect ( cache ?. list ) . toStrictEqual ( [ 300 , ...generateContinuousNumbers ( 18 , 10 ) ] ) ;
528
528
529
- // 检查是否重新fetch了后一页的数据
530
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 3 ) ;
529
+ // insert时不会重新fetch后一页的数据
530
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 2 ) ;
531
531
cache = queryCache ( getter ( page + 1 , pageSize ) ) ;
532
532
// insert时会将缓存末尾去掉,因此还是剩下10项
533
533
expect ( cache ?. list ) . toEqual ( generateContinuousNumbers ( 28 , 19 ) ) ;
@@ -544,7 +544,7 @@ describe('react => usePagination', () => {
544
544
const cache = queryCache ( getter ( page , pageSize ) ) ;
545
545
expect ( cache ?. list ) . toStrictEqual ( curData ) ;
546
546
547
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 4 ) ;
547
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 2 ) ; // insert不会触发下一页预加载
548
548
} ) ;
549
549
550
550
// 翻到最后一页后,再插入数据不会再去除一条数据
@@ -647,8 +647,8 @@ describe('react => usePagination', () => {
647
647
await waitFor ( ( ) => {
648
648
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( generateContinuousNumbers ( 9 , 6 ) ) ) ; // 有两项被挤到后面一页了
649
649
expect ( screen . getByRole ( 'total' ) ) . toHaveTextContent ( total . toString ( ) ) ;
650
- // 5次来源分别是:初始化时2次、插入数据时1次(fetch下一页)、翻页时2次
651
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 5 ) ;
650
+ // 5次来源分别是:初始化时2次、翻页时1次(翻页后上一页数据已有不再fetch)
651
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 3 ) ;
652
652
} ) ;
653
653
654
654
// 再次返回前一页,移除的数据不应该存在
@@ -1440,14 +1440,14 @@ describe('react => usePagination', () => {
1440
1440
1441
1441
// fetch响应后
1442
1442
await waitFor ( ( ) => {
1443
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 5 ) ; // 初始化2次 + 删除fetch1次 + 翻页后fetch2次
1443
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 4 ) ; // 初始化2次 + 删除fetch重新fetch1次 + 翻页后fetch下一页1次
1444
1444
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( [ 10 , 11 , 12 , 13 ] ) ) ;
1445
1445
} ) ;
1446
1446
1447
1447
// 再次返回前一页,移除的数据不应该存在
1448
1448
fireEvent . click ( screen . getByRole ( 'subtractPage' ) ) ;
1449
1449
await waitFor ( ( ) => {
1450
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 7 ) ; // 翻页再fetch2次
1450
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 4 ) ; // 前后一页都有缓存,不fetch
1451
1451
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( [ 4 , 7 , 8 , 9 ] ) ) ;
1452
1452
} ) ;
1453
1453
} ) ;
@@ -1574,15 +1574,15 @@ describe('react => usePagination', () => {
1574
1574
fireEvent . click ( screen . getByRole ( 'subtractPage' ) ) ;
1575
1575
// 等待fetch完成后检查total是否正确
1576
1576
await waitFor ( ( ) => {
1577
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 4 ) ; // 初始化2次、删除后1次、翻到第1页1次 (第1页不触发上一页预加载)
1577
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 3 ) ; // 初始化2次、删除后1次、翻到第1页无fetch (第1页不触发上一页预加载,且下一页有缓存 )
1578
1578
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( [ 0 , 1 , 2 , 3 ] ) ) ;
1579
1579
expect ( screen . getByRole ( 'total' ) ) . toHaveTextContent ( total . toString ( ) ) ;
1580
1580
} ) ;
1581
1581
1582
1582
fireEvent . click ( screen . getByRole ( 'addPage2' ) ) ;
1583
1583
// 等待fetch完成后检查total是否正确
1584
1584
await waitFor ( ( ) => {
1585
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 6 ) ; // 再次翻页+2次
1585
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 4 ) ; // 再次翻页+1次下一页fetch
1586
1586
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( [ 10 , 11 , 12 , 13 ] ) ) ;
1587
1587
expect ( screen . getByRole ( 'total' ) ) . toHaveTextContent ( total . toString ( ) ) ;
1588
1588
} ) ;
@@ -1593,7 +1593,7 @@ describe('react => usePagination', () => {
1593
1593
let totalBackup = total ;
1594
1594
total = 200 ;
1595
1595
await waitFor ( ( ) => {
1596
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 8 ) ; // 改变筛选条件(自动重置第initialPage页)+2次
1596
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 6 ) ; // 改变筛选条件(自动重置第initialPage页)+2次
1597
1597
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( [ 104 , 105 , 106 , 107 ] ) ) ; // 重置到第initialPage页
1598
1598
expect ( screen . getByRole ( 'total' ) ) . toHaveTextContent ( total . toString ( ) ) ;
1599
1599
} ) ;
@@ -1607,7 +1607,7 @@ describe('react => usePagination', () => {
1607
1607
return data . filter ( ( i : number ) => ! [ 105 ] . includes ( i ) ) ;
1608
1608
} ) ;
1609
1609
await waitFor ( ( ) => {
1610
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 9 ) ; // 预加载下一页+1
1610
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 7 ) ; // 预加载下一页+1
1611
1611
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( [ 104 , 106 , 107 , 108 ] ) ) ;
1612
1612
// 再次看total是否正确
1613
1613
expect ( screen . getByRole ( 'total' ) ) . toHaveTextContent ( total . toString ( ) ) ;
@@ -1618,7 +1618,7 @@ describe('react => usePagination', () => {
1618
1618
fireEvent . click ( screen . getByRole ( 'resetMin' ) ) ;
1619
1619
total = totalBackup ;
1620
1620
await waitFor ( ( ) => {
1621
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 11 ) ; // 条件改变 (当前第initialPage页)+2
1621
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 8 ) ; // 条件重置了,预加载了前一页数 (当前第initialPage页)+1
1622
1622
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( [ 4 , 7 , 8 , 9 ] ) ) ;
1623
1623
expect ( screen . getByRole ( 'total' ) ) . toHaveTextContent ( total . toString ( ) ) ;
1624
1624
} ) ;
@@ -1877,7 +1877,7 @@ describe('react => usePagination', () => {
1877
1877
expect ( screen . getByRole ( 'total' ) ) . toHaveTextContent ( total . toString ( ) ) ;
1878
1878
} ) ;
1879
1879
// method没有设置缓存时,不会触发数据拉取
1880
- expect ( fetchMockFn ) . not . toBeCalled ( ) ;
1880
+ expect ( fetchMockFn ) . not . toHaveBeenCalled ( ) ;
1881
1881
} ) ;
1882
1882
1883
1883
// // 下拉加载更多相关
@@ -2383,7 +2383,7 @@ describe('react => usePagination', () => {
2383
2383
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( [ 9 , 10 , 11 , 12 ] ) ) ;
2384
2384
expect ( screen . getByRole ( 'total' ) ) . toHaveTextContent ( '' ) ;
2385
2385
expect ( screen . getByRole ( 'pageCount' ) ) . toHaveTextContent ( '' ) ;
2386
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 5 ) ;
2386
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 4 ) ; // 翻页只预加载下一页
2387
2387
} ) ;
2388
2388
} ) ;
2389
2389
@@ -2560,13 +2560,13 @@ describe('react => usePagination', () => {
2560
2560
2561
2561
fireEvent . click ( screen . getByRole ( 'addPage' ) ) ;
2562
2562
await waitFor ( ( ) => {
2563
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 4 ) ;
2563
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 3 ) ; // 上一页已有缓存不会再被预加载
2564
2564
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( [ 100 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ) ) ;
2565
2565
} ) ;
2566
2566
2567
2567
fireEvent . click ( screen . getByRole ( 'reload1' ) ) ;
2568
2568
await waitFor ( ( ) => {
2569
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 5 ) ;
2569
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 4 ) ;
2570
2570
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( [ 100 , 1 , 2 , 3 ] ) ) ;
2571
2571
} ) ;
2572
2572
} ) ;
@@ -2640,8 +2640,8 @@ describe('react => usePagination', () => {
2640
2640
fireEvent . click ( screen . getByRole ( 'addPage' ) ) ;
2641
2641
page ++ ;
2642
2642
await waitFor ( ( ) => {
2643
- // 已经到最后一页了,不需要再预加载下一页数据了
2644
- expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 3 ) ;
2643
+ // 已经到最后一页了,不需要再预加载下一页数据了,同时上一页也有缓存不会触发预加载
2644
+ expect ( fetchMockFn ) . toHaveBeenCalledTimes ( 2 ) ;
2645
2645
expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( generateContinuousNumbers ( 9 , 4 ) ) ) ;
2646
2646
expect ( queryCache ( getter ( page + 1 , pageSize ) ) ) . toBeUndefined ( ) ;
2647
2647
} ) ;
@@ -2805,4 +2805,61 @@ describe('react => usePagination', () => {
2805
2805
expect ( completeFn ) . toHaveBeenCalledTimes ( 3 ) ;
2806
2806
} ) ;
2807
2807
} ) ;
2808
+
2809
+ test ( 'should use the data of last request when set `abortLast` to true' , async ( ) => {
2810
+ const alovaInst = createMockAlova ( ) ;
2811
+ const getter = ( page : number , pageSize : number , keyword : string ) =>
2812
+ alovaInst . Get < SearchListResponse > ( '/list-with-search' , {
2813
+ params : {
2814
+ page,
2815
+ pageSize,
2816
+ keyword
2817
+ }
2818
+ } ) ;
2819
+
2820
+ const successFn = jest . fn ( ) ;
2821
+ function Page ( ) {
2822
+ const [ keyword , setKeyword ] = useState ( '' ) ;
2823
+ const { data, onSuccess, total } = usePagination ( ( p , ps ) => getter ( p , ps , keyword ) , {
2824
+ watchingStates : [ keyword ] ,
2825
+ abortLast : true ,
2826
+ data : res => res . list
2827
+ } ) ;
2828
+ onSuccess ( successFn ) ;
2829
+ return (
2830
+ < div >
2831
+ < span role = "total" > { total } </ span >
2832
+ < span role = "response" > { JSON . stringify ( data ) } </ span >
2833
+ < button
2834
+ role = "setKeyword-bbb"
2835
+ onClick = { ( ) => setKeyword ( 'bbb' ) } > </ button >
2836
+ < button
2837
+ role = "resetKeyword"
2838
+ onClick = { ( ) => setKeyword ( '' ) } > </ button >
2839
+ </ div >
2840
+ ) ;
2841
+ }
2842
+
2843
+ render ( ( < Page /> ) as ReactElement < any , any > ) ;
2844
+ let currentList = generateContinuousNumbers ( 9 , 0 , i => {
2845
+ let n = i % 3 ;
2846
+ return {
2847
+ id : i ,
2848
+ word : [ 'aaa' , 'bbb' , 'ccc' ] [ n ]
2849
+ } ;
2850
+ } ) ;
2851
+ await waitFor ( ( ) => {
2852
+ expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( currentList ) ) ;
2853
+ expect ( screen . getByRole ( 'total' ) ) . toHaveTextContent ( '300' ) ;
2854
+ } ) ;
2855
+
2856
+ fireEvent . click ( screen . getByRole ( 'setKeyword-bbb' ) ) ;
2857
+ await untilCbCalled ( setTimeout , 10 ) ;
2858
+ fireEvent . click ( screen . getByRole ( 'resetKeyword' ) ) ;
2859
+ await waitFor ( ( ) => {
2860
+ expect ( screen . getByRole ( 'response' ) ) . toHaveTextContent ( JSON . stringify ( currentList ) ) ;
2861
+ expect ( screen . getByRole ( 'total' ) ) . toHaveTextContent ( '300' ) ;
2862
+ expect ( successFn ) . toHaveBeenCalledTimes ( 2 ) ;
2863
+ } ) ;
2864
+ } ) ;
2808
2865
} ) ;
0 commit comments