Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose TestUtils.act() for batching actions in tests #14744

Merged
merged 33 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e1f7952
expose unstable_interact for batching actions in tests
Feb 1, 2019
5407e88
move to TestUtils
Feb 1, 2019
e8a03d0
move it all into testutils
Feb 1, 2019
723f347
s/interact/act
Feb 1, 2019
ac7416d
warn when calling hook-like setState outside batching mode
Feb 2, 2019
1993be9
pass tests
Feb 2, 2019
3c18517
merge-temp
Feb 2, 2019
b98d051
Merge branch 'flush-sync-effects' into warn-jsdom-setstate
Feb 2, 2019
324fe0e
move jsdom test to callsite
Feb 2, 2019
e7ebd84
mark failing tests
Feb 2, 2019
0d3b45a
pass most tests (except one)
Feb 3, 2019
20a959d
augh IE
Feb 3, 2019
ee4ebb9
pass fuzz tests
Feb 3, 2019
4cb92ad
better warning, expose the right batchedUpdates on TestRenderer for www
Feb 3, 2019
aa1d531
move it into hooks, test for dom
Feb 3, 2019
00d0614
expose a flag on the host config, move stuff around
Feb 3, 2019
1b2e657
rename, pass flow
Feb 3, 2019
86a443e
pass flow... again
Feb 3, 2019
cec1ed1
tweak .act() type
Feb 3, 2019
a216c15
Merge remote-tracking branch 'upstream/master' into flush-sync-effects
Feb 3, 2019
6fa4202
enable for all jest environments/renderers; pass (most) tests.
Feb 4, 2019
6cba8f5
pass all tests
Feb 4, 2019
f946089
expose just the warning from the scheduler
Feb 4, 2019
8617b64
don't return values
Feb 5, 2019
3bfa963
Merge remote-tracking branch 'upstream/master' into flush-sync-effects
Feb 5, 2019
8f30593
Merge branch 'master' into flush-sync-effects
Feb 5, 2019
c126f10
a bunch of changes.
Feb 5, 2019
790ce2a
fixes and nits
Feb 5, 2019
32c7e1a
"fire events that udpates state"
Feb 5, 2019
5873317
nit
Feb 5, 2019
0a4b3cf
🙄
Feb 5, 2019
b6689e1
my bad
Feb 5, 2019
26a6db5
hi andrew
Feb 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
expose a flag on the host config, move stuff around
  • Loading branch information
Sunil Pai committed Feb 3, 2019
commit 00d0614b0c9a69d56ecb52239e634cc2b59af30e
18 changes: 18 additions & 0 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
DOCUMENT_FRAGMENT_NODE,
} from '../shared/HTMLNodeType';
import dangerousStyleValue from '../shared/dangerousStyleValue';
import {canUseDOM} from 'shared/ExecutionEnvironment';

import type {DOMContainer} from './ReactDOM';

Expand Down Expand Up @@ -647,3 +648,20 @@ export function didNotFindHydratableTextInstance(
warnForInsertedHydratedText(parentInstance, text);
}
}

// in a dom-like test environment, we want to warn if dispatchAction()
// is called outside of a batchedUpdates/TestUtils.act(...) call.
let ensureBatchedDispatchedActions = false;

if (__DEV__) {
if (
canUseDOM &&
(navigator.userAgent.indexOf('Node.js') >= 0 ||
navigator.userAgent.indexOf('jsdom') >= 0)
threepointone marked this conversation as resolved.
Show resolved Hide resolved
// we should probably add puppeteer-like environments here too
) {
ensureBatchedDispatchedActions = true;
}
}

export const shouldBatchDispatchedActions = ensureBatchedDispatchedActions;
67 changes: 7 additions & 60 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ import {
} from './ReactHookEffectTags';
import {
scheduleWork,
getBatchingStatus,
ensureBatchingAndScheduleWork,
computeExpirationForFiber,
flushPassiveEffects,
requestCurrentTime,
} from './ReactFiberScheduler';

import {shouldBatchDispatchedActions} from './ReactFiberHostConfig';
import invariant from 'shared/invariant';
import warning from 'shared/warning';
import getComponentName from 'shared/getComponentName';
import is from 'shared/objectIs';
import {canUseDOM} from 'shared/ExecutionEnvironment';
import {markWorkInProgressReceivedUpdate} from './ReactFiberBeginWork';

type Update<S, A> = {
Expand Down Expand Up @@ -1129,66 +1129,13 @@ function dispatchAction<S, A>(
}
}
if (__DEV__) {
ensureBatchingAndScheduleWork(fiber, expirationTime);
} else {
scheduleWork(fiber, expirationTime);
}
}
}

// in a dom-like test environment, we want to warn if dispatchAction()
// is called outside of a batchedUpdates/TestUtils.act(...) call.

let isNotDOMRenderer = undefined;
// some folks initialize a jsdom, but still use TestRenderer.
// So when dispatchAction is called first, we'll test the
// fiber tree and set this value.

let ensureBatchingAndScheduleWork = (
fiber: Fiber,
expirationTime: ExpirationTime,
) => {
scheduleWork(fiber, expirationTime);
};
// we'll swap out the function definition when we learn more about the environment

function ensureBatchingAndScheduleWorkImpl(
fiber: Fiber,
expirationTime: ExpirationTime,
) {
if (__DEV__) {
if (isNotDOMRenderer === undefined) {
let f = fiber;
while (f.return) {
f = f.return;
}
if (f.stateNode.containerInfo instanceof HTMLElement) {
isNotDOMRenderer = false;
if (shouldBatchDispatchedActions === true) {
ensureBatchingAndScheduleWork(fiber, expirationTime);
} else {
isNotDOMRenderer = true;
ensureBatchingAndScheduleWork = scheduleWork;
scheduleWork(fiber, expirationTime);
}
} else {
scheduleWork(fiber, expirationTime);
}
if (isNotDOMRenderer === false && getBatchingStatus() === false) {
warning(
false,
'It looks like you are in a test environment, trying to ' +
'set state outside of TestUtils.act(...). ' +
'This could lead to unexpected ui while testing. Use ' +
'ReactTestUtils.act(...) to batch your updates and remove this warning.',
);
}
}
scheduleWork(fiber, expirationTime);
}

if (__DEV__) {
if (
canUseDOM &&
(navigator.userAgent.indexOf('Node.js') >= 0 ||
navigator.userAgent.indexOf('jsdom') >= 0)
// we should probably add puppeteer-like environments here too
) {
ensureBatchingAndScheduleWork = ensureBatchingAndScheduleWorkImpl;
}
}
18 changes: 16 additions & 2 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1789,8 +1789,22 @@ function scheduleWorkToRoot(fiber: Fiber, expirationTime): FiberRoot | null {
return root;
}

export function getBatchingStatus(): boolean {
return isBatchingUpdates;
export function ensureBatchingAndScheduleWork(
fiber: Fiber,
expirationTime: ExpirationTime,
) {
if (__DEV__) {
if (isBatchingUpdates === false) {
warningWithoutStack(
false,
'It looks like you are in a test environment, trying to ' +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to remember to update the wording, e.g. "UI" not "ui", and also it's not clear where to import "act" from. It's also not clear what "unexpected UI in testing" means.

'set state outside of TestUtils.act(...). ' +
'This could lead to unexpected ui while testing. Use ' +
'ReactTestUtils.act(...) to batch your updates and remove this warning.',
);
}
}
scheduleWork(fiber, expirationTime);
}

function scheduleWork(fiber: Fiber, expirationTime: ExpirationTime) {
Expand Down