Skip to content

Commit 942d546

Browse files
authored
Migrate Sinon's fake XHR to Nock and XHR polyfill (#795)
1 parent 082f018 commit 942d546

File tree

5 files changed

+48
-150
lines changed

5 files changed

+48
-150
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@
3232
"esprima": "^4.0.1",
3333
"jest": "^24.8.0",
3434
"metro-react-native-babel-preset": "0.72.2",
35+
"nock": "^13.2.9",
3536
"react": "^16.8.6",
3637
"react-native": "^0.60.0",
37-
"sinon": "^7.3.2",
3838
"typescript": "4.0.3",
39-
"wait-for-expect": "^1.2.0"
39+
"wait-for-expect": "^1.2.0",
40+
"xhr2": "^0.2.1"
4041
},
4142
"jest": {
4243
"preset": "react-native",

tests/mocks/fakeNetworkRequest.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ export default {
1919
mockStatus(request, status) {
2020
request.setStatus(status);
2121
},
22-
mockResponse(request, headers, body) {
23-
request.setResponseHeaders(headers ? headers : '');
24-
request.setResponseBody(body ? body : '');
22+
mockResponse(request, status = 200, body = 'ok', headers) {
23+
request.once().reply(status, body, headers);
2524
}
2625
}

tests/setup.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import './mocks/mockNativeModules';
22
import { Platform } from 'react-native';
33
import IBGEventEmitter from '../src/utils/IBGEventEmitter';
4-
import sinon from 'sinon';
4+
import XHR from 'xhr2';
5+
6+
global.XMLHttpRequest = XHR;
57

68
// For some reason `Platform.constants` might be undefined,
79
// if so, we initialize it to an empty object.
810
if (!Platform.constants) {
911
Platform.constants = {};
1012
}
1113

12-
global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
13-
1414
beforeEach(() => {
1515
IBGEventEmitter.removeAllListeners();
1616
});
Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
1-
import sinon from 'sinon';
1+
import nock from 'nock';
22
import FakeRequest from '../mocks/fakeNetworkRequest';
33
import InstabugConstants from '../../src/utils/InstabugConstants';
44
import Interceptor from '../../src/utils/XhrNetworkInterceptor';
55

66
const url = 'http://api.instabug.com';
77
const method = 'GET';
88

9-
describe('Network Interceptor', () => {
10-
let server;
11-
let requests;
12-
13-
beforeEach(function () {
14-
server = sinon.useFakeXMLHttpRequest();
15-
requests = [];
16-
17-
server.onCreate = function (xhr) {
18-
requests.push(xhr);
19-
};
20-
});
9+
const request = nock(url).get('/');
2110

22-
afterEach(function () {
23-
server.restore();
11+
describe('Network Interceptor', () => {
12+
beforeEach(() => {
13+
nock.cleanAll();
2414
});
2515

2616
it('should set network object on entering XMLHttpRequest.prototype.open', done => {
@@ -30,9 +20,9 @@ describe('Network Interceptor', () => {
3020
expect(network.method).toEqual(method);
3121
done();
3222
});
23+
FakeRequest.mockResponse(request);
3324
FakeRequest.open(method, url);
3425
FakeRequest.send();
35-
FakeRequest.mockResponse(requests[0]);
3626
});
3727

3828
it('should set network object on calling setRequestHeader', done => {
@@ -44,9 +34,9 @@ describe('Network Interceptor', () => {
4434
done();
4535
});
4636
FakeRequest.open(method, url);
37+
FakeRequest.mockResponse(request);
4738
FakeRequest.setRequestHeaders(requestHeaders);
4839
FakeRequest.send();
49-
FakeRequest.mockResponse(requests[0]);
5040
});
5141

5242
it('should set requestBody in network object', done => {
@@ -56,9 +46,9 @@ describe('Network Interceptor', () => {
5646
expect(network.requestBody).toEqual(JSON.stringify(requestBody));
5747
done();
5848
});
49+
FakeRequest.mockResponse(request);
5950
FakeRequest.open(method, url);
6051
FakeRequest.send(requestBody);
61-
FakeRequest.mockResponse(requests[0]);
6252
});
6353

6454
it('should set contentType in network object on receiving response', done => {
@@ -68,9 +58,9 @@ describe('Network Interceptor', () => {
6858
expect(network.contentType).toEqual(headers['Content-type']);
6959
done();
7060
});
61+
FakeRequest.mockResponse(request, 200, 'ok', headers);
7162
FakeRequest.open(method, url);
7263
FakeRequest.send();
73-
FakeRequest.mockResponse(requests[0], headers);
7464
});
7565

7666
it('should set responseHeaders in network object on receiving response', done => {
@@ -81,26 +71,13 @@ describe('Network Interceptor', () => {
8171
};
8272
Interceptor.enableInterception();
8373
Interceptor.setOnDoneCallback(network => {
84-
expect(network.responseHeaders['Content-type'].trim()).toEqual(headers['Content-type']);
85-
expect(network.responseHeaders['Accept'].trim()).toEqual(headers['Accept']);
86-
done();
87-
});
88-
FakeRequest.open(method, url);
89-
FakeRequest.send();
90-
FakeRequest.mockResponse(requests[0], headers);
91-
});
92-
93-
it('should set responseHeaders in network object on receiving response', done => {
94-
const headers = { 'Content-type': 'application/json', Accept: 'text/html' };
95-
Interceptor.enableInterception();
96-
Interceptor.setOnDoneCallback(network => {
97-
expect(network.responseHeaders['Content-type'].trim()).toEqual(headers['Content-type']);
98-
expect(network.responseHeaders['Accept'].trim()).toEqual(headers['Accept']);
74+
expect(network.responseHeaders['content-type'].trim()).toEqual(headers['Content-type']);
75+
expect(network.responseHeaders.accept.trim()).toEqual(headers['Accept']);
9976
done();
10077
});
78+
FakeRequest.mockResponse(request, 200, 'ok', headers);
10179
FakeRequest.open(method, url);
10280
FakeRequest.send();
103-
FakeRequest.mockResponse(requests[0], headers);
10481
});
10582

10683
it('should set responseCode in network object on receiving response', done => {
@@ -110,10 +87,9 @@ describe('Network Interceptor', () => {
11087
expect(network.responseCode).toEqual(status);
11188
done();
11289
});
90+
FakeRequest.mockResponse(request, status);
11391
FakeRequest.open(method, url);
11492
FakeRequest.send();
115-
FakeRequest.mockStatus(requests[0], status);
116-
FakeRequest.mockResponse(requests[0]);
11793
});
11894

11995
it('should set responseBody in network object on receiving response', done => {
@@ -124,9 +100,9 @@ describe('Network Interceptor', () => {
124100
done();
125101
});
126102
FakeRequest.open(method, url);
103+
FakeRequest.mockResponse(request, 200, JSON.stringify(responseBody));
127104
FakeRequest.setResponseType('json');
128105
FakeRequest.send();
129-
FakeRequest.mockResponse(requests[0], null, JSON.stringify(responseBody));
130106
});
131107

132108
it('should call onProgressCallback in network object on receiving response', done => {
@@ -137,19 +113,18 @@ describe('Network Interceptor', () => {
137113
done();
138114
});
139115

116+
FakeRequest.mockResponse(request, 200, 'ok', { 'Content-Length': 100 });
140117
FakeRequest.open(method, url);
141118
FakeRequest.send();
142-
FakeRequest.mockStatus(requests[0], 200);
143-
FakeRequest.mockResponse(requests[0]);
144119
});
145120

146121
it('should set responseBody in network object on receiving response', () => {
147122
Interceptor.disableInterception();
148123
const callback = jest.fn();
149124
Interceptor.setOnDoneCallback(callback);
125+
FakeRequest.mockResponse(request);
150126
FakeRequest.open(method, url);
151127
FakeRequest.send();
152-
FakeRequest.mockResponse(requests[0]);
153128
expect(callback).not.toHaveBeenCalled();
154129
});
155130
it('should set gqlQueryName in network object on receiving response', done => {
@@ -162,9 +137,9 @@ describe('Network Interceptor', () => {
162137
done();
163138
});
164139
FakeRequest.open(method, url);
140+
FakeRequest.mockResponse(request, 200, JSON.stringify(responseBody));
165141
FakeRequest.setRequestHeaders(headers);
166142
FakeRequest.send();
167-
FakeRequest.mockResponse(requests[0], null, JSON.stringify(responseBody));
168143
});
169144

170145
it('should set gqlQueryName in network object on receiving response with empty string', done => {
@@ -176,9 +151,9 @@ describe('Network Interceptor', () => {
176151
done();
177152
});
178153
FakeRequest.open(method, url);
154+
FakeRequest.mockResponse(request);
179155
FakeRequest.setRequestHeaders(headers);
180156
FakeRequest.send();
181-
FakeRequest.mockResponse(requests[0]);
182157
});
183158

184159
it('should set serverErrorMessage in network object on receiving response', done => {
@@ -192,8 +167,8 @@ describe('Network Interceptor', () => {
192167
});
193168
FakeRequest.open(method, url);
194169
FakeRequest.setRequestHeaders(headers);
170+
FakeRequest.mockResponse(request, 200, JSON.stringify(responseBody));
195171
FakeRequest.setResponseType('json');
196172
FakeRequest.send();
197-
FakeRequest.mockResponse(requests[0], null, JSON.stringify(responseBody));
198173
});
199174
});

0 commit comments

Comments
 (0)