From 89a9ca66887244f09dfdefb5a361ad1f5c44bf77 Mon Sep 17 00:00:00 2001 From: Christoph Pojer Date: Thu, 30 Jun 2016 01:57:30 -0700 Subject: [PATCH] Update some JS in preparation for some Jest updates. Summary: * Next version of Jest doesn't allow non test files in __tests__ folders. * I'm trying to switch all tests off of jsdom on react-native. This should save 500ms of time when running a single test because jsdom is slow to load and react-native is also not supposed to run in a DOM environment, so let's not pretend we are providing the DOM in tests. * Make the bridge config configurable so that when we disable automocking and we reset the registry we can redefine the value. Oh also, stop using lodash in Server.js. First off, lodash 3 doesn't work in Jest's node env because it does some crazy stuff, second because we don't need to load all of lodash for debounce. Reviewed By: davidaurelio Differential Revision: D3502886 fbshipit-source-id: 1da1cfba9ed12264d81945b702e7a429d5f84424 --- Libraries/Animated/src/AnimatedImplementation.js | 6 +++--- Libraries/Animated/src/__tests__/Animated-test.js | 4 ++-- .../Animated/src/__tests__/AnimatedNative-test.js | 4 ++-- Libraries/BatchedBridge/BatchedBridge.js | 5 ++++- .../MessageQueueTestConfig.js | 0 .../MessageQueueTestModule1.js | 0 .../MessageQueueTestModule2.js | 0 Libraries/Utilities/__tests__/MessageQueue-test.js | 6 +++--- packager/react-packager/src/Server/index.js | 11 +++++++++-- 9 files changed, 23 insertions(+), 13 deletions(-) rename Libraries/Utilities/{__tests__ => __mocks__}/MessageQueueTestConfig.js (100%) rename Libraries/Utilities/{__tests__ => __mocks__}/MessageQueueTestModule1.js (100%) rename Libraries/Utilities/{__tests__ => __mocks__}/MessageQueueTestModule2.js (100%) diff --git a/Libraries/Animated/src/AnimatedImplementation.js b/Libraries/Animated/src/AnimatedImplementation.js index a8f01c535..09b960aa1 100644 --- a/Libraries/Animated/src/AnimatedImplementation.js +++ b/Libraries/Animated/src/AnimatedImplementation.js @@ -326,7 +326,7 @@ class TimingAnimation extends Animation { super.stop(); this.__active = false; clearTimeout(this._timeout); - window.cancelAnimationFrame(this._animationFrame); + global.cancelAnimationFrame(this._animationFrame); this.__debouncedOnEnd({finished: false}); } } @@ -396,7 +396,7 @@ class DecayAnimation extends Animation { stop(): void { super.stop(); this.__active = false; - window.cancelAnimationFrame(this._animationFrame); + global.cancelAnimationFrame(this._animationFrame); this.__debouncedOnEnd({finished: false}); } } @@ -611,7 +611,7 @@ class SpringAnimation extends Animation { stop(): void { super.stop(); this.__active = false; - window.cancelAnimationFrame(this._animationFrame); + global.cancelAnimationFrame(this._animationFrame); this.__debouncedOnEnd({finished: false}); } } diff --git a/Libraries/Animated/src/__tests__/Animated-test.js b/Libraries/Animated/src/__tests__/Animated-test.js index 37941f6ca..02cb50575 100644 --- a/Libraries/Animated/src/__tests__/Animated-test.js +++ b/Libraries/Animated/src/__tests__/Animated-test.js @@ -100,8 +100,8 @@ describe('Animated', () => { it('stops animation when detached', () => { // jest environment doesn't have cancelAnimationFrame :( - if (!window.cancelAnimationFrame) { - window.cancelAnimationFrame = jest.fn(); + if (!global.cancelAnimationFrame) { + global.cancelAnimationFrame = jest.fn(); } var anim = new Animated.Value(0); diff --git a/Libraries/Animated/src/__tests__/AnimatedNative-test.js b/Libraries/Animated/src/__tests__/AnimatedNative-test.js index 71c8d36e6..34175ef83 100644 --- a/Libraries/Animated/src/__tests__/AnimatedNative-test.js +++ b/Libraries/Animated/src/__tests__/AnimatedNative-test.js @@ -35,8 +35,8 @@ describe('Animated', () => { nativeAnimatedModule.dropAnimatedNode = jest.fn(); // jest environment doesn't have cancelAnimationFrame :( - if (!window.cancelAnimationFrame) { - window.cancelAnimationFrame = jest.fn(); + if (!global.cancelAnimationFrame) { + global.cancelAnimationFrame = jest.fn(); } }); diff --git a/Libraries/BatchedBridge/BatchedBridge.js b/Libraries/BatchedBridge/BatchedBridge.js index 77dc9e6a5..f1450ed4b 100644 --- a/Libraries/BatchedBridge/BatchedBridge.js +++ b/Libraries/BatchedBridge/BatchedBridge.js @@ -38,6 +38,9 @@ if (__DEV__) { // would export it. A possible fix would be to trim the dependencies in // MessageQueue to its minimal features and embed that in the native runtime. -Object.defineProperty(global, '__fbBatchedBridge', { value: BatchedBridge }); +Object.defineProperty(global, '__fbBatchedBridge', { + configurable: true, + value: BatchedBridge, +}); module.exports = BatchedBridge; diff --git a/Libraries/Utilities/__tests__/MessageQueueTestConfig.js b/Libraries/Utilities/__mocks__/MessageQueueTestConfig.js similarity index 100% rename from Libraries/Utilities/__tests__/MessageQueueTestConfig.js rename to Libraries/Utilities/__mocks__/MessageQueueTestConfig.js diff --git a/Libraries/Utilities/__tests__/MessageQueueTestModule1.js b/Libraries/Utilities/__mocks__/MessageQueueTestModule1.js similarity index 100% rename from Libraries/Utilities/__tests__/MessageQueueTestModule1.js rename to Libraries/Utilities/__mocks__/MessageQueueTestModule1.js diff --git a/Libraries/Utilities/__tests__/MessageQueueTestModule2.js b/Libraries/Utilities/__mocks__/MessageQueueTestModule2.js similarity index 100% rename from Libraries/Utilities/__tests__/MessageQueueTestModule2.js rename to Libraries/Utilities/__mocks__/MessageQueueTestModule2.js diff --git a/Libraries/Utilities/__tests__/MessageQueue-test.js b/Libraries/Utilities/__tests__/MessageQueue-test.js index 32d53182f..7a69e8d7e 100644 --- a/Libraries/Utilities/__tests__/MessageQueue-test.js +++ b/Libraries/Utilities/__tests__/MessageQueue-test.js @@ -9,7 +9,7 @@ */ 'use strict'; -const MessageQueueTestConfig = require('./MessageQueueTestConfig'); +const MessageQueueTestConfig = require('MessageQueueTestConfig'); jest.unmock('MessageQueue'); let MessageQueue; @@ -46,8 +46,8 @@ describe('MessageQueue', function() { beforeEach(function() { jest.resetModuleRegistry(); MessageQueue = require('MessageQueue'); - MessageQueueTestModule1 = require('./MessageQueueTestModule1'); - MessageQueueTestModule2 = require('./MessageQueueTestModule2'); + MessageQueueTestModule1 = require('MessageQueueTestModule1'); + MessageQueueTestModule2 = require('MessageQueueTestModule2'); queue = new MessageQueue( () => MessageQueueTestConfig ); diff --git a/packager/react-packager/src/Server/index.js b/packager/react-packager/src/Server/index.js index ef35844fd..c7ab61bee 100644 --- a/packager/react-packager/src/Server/index.js +++ b/packager/react-packager/src/Server/index.js @@ -16,11 +16,18 @@ const Bundler = require('../Bundler'); const Promise = require('promise'); const SourceMapConsumer = require('source-map').SourceMapConsumer; -const _ = require('lodash'); const declareOpts = require('../lib/declareOpts'); const path = require('path'); const url = require('url'); +function debounce(fn, delay) { + var timeout; + return () => { + clearTimeout(timeout); + timeout = setTimeout(fn, delay); + }; +} + const validateOpts = declareOpts({ projectRoots: { type: 'array', @@ -209,7 +216,7 @@ class Server { this._fileWatcher.on('all', this._onFileChange.bind(this)); - this._debouncedFileChangeHandler = _.debounce(filePath => { + this._debouncedFileChangeHandler = debounce(filePath => { this._clearBundles(); this._informChangeWatchers(); }, 50);