@@ -6,6 +6,7 @@ import { useRequest } from '../index';
6
6
import { clearGlobalOptions , setGlobalOptions } from '../core/config' ;
7
7
import { clearCache } from '../core/utils/cache' ;
8
8
import { waitForAll , waitForTime } from './utils' ;
9
+ import { RECONNECT_LISTENER , FOCUS_LISTENER , VISIBLE_LISTENER } from '../core/utils/listener' ;
9
10
declare let jsdom : any ;
10
11
11
12
describe ( 'useRequest' , ( ) => {
@@ -31,6 +32,11 @@ describe('useRequest', () => {
31
32
clearCache ( ) ;
32
33
// clear global options
33
34
clearGlobalOptions ( ) ;
35
+
36
+ // clear listner
37
+ RECONNECT_LISTENER . clear ( ) ;
38
+ FOCUS_LISTENER . clear ( ) ;
39
+ VISIBLE_LISTENER . clear ( ) ;
34
40
} ) ;
35
41
36
42
afterEach ( ( ) => {
@@ -1583,4 +1589,188 @@ describe('useRequest', () => {
1583
1589
// 5 times is the retry count
1584
1590
expectCount ( errorRetryCountRef , 3 + 5 ) ;
1585
1591
} ) ;
1592
+
1593
+ test ( 'pollingWhenOffline should work. case 1' , async ( ) => {
1594
+ let count = 0 ;
1595
+ const wrapper = shallowMount (
1596
+ defineComponent ( {
1597
+ setup ( ) {
1598
+ const { data, loading } = useRequest ( ( ) => request ( ( count += 1 ) ) , {
1599
+ pollingInterval : 500 ,
1600
+ } ) ;
1601
+ return ( ) => < button > { `${ loading . value || data . value } ` } </ button > ;
1602
+ } ,
1603
+ } ) ,
1604
+ ) ;
1605
+
1606
+ for ( let index = 0 ; index < 1000 ; index ++ ) {
1607
+ expect ( wrapper . text ( ) ) . toBe ( 'true' ) ;
1608
+ await waitForTime ( 1000 ) ;
1609
+ expect ( wrapper . text ( ) ) . toBe ( `${ index + 1 } ` ) ;
1610
+ await waitForTime ( 500 ) ;
1611
+ }
1612
+
1613
+ // mock offline
1614
+ Object . defineProperty ( window . navigator , 'onLine' , {
1615
+ value : false ,
1616
+ writable : true ,
1617
+ } ) ;
1618
+
1619
+ // last request
1620
+ expect ( wrapper . text ( ) ) . toBe ( 'true' ) ;
1621
+ await waitForTime ( 1000 ) ;
1622
+ expect ( wrapper . text ( ) ) . toBe ( `1001` ) ;
1623
+ await waitForTime ( 500 ) ;
1624
+ expect ( wrapper . text ( ) ) . toBe ( `1001` ) ;
1625
+
1626
+ // mock online
1627
+ Object . defineProperty ( window . navigator , 'onLine' , {
1628
+ value : true ,
1629
+ writable : true ,
1630
+ } ) ;
1631
+ jsdom . window . dispatchEvent ( new Event ( 'online' ) ) ;
1632
+ await waitForTime ( 1 ) ;
1633
+
1634
+ for ( let index = 0 ; index < 1000 ; index ++ ) {
1635
+ expect ( wrapper . text ( ) ) . toBe ( 'true' ) ;
1636
+ await waitForTime ( 1000 ) ;
1637
+ expect ( wrapper . text ( ) ) . toBe ( `${ 1001 + index + 1 } ` ) ;
1638
+ await waitForTime ( 500 ) ;
1639
+ }
1640
+ } ) ;
1641
+
1642
+ test ( 'pollingWhenOffline should work. case 2' , async ( ) => {
1643
+ let count = 0 ;
1644
+ const wrapper = shallowMount (
1645
+ defineComponent ( {
1646
+ setup ( ) {
1647
+ const { data, loading } = useRequest ( ( ) => request ( ( count += 1 ) ) , {
1648
+ pollingInterval : 500 ,
1649
+ pollingWhenOffline : true ,
1650
+ } ) ;
1651
+ return ( ) => < button > { `${ loading . value || data . value } ` } </ button > ;
1652
+ } ,
1653
+ } ) ,
1654
+ ) ;
1655
+
1656
+ for ( let index = 0 ; index < 1000 ; index ++ ) {
1657
+ expect ( wrapper . text ( ) ) . toBe ( 'true' ) ;
1658
+ await waitForTime ( 1000 ) ;
1659
+ expect ( wrapper . text ( ) ) . toBe ( `${ index + 1 } ` ) ;
1660
+ await waitForTime ( 500 ) ;
1661
+ }
1662
+
1663
+ // mock offline
1664
+ Object . defineProperty ( window . navigator , 'onLine' , {
1665
+ value : false ,
1666
+ writable : true ,
1667
+ } ) ;
1668
+
1669
+ // last request
1670
+ expect ( wrapper . text ( ) ) . toBe ( 'true' ) ;
1671
+ await waitForTime ( 1000 ) ;
1672
+ expect ( wrapper . text ( ) ) . toBe ( `1001` ) ;
1673
+ await waitForTime ( 500 ) ;
1674
+ expect ( wrapper . text ( ) ) . toBe ( `true` ) ;
1675
+
1676
+ // mock online
1677
+ Object . defineProperty ( window . navigator , 'onLine' , {
1678
+ value : true ,
1679
+ writable : true ,
1680
+ } ) ;
1681
+ jsdom . window . dispatchEvent ( new Event ( 'online' ) ) ;
1682
+ await waitForTime ( 1 ) ;
1683
+
1684
+ for ( let index = 0 ; index < 1000 ; index ++ ) {
1685
+ expect ( wrapper . text ( ) ) . toBe ( 'true' ) ;
1686
+ await waitForTime ( 1000 ) ;
1687
+ expect ( wrapper . text ( ) ) . toBe ( `${ 1001 + index + 1 } ` ) ;
1688
+ await waitForTime ( 500 ) ;
1689
+ }
1690
+ } ) ;
1691
+
1692
+ test ( 'pollingWhenOffline should work with pollingWhenHidden' , async ( ) => {
1693
+ let count = 0 ;
1694
+ const wrapper = shallowMount (
1695
+ defineComponent ( {
1696
+ setup ( ) {
1697
+ const { data, loading } = useRequest ( ( ) => request ( ( count += 1 ) ) , {
1698
+ pollingInterval : 500 ,
1699
+ } ) ;
1700
+ return ( ) => < button > { `${ loading . value || data . value } ` } </ button > ;
1701
+ } ,
1702
+ } ) ,
1703
+ ) ;
1704
+
1705
+ for ( let index = 0 ; index < 1000 ; index ++ ) {
1706
+ expect ( wrapper . text ( ) ) . toBe ( 'true' ) ;
1707
+ await waitForTime ( 1000 ) ;
1708
+ expect ( wrapper . text ( ) ) . toBe ( `${ index + 1 } ` ) ;
1709
+ await waitForTime ( 500 ) ;
1710
+ }
1711
+
1712
+ // mock offline
1713
+ Object . defineProperty ( window . navigator , 'onLine' , {
1714
+ value : false ,
1715
+ writable : true ,
1716
+ } ) ;
1717
+
1718
+ // last request
1719
+ expect ( wrapper . text ( ) ) . toBe ( 'true' ) ;
1720
+ await waitForTime ( 1000 ) ;
1721
+ expect ( wrapper . text ( ) ) . toBe ( `1001` ) ;
1722
+ await waitForTime ( 500 ) ;
1723
+ expect ( wrapper . text ( ) ) . toBe ( `1001` ) ;
1724
+
1725
+ // mock tab show
1726
+ Object . defineProperty ( document , 'visibilityState' , {
1727
+ value : 'visible' ,
1728
+ writable : true ,
1729
+ } ) ;
1730
+ jsdom . window . dispatchEvent ( new Event ( 'visibilitychange' ) ) ;
1731
+ // wait 1ms make to sure event has trigger
1732
+ await waitForTime ( 1 ) ;
1733
+ expect ( wrapper . text ( ) ) . toBe ( `1001` ) ;
1734
+
1735
+ // mock online
1736
+ Object . defineProperty ( window . navigator , 'onLine' , {
1737
+ value : true ,
1738
+ writable : true ,
1739
+ } ) ;
1740
+ jsdom . window . dispatchEvent ( new Event ( 'online' ) ) ;
1741
+ // wait 1ms to make sure event has trigger
1742
+ await waitForTime ( 1 ) ;
1743
+
1744
+ for ( let index = 0 ; index < 1000 ; index ++ ) {
1745
+ expect ( wrapper . text ( ) ) . toBe ( 'true' ) ;
1746
+ await waitForTime ( 1000 ) ;
1747
+ expect ( wrapper . text ( ) ) . toBe ( `${ 1001 + index + 1 } ` ) ;
1748
+ await waitForTime ( 500 ) ;
1749
+ }
1750
+ } ) ;
1751
+
1752
+ test ( 'listener should unsubscribe when the component was unmounted' , async ( ) => {
1753
+ let count = 0 ;
1754
+ const wrapper = shallowMount (
1755
+ defineComponent ( {
1756
+ setup ( ) {
1757
+ const { data, loading } = useRequest ( ( ) => request ( ( count += 1 ) ) , {
1758
+ pollingInterval : 500 ,
1759
+ } ) ;
1760
+ return ( ) => < button > { `${ loading . value || data . value } ` } </ button > ;
1761
+ } ,
1762
+ } ) ,
1763
+ ) ;
1764
+
1765
+ for ( let index = 0 ; index < 1000 ; index ++ ) {
1766
+ expect ( wrapper . text ( ) ) . toBe ( 'true' ) ;
1767
+ await waitForTime ( 1000 ) ;
1768
+ expect ( wrapper . text ( ) ) . toBe ( `${ index + 1 } ` ) ;
1769
+ await waitForTime ( 500 ) ;
1770
+ }
1771
+
1772
+ expect ( RECONNECT_LISTENER . size ) . toBe ( 1 ) ;
1773
+ wrapper . unmount ( ) ;
1774
+ expect ( RECONNECT_LISTENER . size ) . toBe ( 0 ) ;
1775
+ } ) ;
1586
1776
} ) ;
0 commit comments