From 963a53b1a7908c246bfb90a2493f94de365025e7 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Mon, 9 May 2016 10:35:19 -0700 Subject: [PATCH] Fixed XHR tests Summary: The XMLHttpRequest jest tests were attempting to call a private method in XMLHttpRequestBase.js (denoted by an _ prefix). JS doesn't actually have any language-level support for private methods, the use of _ prefix is just a convention. But to prevent casually calling private methods externally, we have a transform that mangles the names of prefixed methods so that that attempting to call them will fail. Using a double _ bypasses this name-mangling mechanism, while still making it clear that the method is intended to be private. Reviewed By: javache Differential Revision: D3276261 fb-gh-sync-id: e0c17e1003d2df09d1a16f78ae9d95bef923d74e fbshipit-source-id: e0c17e1003d2df09d1a16f78ae9d95bef923d74e --- Libraries/Network/XMLHttpRequestBase.js | 10 ++++++---- .../__tests__/XMLHttpRequestBase-test.js | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Libraries/Network/XMLHttpRequestBase.js b/Libraries/Network/XMLHttpRequestBase.js index 5fbf9182d25..d5d5da5c577 100644 --- a/Libraries/Network/XMLHttpRequestBase.js +++ b/Libraries/Network/XMLHttpRequestBase.js @@ -214,7 +214,7 @@ class XMLHttpRequestBase extends EventTarget(...XHR_EVENTS) { this._requestId = requestId; this._subscriptions.push(RCTDeviceEventEmitter.addListener( 'didSendNetworkData', - (args) => this._didUploadProgress(...args) + (args) => this.__didUploadProgress(...args) )); this._subscriptions.push(RCTDeviceEventEmitter.addListener( 'didReceiveNetworkResponse', @@ -226,11 +226,12 @@ class XMLHttpRequestBase extends EventTarget(...XHR_EVENTS) { )); this._subscriptions.push(RCTDeviceEventEmitter.addListener( 'didCompleteNetworkResponse', - (args) => this._didCompleteResponse(...args) + (args) => this.__didCompleteResponse(...args) )); } - _didUploadProgress(requestId: number, progress: number, total: number): void { + // exposed for testing + __didUploadProgress(requestId: number, progress: number, total: number): void { if (requestId === this._requestId) { this.upload.dispatchEvent({ type: 'progress', @@ -266,7 +267,8 @@ class XMLHttpRequestBase extends EventTarget(...XHR_EVENTS) { } } - _didCompleteResponse(requestId: number, error: string, timeOutError: boolean): void { + // exposed for testing + __didCompleteResponse(requestId: number, error: string, timeOutError: boolean): void { if (requestId === this._requestId) { if (error) { this.responseText = error; diff --git a/Libraries/Network/__tests__/XMLHttpRequestBase-test.js b/Libraries/Network/__tests__/XMLHttpRequestBase-test.js index 7218cd9bb91..9d967d4b34b 100644 --- a/Libraries/Network/__tests__/XMLHttpRequestBase-test.js +++ b/Libraries/Network/__tests__/XMLHttpRequestBase-test.js @@ -1,3 +1,12 @@ +/** + * 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. + */ + 'use strict'; jest @@ -55,7 +64,7 @@ describe('XMLHttpRequestBase', function(){ }); it('should call ontimeout function when the request times out', function(){ - xhr._didCompleteResponse(1, 'Timeout', true); + xhr.__didCompleteResponse(1, 'Timeout', true); expect(xhr.readyState).toBe(xhr.DONE); @@ -69,7 +78,7 @@ describe('XMLHttpRequestBase', function(){ }); it('should call onerror function when the request times out', function(){ - xhr._didCompleteResponse(1, 'Generic error'); + xhr.__didCompleteResponse(1, 'Generic error'); expect(xhr.readyState).toBe(xhr.DONE); @@ -85,7 +94,7 @@ describe('XMLHttpRequestBase', function(){ }); it('should call onload function when there is no error', function(){ - xhr._didCompleteResponse(1, null); + xhr.__didCompleteResponse(1, null); expect(xhr.readyState).toBe(xhr.DONE); @@ -105,7 +114,7 @@ describe('XMLHttpRequestBase', function(){ var handleProgress = jest.fn(); xhr.upload.addEventListener('progress', handleProgress); - xhr._didUploadProgress(1, 42, 100); + xhr.__didUploadProgress(1, 42, 100); expect(xhr.upload.onprogress.mock.calls.length).toBe(1); expect(handleProgress.mock.calls.length).toBe(1);