11import '../mocks/mockXhrNetworkInterceptor' ;
2-
3- import { Platform } from 'react-native' ;
2+ import '../mocks/mockInstabugUtils' ;
43
54import waitForExpect from 'wait-for-expect' ;
65
76import * as NetworkLogger from '../../src/modules/NetworkLogger' ;
8- import { NativeAPM } from '../../src/native/NativeAPM' ;
9- import { NativeInstabug } from '../../src/native/NativeInstabug' ;
107import Interceptor from '../../src/utils/XhrNetworkInterceptor' ;
8+ import { reportNetworkLog } from '../../src/utils/InstabugUtils' ;
119
12- const clone = ( obj : any ) => {
10+ const clone = < T > ( obj : T ) : T => {
1311 return JSON . parse ( JSON . stringify ( obj ) ) ;
1412} ;
1513
1614describe ( 'NetworkLogger Module' , ( ) => {
17- const network = {
15+ const network : NetworkLogger . NetworkData = {
1816 url : 'https://api.instabug.com' ,
1917 requestBody : '' ,
20- requestHeaders : { 'Content -type' : 'application/json' } ,
18+ requestHeaders : { 'content -type' : 'application/json' } ,
2119 method : 'GET' ,
2220 responseBody : '' ,
2321 responseCode : 200 ,
24- responseHeaders : '' ,
22+ responseHeaders : { 'content-type' : 'application/json' } ,
2523 contentType : 'application/json' ,
2624 duration : 0 ,
25+ requestBodySize : 0 ,
26+ responseBodySize : 0 ,
27+ errorDomain : '' ,
28+ errorCode : 0 ,
29+ startTime : 0 ,
30+ serverErrorMessage : '' ,
31+ requestContentType : 'application/json' ,
2732 } ;
2833
2934 beforeEach ( ( ) => {
30- // @ts -ignore
3135 NetworkLogger . setNetworkDataObfuscationHandler ( null ) ;
3236 } ) ;
3337
@@ -52,52 +56,27 @@ describe('NetworkLogger Module', () => {
5256 expect ( Interceptor . disableInterception ) . toBeCalledTimes ( 1 ) ;
5357 } ) ;
5458
55- it ( 'should send log network when Platform is ios' , ( ) => {
56- Platform . OS = 'ios' ;
59+ it ( 'should report the network log' , ( ) => {
5760 Interceptor . setOnDoneCallback = jest
5861 . fn ( )
5962 . mockImplementation ( ( callback ) => callback ( clone ( network ) ) ) ;
63+
6064 NetworkLogger . setEnabled ( true ) ;
6165
62- expect ( NativeInstabug . networkLog ) . toBeCalledTimes ( 1 ) ;
63- expect ( NativeInstabug . networkLog ) . toBeCalledWith ( network ) ;
66+ expect ( reportNetworkLog ) . toBeCalledTimes ( 1 ) ;
67+ expect ( reportNetworkLog ) . toBeCalledWith ( network ) ;
6468 } ) ;
6569
6670 it ( 'should send log network when Platform is android' , ( ) => {
67- Platform . OS = 'android' ;
6871 Interceptor . setOnDoneCallback = jest
6972 . fn ( )
7073 . mockImplementation ( ( callback ) => callback ( clone ( network ) ) ) ;
7174 NetworkLogger . setEnabled ( true ) ;
7275
73- expect ( NativeInstabug . networkLog ) . toBeCalledWith ( JSON . stringify ( network ) ) ;
74- expect ( NativeAPM . networkLog ) . toBeCalledWith ( JSON . stringify ( network ) ) ;
75- } ) ;
76-
77- it ( 'should not break if it fails to stringify to JSON on network log if platform is android' , ( ) => {
78- Platform . OS = 'android' ;
79-
80- // Avoid the console.error to clutter the test log
81- const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
82-
83- // Make a circular object, this should make JSON.stringify fail
84- const networkResult = clone ( network ) ;
85- networkResult . responseBody = { } ;
86- networkResult . responseBody . result = { body : networkResult . responseBody } ;
87-
88- Interceptor . setOnDoneCallback = jest
89- . fn ( )
90- . mockImplementation ( ( callback ) => callback ( networkResult ) ) ;
91-
92- expect ( ( ) => NetworkLogger . setEnabled ( true ) ) . not . toThrow ( ) ;
93- expect ( NativeInstabug . networkLog ) . not . toBeCalled ( ) ;
94- expect ( NativeAPM . networkLog ) . not . toBeCalled ( ) ;
95-
96- consoleSpy . mockRestore ( ) ;
76+ expect ( reportNetworkLog ) . toBeCalledWith ( network ) ;
9777 } ) ;
9878
9979 it ( 'should send log network when setNetworkDataObfuscationHandler is set and Platform is ios' , async ( ) => {
100- Platform . OS = 'ios' ;
10180 const randomString = '28930q938jqhd' ;
10281 Interceptor . setOnDoneCallback = jest
10382 . fn ( )
@@ -111,12 +90,11 @@ describe('NetworkLogger Module', () => {
11190 await waitForExpect ( ( ) => {
11291 const newData = clone ( network ) ;
11392 newData . requestHeaders . token = randomString ;
114- expect ( NativeInstabug . networkLog ) . toBeCalledWith ( newData ) ;
93+ expect ( reportNetworkLog ) . toBeCalledWith ( newData ) ;
11594 } ) ;
11695 } ) ;
11796
11897 it ( 'should send log network when setNetworkDataObfuscationHandler is set and Platform is android' , async ( ) => {
119- Platform . OS = 'android' ;
12098 const randomString = '28930q938jqhd' ;
12199 Interceptor . setOnDoneCallback = jest
122100 . fn ( )
@@ -130,14 +108,11 @@ describe('NetworkLogger Module', () => {
130108 await waitForExpect ( ( ) => {
131109 const newData = clone ( network ) ;
132110 newData . requestHeaders . token = randomString ;
133- expect ( NativeInstabug . networkLog ) . toBeCalledWith ( JSON . stringify ( newData ) ) ;
134- expect ( NativeAPM . networkLog ) . toBeCalledWith ( JSON . stringify ( newData ) ) ;
111+ expect ( reportNetworkLog ) . toBeCalledWith ( newData ) ;
135112 } ) ;
136113 } ) ;
137114
138115 it ( 'should not break if network data obfuscation fails when platform is android' , async ( ) => {
139- Platform . OS = 'android' ;
140-
141116 // Avoid the console.error to clutter the test log
142117 const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
143118
@@ -152,8 +127,7 @@ describe('NetworkLogger Module', () => {
152127 NetworkLogger . setNetworkDataObfuscationHandler ( handler ) ;
153128
154129 expect ( ( ) => NetworkLogger . setEnabled ( true ) ) . not . toThrow ( ) ;
155- expect ( NativeInstabug . networkLog ) . not . toBeCalled ( ) ;
156- expect ( NativeAPM . networkLog ) . not . toBeCalled ( ) ;
130+ expect ( reportNetworkLog ) . not . toBeCalled ( ) ;
157131
158132 consoleSpy . mockRestore ( ) ;
159133 } ) ;
@@ -164,12 +138,11 @@ describe('NetworkLogger Module', () => {
164138 . mockImplementation ( ( callback ) => callback ( clone ( network ) ) ) ;
165139
166140 NetworkLogger . setRequestFilterExpression (
167- "network.requestHeaders['Content -type'] === 'application/json'" ,
141+ "network.requestHeaders['content -type'] === 'application/json'" ,
168142 ) ;
169143 NetworkLogger . setEnabled ( true ) ;
170144
171- expect ( NativeInstabug . networkLog ) . not . toBeCalled ( ) ;
172- expect ( NativeAPM . networkLog ) . not . toBeCalled ( ) ;
145+ expect ( reportNetworkLog ) . not . toBeCalled ( ) ;
173146 } ) ;
174147
175148 it ( 'should test that operationSetContext at apollo handler called' , async ( ) => {
0 commit comments