Skip to content

Commit

Permalink
Fiber ReactDOM shouldn't throw on import in Node environment if it's …
Browse files Browse the repository at this point in the history
…unused (#9389)

* Fixes #9102 by fake polyfilling rAF (and rIC)

* Ensure we restore globals even if test fails + minor nits

* Remove periods
  • Loading branch information
ManasJayanth authored and gaearon committed Apr 27, 2017
1 parent 70c0196 commit 182642b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
4 changes: 4 additions & 0 deletions scripts/fiber/tests-passing.txt
Expand Up @@ -1642,6 +1642,10 @@ src/renderers/native/__tests__/ReactNativeMount-test.js
* returns the correct instance and calls it in the callback
* renders and reorders children

src/renderers/shared/__tests__/ReactDOMFrameScheduling-test.js
* throws when requestAnimationFrame is not polyfilled in the browser
* can import findDOMNode in Node environment

src/renderers/shared/__tests__/ReactDebugTool-test.js
* should add and remove hooks
* warns once when an error is thrown in hook
Expand Down
20 changes: 19 additions & 1 deletion src/renderers/shared/ReactDOMFrameScheduling.js
Expand Up @@ -23,11 +23,29 @@
import type {Deadline} from 'ReactFiberReconciler';

var invariant = require('fbjs/lib/invariant');
var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');

// TODO: There's no way to cancel these, because Fiber doesn't atm.
let rAF: (callback: (time: number) => void) => number;
let rIC: (callback: (deadline: Deadline) => void) => number;
if (typeof requestAnimationFrame !== 'function') {

if (!ExecutionEnvironment.canUseDOM) {
rAF = function(frameCallback: (time: number) => void): number {
setTimeout(frameCallback, 16);
return 0;
};

rIC = function(frameCallback: (deadline: Deadline) => void): number {
setTimeout(() => {
frameCallback({
timeRemaining() {
return Infinity;
},
});
});
return 0;
};
} else if (typeof requestAnimationFrame !== 'function') {
invariant(
false,
'React depends on requestAnimationFrame. Make sure that you load a ' +
Expand Down
55 changes: 55 additions & 0 deletions src/renderers/shared/__tests__/ReactDOMFrameScheduling-test.js
@@ -0,0 +1,55 @@
/**
* Copyright 2016-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.
*
* @emails react-core
*/

'use strict';

const ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
const describeFiber = ReactDOMFeatureFlags.useFiber ? describe : xdescribe;

describeFiber('ReactDOMFrameScheduling', () => {
it('throws when requestAnimationFrame is not polyfilled in the browser', () => {
const previousRAF = global.requestAnimationFrame;
try {
global.requestAnimationFrame = undefined;
jest.resetModules();
expect(() => {
require('ReactDOM');
}).toThrow(
'React depends on requestAnimationFrame. Make sure that you load a ' +
'polyfill in older browsers.',
);
} finally {
global.requestAnimationFrame = previousRAF;
}
});

// We're just testing importing, not using it.
// It is important because even isomorphic components may import it.
it('can import findDOMNode in Node environment', () => {
const previousRAF = global.requestAnimationFrame;
const previousRIC = global.requestIdleCallback;
const prevWindow = global.window;
try {
global.requestAnimationFrame = undefined;
global.requestIdleCallback = undefined;
// Simulate the Node environment:
delete global.window;
jest.resetModules();
expect(() => {
require('ReactDOM');
}).not.toThrow();
} finally {
global.requestAnimationFrame = previousRAF;
global.requestIdleCallback = previousRIC;
global.window = prevWindow;
}
});
});

0 comments on commit 182642b

Please sign in to comment.