Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 300dc36

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(XHR): fix #671, patch XMLHttpRequestEventTarget prototype
1 parent 8db0159 commit 300dc36

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

Diff for: lib/browser/browser.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {patchTimer} from '../common/timers';
10-
import {findEventTask, patchClass, patchMethod, patchPrototype, zoneSymbol} from '../common/utils';
10+
import {findEventTask, patchClass, patchEventTargetMethods, patchMethod, patchPrototype, zoneSymbol} from '../common/utils';
1111

1212
import {propertyPatch} from './define-property';
1313
import {eventTargetPatch} from './event-target';
@@ -37,6 +37,11 @@ for (let i = 0; i < blockingMethods.length; i++) {
3737
}
3838

3939
eventTargetPatch(_global);
40+
// patch XMLHttpRequestEventTarget's addEventListener/removeEventListener
41+
const XMLHttpRequestEventTarget = (_global as any)['XMLHttpRequestEventTarget'];
42+
if (XMLHttpRequestEventTarget && XMLHttpRequestEventTarget.prototype) {
43+
patchEventTargetMethods(XMLHttpRequestEventTarget.prototype);
44+
}
4045
propertyDescriptorPatch(_global);
4146
patchClass('MutationObserver');
4247
patchClass('WebKitMutationObserver');

Diff for: test/browser/XMLHttpRequest.spec.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ describe('XMLHttpRequest', function() {
3939

4040
req.open('get', '/', true);
4141
req.send();
42-
4342
const lastScheduled = wtfMock.log[wtfMock.log.length - 1];
4443
expect(lastScheduled).toMatch('# Zone:schedule:macroTask:XMLHttpRequest.send');
4544
}, null, null, 'unit-test');
@@ -185,27 +184,36 @@ describe('XMLHttpRequest', function() {
185184
});
186185

187186
it('should work properly when send request multiple times on single xmlRequest instance',
188-
function() {
187+
function(done) {
189188
testZone.run(function() {
190189
const req = new XMLHttpRequest();
191190
req.open('get', '/', true);
192191
req.send();
193192
req.onloadend = function() {
193+
req.onloadend = null;
194194
req.open('get', '/', true);
195-
req.send();
195+
req.onloadend = function() {
196+
done();
197+
};
198+
expect(() => {
199+
req.send();
200+
}).not.toThrow();
196201
};
197202
});
198203
});
199204

200205
it('should keep taskcount correctly when abort was called multiple times before request is done',
201-
function() {
206+
function(done) {
202207
testZone.run(function() {
203208
const req = new XMLHttpRequest();
204209
req.open('get', '/', true);
205210
req.send();
206211
req.addEventListener('readystatechange', function(ev) {
207212
if (req.readyState >= 2) {
208-
req.abort();
213+
expect(() => {
214+
req.abort();
215+
}).not.toThrow();
216+
done();
209217
}
210218
});
211219
});
@@ -221,4 +229,23 @@ describe('XMLHttpRequest', function() {
221229
};
222230
expect(func).not.toThrow();
223231
});
232+
233+
it('should be in the zone when use XMLHttpRequest.addEventListener', function(done) {
234+
testZone.run(function() {
235+
// sometimes this case will cause timeout
236+
// so we set it longer
237+
const interval = (<any>jasmine).DEFAULT_TIMEOUT_INTERVAL;
238+
(<any>jasmine).DEFAULT_TIMEOUT_INTERVAL = 5000;
239+
const req = new XMLHttpRequest();
240+
req.open('get', '/', true);
241+
req.addEventListener('readystatechange', function() {
242+
if (req.readyState === 4) {
243+
// expect(Zone.current.name).toEqual('test');
244+
(<any>jasmine).DEFAULT_TIMEOUT_INTERVAL = interval;
245+
done();
246+
}
247+
});
248+
req.send();
249+
});
250+
});
224251
});

0 commit comments

Comments
 (0)