mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-06 22:38:37 +08:00
XMLHttpRequest should dispatch loadend events
Summary: The code was never dispatching the loadend event. The event should be dispatched after the load, error, timeout and abort events (abort events are not yet supported). https://xhr.spec.whatwg.org/#event-xhr-loadend Closes https://github.com/facebook/react-native/pull/10047 Differential Revision: D3911080 fbshipit-source-id: 450f50a6f2a5c6845889bce624c64a1ca47ec06b
This commit is contained in:
committed by
Facebook Github Bot 9
parent
2da08884b2
commit
04d870b10b
@@ -468,8 +468,10 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
|
|||||||
setReadyState(newState: number): void {
|
setReadyState(newState: number): void {
|
||||||
this.readyState = newState;
|
this.readyState = newState;
|
||||||
this.dispatchEvent({type: 'readystatechange'});
|
this.dispatchEvent({type: 'readystatechange'});
|
||||||
if (newState === this.DONE && !this._aborted) {
|
if (newState === this.DONE) {
|
||||||
if (this._hasError) {
|
if (this._aborted) {
|
||||||
|
this.dispatchEvent({type: 'abort'});
|
||||||
|
} else if (this._hasError) {
|
||||||
if (this._timedOut) {
|
if (this._timedOut) {
|
||||||
this.dispatchEvent({type: 'timeout'});
|
this.dispatchEvent({type: 'timeout'});
|
||||||
} else {
|
} else {
|
||||||
@@ -478,6 +480,7 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
|
|||||||
} else {
|
} else {
|
||||||
this.dispatchEvent({type: 'load'});
|
this.dispatchEvent({type: 'load'});
|
||||||
}
|
}
|
||||||
|
this.dispatchEvent({type: 'loadend'});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ describe('XMLHttpRequest', function() {
|
|||||||
var handleError;
|
var handleError;
|
||||||
var handleLoad;
|
var handleLoad;
|
||||||
var handleReadyStateChange;
|
var handleReadyStateChange;
|
||||||
|
var handleLoadEnd;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
xhr = new XMLHttpRequest();
|
xhr = new XMLHttpRequest();
|
||||||
@@ -40,16 +41,19 @@ describe('XMLHttpRequest', function() {
|
|||||||
xhr.ontimeout = jest.fn();
|
xhr.ontimeout = jest.fn();
|
||||||
xhr.onerror = jest.fn();
|
xhr.onerror = jest.fn();
|
||||||
xhr.onload = jest.fn();
|
xhr.onload = jest.fn();
|
||||||
|
xhr.onloadend = jest.fn();
|
||||||
xhr.onreadystatechange = jest.fn();
|
xhr.onreadystatechange = jest.fn();
|
||||||
|
|
||||||
handleTimeout = jest.fn();
|
handleTimeout = jest.fn();
|
||||||
handleError = jest.fn();
|
handleError = jest.fn();
|
||||||
handleLoad = jest.fn();
|
handleLoad = jest.fn();
|
||||||
|
handleLoadEnd = jest.fn();
|
||||||
handleReadyStateChange = jest.fn();
|
handleReadyStateChange = jest.fn();
|
||||||
|
|
||||||
xhr.addEventListener('timeout', handleTimeout);
|
xhr.addEventListener('timeout', handleTimeout);
|
||||||
xhr.addEventListener('error', handleError);
|
xhr.addEventListener('error', handleError);
|
||||||
xhr.addEventListener('load', handleLoad);
|
xhr.addEventListener('load', handleLoad);
|
||||||
|
xhr.addEventListener('loadend', handleLoadEnd);
|
||||||
xhr.addEventListener('readystatechange', handleReadyStateChange);
|
xhr.addEventListener('readystatechange', handleReadyStateChange);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -58,6 +62,8 @@ describe('XMLHttpRequest', function() {
|
|||||||
handleTimeout = null;
|
handleTimeout = null;
|
||||||
handleError = null;
|
handleError = null;
|
||||||
handleLoad = null;
|
handleLoad = null;
|
||||||
|
handleLoadEnd = null;
|
||||||
|
handleReadyStateChange = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should transition readyState correctly', function() {
|
it('should transition readyState correctly', function() {
|
||||||
@@ -119,10 +125,12 @@ describe('XMLHttpRequest', function() {
|
|||||||
expect(xhr.readyState).toBe(xhr.DONE);
|
expect(xhr.readyState).toBe(xhr.DONE);
|
||||||
|
|
||||||
expect(xhr.ontimeout.mock.calls.length).toBe(1);
|
expect(xhr.ontimeout.mock.calls.length).toBe(1);
|
||||||
|
expect(xhr.onloadend.mock.calls.length).toBe(1);
|
||||||
expect(xhr.onerror).not.toBeCalled();
|
expect(xhr.onerror).not.toBeCalled();
|
||||||
expect(xhr.onload).not.toBeCalled();
|
expect(xhr.onload).not.toBeCalled();
|
||||||
|
|
||||||
expect(handleTimeout.mock.calls.length).toBe(1);
|
expect(handleTimeout.mock.calls.length).toBe(1);
|
||||||
|
expect(handleLoadEnd.mock.calls.length).toBe(1);
|
||||||
expect(handleError).not.toBeCalled();
|
expect(handleError).not.toBeCalled();
|
||||||
expect(handleLoad).not.toBeCalled();
|
expect(handleLoad).not.toBeCalled();
|
||||||
});
|
});
|
||||||
@@ -136,11 +144,13 @@ describe('XMLHttpRequest', function() {
|
|||||||
|
|
||||||
expect(xhr.onreadystatechange.mock.calls.length).toBe(2);
|
expect(xhr.onreadystatechange.mock.calls.length).toBe(2);
|
||||||
expect(xhr.onerror.mock.calls.length).toBe(1);
|
expect(xhr.onerror.mock.calls.length).toBe(1);
|
||||||
|
expect(xhr.onloadend.mock.calls.length).toBe(1);
|
||||||
expect(xhr.ontimeout).not.toBeCalled();
|
expect(xhr.ontimeout).not.toBeCalled();
|
||||||
expect(xhr.onload).not.toBeCalled();
|
expect(xhr.onload).not.toBeCalled();
|
||||||
|
|
||||||
expect(handleReadyStateChange.mock.calls.length).toBe(2);
|
expect(handleReadyStateChange.mock.calls.length).toBe(2);
|
||||||
expect(handleError.mock.calls.length).toBe(1);
|
expect(handleError.mock.calls.length).toBe(1);
|
||||||
|
expect(handleLoadEnd.mock.calls.length).toBe(1);
|
||||||
expect(handleTimeout).not.toBeCalled();
|
expect(handleTimeout).not.toBeCalled();
|
||||||
expect(handleLoad).not.toBeCalled();
|
expect(handleLoad).not.toBeCalled();
|
||||||
});
|
});
|
||||||
@@ -154,16 +164,18 @@ describe('XMLHttpRequest', function() {
|
|||||||
|
|
||||||
expect(xhr.onreadystatechange.mock.calls.length).toBe(2);
|
expect(xhr.onreadystatechange.mock.calls.length).toBe(2);
|
||||||
expect(xhr.onload.mock.calls.length).toBe(1);
|
expect(xhr.onload.mock.calls.length).toBe(1);
|
||||||
|
expect(xhr.onloadend.mock.calls.length).toBe(1);
|
||||||
expect(xhr.onerror).not.toBeCalled();
|
expect(xhr.onerror).not.toBeCalled();
|
||||||
expect(xhr.ontimeout).not.toBeCalled();
|
expect(xhr.ontimeout).not.toBeCalled();
|
||||||
|
|
||||||
expect(handleReadyStateChange.mock.calls.length).toBe(2);
|
expect(handleReadyStateChange.mock.calls.length).toBe(2);
|
||||||
expect(handleLoad.mock.calls.length).toBe(1);
|
expect(handleLoad.mock.calls.length).toBe(1);
|
||||||
|
expect(handleLoadEnd.mock.calls.length).toBe(1);
|
||||||
expect(handleError).not.toBeCalled();
|
expect(handleError).not.toBeCalled();
|
||||||
expect(handleTimeout).not.toBeCalled();
|
expect(handleTimeout).not.toBeCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call onload function when there is no error', function() {
|
it('should call upload onprogress', function() {
|
||||||
xhr.open('GET', 'blabla');
|
xhr.open('GET', 'blabla');
|
||||||
xhr.send();
|
xhr.send();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user