2016-05-09 17:35:19 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2016-04-15 12:16:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
jest
|
2016-04-28 02:15:28 +00:00
|
|
|
.disableAutomock()
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
.dontMock('event-target-shim')
|
2016-05-25 11:17:35 +00:00
|
|
|
.setMock('NativeModules', {
|
|
|
|
Networking: {
|
|
|
|
addListener: function(){},
|
|
|
|
removeListeners: function(){},
|
|
|
|
}
|
|
|
|
});
|
2016-04-15 12:16:53 +00:00
|
|
|
|
2016-05-25 11:17:35 +00:00
|
|
|
const XMLHttpRequest = require('XMLHttpRequest');
|
2016-04-15 12:16:53 +00:00
|
|
|
|
2016-05-25 11:17:35 +00:00
|
|
|
describe('XMLHttpRequest', function(){
|
2016-04-15 12:16:53 +00:00
|
|
|
var xhr;
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
var handleTimeout;
|
|
|
|
var handleError;
|
|
|
|
var handleLoad;
|
|
|
|
var handleReadyStateChange;
|
2016-04-15 12:16:53 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
|
2016-04-15 12:16:53 +00:00
|
|
|
xhr.ontimeout = jest.fn();
|
|
|
|
xhr.onerror = jest.fn();
|
|
|
|
xhr.onload = jest.fn();
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
xhr.onreadystatechange = jest.fn();
|
|
|
|
|
|
|
|
handleTimeout = jest.fn();
|
|
|
|
handleError = jest.fn();
|
|
|
|
handleLoad = jest.fn();
|
|
|
|
handleReadyStateChange = jest.fn();
|
|
|
|
|
|
|
|
xhr.addEventListener('timeout', handleTimeout);
|
|
|
|
xhr.addEventListener('error', handleError);
|
|
|
|
xhr.addEventListener('load', handleLoad);
|
|
|
|
xhr.addEventListener('readystatechange', handleReadyStateChange);
|
|
|
|
|
2016-05-25 11:17:35 +00:00
|
|
|
xhr.__didCreateRequest(1);
|
2016-04-15 12:16:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
xhr = null;
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
handleTimeout = null;
|
|
|
|
handleError = null;
|
|
|
|
handleLoad = null;
|
2016-04-15 12:16:53 +00:00
|
|
|
});
|
|
|
|
|
2016-05-25 11:17:35 +00:00
|
|
|
it('should transition readyState correctly', function() {
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
expect(xhr.readyState).toBe(xhr.UNSENT);
|
|
|
|
|
|
|
|
xhr.open('GET', 'blabla');
|
|
|
|
|
|
|
|
expect(xhr.onreadystatechange.mock.calls.length).toBe(1);
|
|
|
|
expect(handleReadyStateChange.mock.calls.length).toBe(1);
|
|
|
|
expect(xhr.readyState).toBe(xhr.OPENED);
|
|
|
|
});
|
|
|
|
|
2016-04-15 12:16:53 +00:00
|
|
|
it('should call ontimeout function when the request times out', function(){
|
2016-05-09 17:35:19 +00:00
|
|
|
xhr.__didCompleteResponse(1, 'Timeout', true);
|
2016-04-15 12:16:53 +00:00
|
|
|
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
expect(xhr.readyState).toBe(xhr.DONE);
|
|
|
|
|
|
|
|
expect(xhr.ontimeout.mock.calls.length).toBe(1);
|
2016-04-15 12:16:53 +00:00
|
|
|
expect(xhr.onerror).not.toBeCalled();
|
|
|
|
expect(xhr.onload).not.toBeCalled();
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
|
|
|
|
expect(handleTimeout.mock.calls.length).toBe(1);
|
|
|
|
expect(handleError).not.toBeCalled();
|
|
|
|
expect(handleLoad).not.toBeCalled();
|
2016-04-15 12:16:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onerror function when the request times out', function(){
|
2016-05-09 17:35:19 +00:00
|
|
|
xhr.__didCompleteResponse(1, 'Generic error');
|
2016-04-15 12:16:53 +00:00
|
|
|
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
expect(xhr.readyState).toBe(xhr.DONE);
|
|
|
|
|
|
|
|
expect(xhr.onreadystatechange.mock.calls.length).toBe(1);
|
|
|
|
expect(xhr.onerror.mock.calls.length).toBe(1);
|
2016-04-15 12:16:53 +00:00
|
|
|
expect(xhr.ontimeout).not.toBeCalled();
|
|
|
|
expect(xhr.onload).not.toBeCalled();
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
|
|
|
|
expect(handleReadyStateChange.mock.calls.length).toBe(1);
|
|
|
|
expect(handleError.mock.calls.length).toBe(1);
|
|
|
|
expect(handleTimeout).not.toBeCalled();
|
|
|
|
expect(handleLoad).not.toBeCalled();
|
2016-04-15 12:16:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onload function when there is no error', function(){
|
2016-05-09 17:35:19 +00:00
|
|
|
xhr.__didCompleteResponse(1, null);
|
2016-04-15 12:16:53 +00:00
|
|
|
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
expect(xhr.readyState).toBe(xhr.DONE);
|
|
|
|
|
|
|
|
expect(xhr.onreadystatechange.mock.calls.length).toBe(1);
|
|
|
|
expect(xhr.onload.mock.calls.length).toBe(1);
|
2016-04-15 12:16:53 +00:00
|
|
|
expect(xhr.onerror).not.toBeCalled();
|
|
|
|
expect(xhr.ontimeout).not.toBeCalled();
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
|
|
|
|
expect(handleReadyStateChange.mock.calls.length).toBe(1);
|
|
|
|
expect(handleLoad.mock.calls.length).toBe(1);
|
|
|
|
expect(handleError).not.toBeCalled();
|
|
|
|
expect(handleTimeout).not.toBeCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call onload function when there is no error', function() {
|
|
|
|
xhr.upload.onprogress = jest.fn();
|
|
|
|
var handleProgress = jest.fn();
|
|
|
|
xhr.upload.addEventListener('progress', handleProgress);
|
|
|
|
|
2016-05-09 17:35:19 +00:00
|
|
|
xhr.__didUploadProgress(1, 42, 100);
|
Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.
Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.
Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.
**T
Closes https://github.com/facebook/react-native/pull/7017
Reviewed By: fkgozali
Differential Revision: D3202021
Pulled By: martinbigio
fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
2016-04-28 22:58:25 +00:00
|
|
|
|
|
|
|
expect(xhr.upload.onprogress.mock.calls.length).toBe(1);
|
|
|
|
expect(handleProgress.mock.calls.length).toBe(1);
|
|
|
|
|
|
|
|
expect(xhr.upload.onprogress.mock.calls[0][0].loaded).toBe(42);
|
|
|
|
expect(xhr.upload.onprogress.mock.calls[0][0].total).toBe(100);
|
|
|
|
expect(handleProgress.mock.calls[0][0].loaded).toBe(42);
|
|
|
|
expect(handleProgress.mock.calls[0][0].total).toBe(100);
|
2016-04-15 12:16:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|