Skip to content

Commit b48f0b5

Browse files
Add more tests for graphql
1 parent 5c4a431 commit b48f0b5

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

__tests__/apm.spec.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import "react-native";
7-
import { NativeModules } from "react-native";
7+
import { NativeModules, Platform } from "react-native";
88
import "../jest/mockAPM";
99
import APM from "../modules/APM";
1010
import sinon from "sinon";
@@ -21,6 +21,8 @@ describe("APM Module", () => {
2121
const endExecutionTrace = sinon.spy(NativeModules.IBGAPM, "endExecutionTrace");
2222
const startUITrace = sinon.spy(NativeModules.IBGAPM, "startUITrace");
2323
const endUITrace = sinon.spy(NativeModules.IBGAPM, "endUITrace");
24+
const setNetworkLoggingEnabled = sinon.spy(NativeModules.Instabug, "setNetworkLoggingEnabled");
25+
const _ibgSleep = sinon.spy(NativeModules.IBGAPM, "ibgSleep");
2426

2527
beforeEach(() => {
2628
IBGEventEmitter.removeAllListeners();
@@ -38,6 +40,13 @@ describe("APM Module", () => {
3840
expect(setAppLaunchEnabled.calledOnceWithExactly(true)).toBe(true);
3941
});
4042

43+
it("should call the native method setNetworkEnabledIOS", () => {
44+
Platform.OS = 'ios';
45+
APM.setNetworkEnabledIOS(true);
46+
47+
expect(setNetworkLoggingEnabled.calledOnceWithExactly(true)).toBe(true);
48+
});
49+
4150
it("should call the native method setAutoUITraceEnabled", () => {
4251
APM.setAutoUITraceEnabled(true);
4352

@@ -49,27 +58,27 @@ describe("APM Module", () => {
4958

5059
expect(setLogLevel.calledOnceWithExactly(APM.logLevel.verbose)).toBe(true);
5160
});
52-
61+
5362
it("should call the native method startExecutionTrace", () => {
5463
APM.startExecutionTrace("trace");
55-
64+
5665
expect(startExecutionTrace.calledOnceWith("trace")).toBe(true);
5766
});
58-
67+
5968
it("should call the native method setExecutionTraceAttribute", () => {
6069
const trace = APM.startExecutionTrace("trace").then(() => {
6170
trace.setAttribute("key", "value");
6271
expect(setExecutionTraceAttribute.calledOnceWithExactly(expect.any(String), "key", "value")).toBe(true);
6372
});
6473
});
65-
74+
6675
it("should call the native method endExecutionTrace", () => {
6776
const trace = APM.startExecutionTrace("trace").then(() => {
6877
trace.end();
6978
expect(endExecutionTrace.calledOnceWithExactly(expect.any(String))).toBe(true);
7079
});
7180
});
72-
81+
7382
it("should call the native method startUITrace", () => {
7483
APM.startUITrace("uiTrace");
7584

@@ -81,4 +90,10 @@ describe("APM Module", () => {
8190

8291
expect(endUITrace.calledOnceWithExactly()).toBe(true);
8392
});
93+
94+
it("should call the native method _ibgSleep", () => {
95+
APM._ibgSleep();
96+
97+
expect(_ibgSleep.calledOnceWithExactly()).toBe(true);
98+
});
8499
});

__tests__/networkLogger.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('NetworkLogger Module', () => {
3737
const disableInterception = sinon.spy(Interceptor, 'disableInterception');
3838
const networkLog = sinon.spy(NativeModules.Instabug, 'networkLog');
3939
const apmNetworkLog = sinon.spy(NativeModules.IBGAPM, 'networkLog');
40+
const apolloLinkRequestHandler = sinon.spy(NetworkLogger, 'apolloLinkRequestHandler')
4041

4142
const network = {
4243
url: 'https://api.instabug.com',
@@ -58,6 +59,7 @@ describe('NetworkLogger Module', () => {
5859
IBGEventEmitter.removeAllListeners();
5960
NetworkLogger.setNetworkDataObfuscationHandler(null);
6061
apmNetworkLog.resetHistory();
62+
apolloLinkRequestHandler.resetHistory();
6163
});
6264

6365
it('should set onProgressCallback with callback', () => {
@@ -157,5 +159,24 @@ describe('NetworkLogger Module', () => {
157159
expect(apmNetworkLog.notCalled).toBe(true);
158160
});
159161

162+
it('should test that operationSetContext at apollo handler called', async () => {
163+
const operation = {
164+
setContext : (callback) => callback({ headers : {} }),
165+
operationName : "operationName"
166+
};
167+
const forward = jest.fn();
168+
const operationSetContextMock = sinon.spy(operation, 'setContext')
169+
170+
NetworkLogger.apolloLinkRequestHandler(operation, forward);
171+
expect(operationSetContextMock.calledOnce).toBe(true);
172+
});
173+
174+
it('should test that apollo handler called with catch error', async () => {
175+
const operation = {};
176+
const forward = jest.fn();
177+
178+
NetworkLogger.apolloLinkRequestHandler(operation, forward);
179+
expect(apolloLinkRequestHandler.calledOnce).toBe(true);
180+
});
160181

161182
});

__tests__/xhrNetworkInterceptor.spec.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('Network Interceptor', () => {
4545

4646
it('should set network object on calling setRequestHeader', (done) => {
4747

48-
let requestHeaders = { 'Content-type': 'application/json', 'token': '9u4hiudhi3bf' };
48+
let requestHeaders = { 'content-type': 'application/json', 'token': '9u4hiudhi3bf' };
4949

5050
Interceptor.enableInterception();
5151
Interceptor.setOnDoneCallback((network) => {
@@ -88,7 +88,7 @@ describe('Network Interceptor', () => {
8888

8989
it('should set responseHeaders in network object on receiving response', (done) => {
9090

91-
const headers = { 'Content-type': 'application/json', 'Accept': 'text/html' }
91+
const headers = { 'Content-type': 'application/json', 'Accept': 'text/html','Content-Length':144 }
9292
Interceptor.enableInterception();
9393
Interceptor.setOnDoneCallback((network) => {
9494
expect(network.responseHeaders['Content-type'].trim()).toEqual(headers['Content-type']);
@@ -173,6 +173,7 @@ describe('Network Interceptor', () => {
173173
const headers = {};
174174
headers[InstabugConstants.GRAPHQL_HEADER] =
175175
InstabugConstants.GRAPHQL_HEADER;
176+
const responseBody = { data: [{ item: 'first' }, { item: 'second' }] };
176177
Interceptor.enableInterception();
177178
Interceptor.setOnDoneCallback((network) => {
178179
expect(network.gqlQueryName).toEqual(
@@ -183,6 +184,20 @@ describe('Network Interceptor', () => {
183184
FakeRequest.open(method, url);
184185
FakeRequest.setRequestHeaders(headers);
185186
FakeRequest.send();
187+
FakeRequest.mockResponse(requests[0], null, JSON.stringify(responseBody));
188+
});
189+
190+
it('should set gqlQueryName in network object on receiving response with empty string', (done) => {
191+
const headers = {};
192+
headers[InstabugConstants.GRAPHQL_HEADER] = 'null';
193+
Interceptor.enableInterception();
194+
Interceptor.setOnDoneCallback((network) => {
195+
expect(network.gqlQueryName).toEqual('');
196+
done();
197+
});
198+
FakeRequest.open(method, url);
199+
FakeRequest.setRequestHeaders(headers);
200+
FakeRequest.send();
186201
FakeRequest.mockResponse(requests[0]);
187202
});
188203

jest/mockAPM.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ jest.mock("NativeModules", () => {
1010
endExecutionTrace: jest.fn(),
1111
startUITrace: jest.fn(),
1212
endUITrace: jest.fn(),
13+
setNetworkEnabledIOS: jest.fn(),
14+
ibgSleep: jest.fn(),
15+
},
16+
Instabug: {
17+
setNetworkLoggingEnabled: jest.fn(),
1318
},
14-
Instabug: {},
1519
};
1620
});

modules/NetworkLogger.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ let { Instabug, IBGAPM } = NativeModules;
66

77
var _networkDataObfuscationHandlerSet = false;
88
var _requestFilterExpression = false;
9-
const appendOperationName = (headers, operationName) => {
10-
try {
11-
let newHeaders = JSON.parse(JSON.stringify(headers));
12-
newHeaders[InstabugConstants.GRAPHQL_HEADER] = operationName;
13-
return newHeaders;
14-
} catch(e) {
15-
console.error(e);
16-
return headers;
17-
}
18-
};
199

2010
/**
2111
* NetworkLogger
@@ -104,10 +94,11 @@ export default {
10494

10595
apolloLinkRequestHandler(operation, forward) {
10696
try {
107-
operation.setContext(({ headers = {} }) => ({
108-
headers: appendOperationName(headers, operation.operationName),
109-
}));
110-
} catch(e) {
97+
operation.setContext(({ headers = {} }) => {
98+
headers[InstabugConstants.GRAPHQL_HEADER] = operation.operationName;
99+
return {headers:headers};
100+
});
101+
} catch (e) {
111102
console.error(e);
112103
}
113104

0 commit comments

Comments
 (0)