@@ -14,16 +14,27 @@ jest.mock('@react-native-community/netinfo', () => ({
1414 }
1515} ) , { virtual : true } )
1616
17+ jest . mock ( '../../src/common/js' , ( ) => ( {
18+ successHandle : jest . fn ( ) ,
19+ failHandle : jest . fn ( ) ,
20+ defineUnsupportedProps : jest . fn ( )
21+ } ) )
22+
1723const {
24+ getNetworkType,
1825 offNetworkStatusChange,
1926 onNetworkStatusChange
2027} = require ( '../../src/platform/api/device/network/rnNetwork' )
28+ const NetInfo = require ( '@react-native-community/netinfo' ) . default
29+ const { failHandle } = require ( '../../src/common/js' )
2130
2231describe ( 'RN network events' , ( ) => {
2332 beforeEach ( ( ) => {
2433 offNetworkStatusChange ( )
2534 mockAddEventListener . mockClear ( )
2635 mockUnsubscribe . mockClear ( )
36+ NetInfo . fetch . mockReset ( )
37+ failHandle . mockClear ( )
2738 } )
2839
2940 test ( 'should clear callbacks and native subscription when callback is null' , ( ) => {
@@ -35,6 +46,19 @@ describe('RN network events', () => {
3546 expect ( mockUnsubscribe ) . toHaveBeenCalledTimes ( 1 )
3647 } )
3748
49+ test ( 'should release native subscription after the last callback is removed' , ( ) => {
50+ const callbackA = jest . fn ( )
51+ const callbackB = jest . fn ( )
52+ onNetworkStatusChange ( callbackA )
53+ onNetworkStatusChange ( callbackB )
54+
55+ offNetworkStatusChange ( callbackA )
56+ expect ( mockUnsubscribe ) . not . toHaveBeenCalled ( )
57+
58+ offNetworkStatusChange ( callbackB )
59+ expect ( mockUnsubscribe ) . toHaveBeenCalledTimes ( 1 )
60+ } )
61+
3862 test ( 'should subscribe again after all callbacks are removed' , ( ) => {
3963 onNetworkStatusChange ( jest . fn ( ) )
4064 offNetworkStatusChange ( )
@@ -44,4 +68,18 @@ describe('RN network events', () => {
4468
4569 expect ( mockAddEventListener ) . toHaveBeenCalledTimes ( 2 )
4670 } )
71+
72+ test ( 'getNetworkType should include API name when fetching fails' , async ( ) => {
73+ const fail = jest . fn ( )
74+ const complete = jest . fn ( )
75+ NetInfo . fetch . mockRejectedValue ( new Error ( 'fetch failed' ) )
76+
77+ getNetworkType ( { fail, complete } )
78+ await Promise . resolve ( )
79+ await Promise . resolve ( )
80+
81+ expect ( failHandle ) . toHaveBeenCalledWith ( {
82+ errMsg : 'getNetworkType:fail fetch failed'
83+ } , fail , complete )
84+ } )
4785} )
0 commit comments