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
move to TestUtils
  • Loading branch information
Sunil Pai committed Feb 1, 2019
commit 5407e887d80ffd83711fe0779cb833664ab91af0
53 changes: 0 additions & 53 deletions packages/react-dom/src/__tests__/ReactDOM-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,57 +509,4 @@ describe('ReactDOM', () => {
' in App (at **)',
]);
});

it('can use interact to batch effects', () => {
function App(props) {
React.useEffect(props.callback);
return null;
}
const container = document.createElement('div');
document.body.appendChild(container);

try {
let called = false;
ReactDOM.unstable_interact(() => {
ReactDOM.render(
<App
callback={() => {
called = true;
}}
/>,
container,
);
});

expect(called).toBe(true);
} finally {
document.body.removeChild(container);
}
});
it('can use interact to batch effects on updates too', () => {
function App() {
let [ctr, setCtr] = React.useState(0);
return (
<button id="button" onClick={() => setCtr(x => x + 1)}>
{ctr}
</button>
);
}
const container = document.createElement('div');
document.body.appendChild(container);
let button;
try {
ReactDOM.unstable_interact(() => {
ReactDOM.render(<App />, container);
});
button = document.getElementById('button');
expect(button.innerHTML).toBe('0');
ReactDOM.unstable_interact(() => {
button.dispatchEvent(new MouseEvent('click', {bubbles: true}));
});
expect(button.innerHTML).toBe('1');
} finally {
document.body.removeChild(container);
}
});
});
54 changes: 54 additions & 0 deletions packages/react-dom/src/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,58 @@ describe('ReactTestUtils', () => {
ReactTestUtils.renderIntoDocument(<Component />);
expect(mockArgs.length).toEqual(0);
});

it('can use interact to batch effects', () => {
function App(props) {
React.useEffect(props.callback);
return null;
}
const container = document.createElement('div');
document.body.appendChild(container);

try {
let called = false;
ReactTestUtils.interact(() => {
ReactDOM.render(
<App
callback={() => {
called = true;
}}
/>,
container,
);
threepointone marked this conversation as resolved.
Show resolved Hide resolved
});

expect(called).toBe(true);
} finally {
document.body.removeChild(container);
}
});

it('can use interact to batch effects on updates too', () => {
function App() {
let [ctr, setCtr] = React.useState(0);
return (
<button id="button" onClick={() => setCtr(x => x + 1)}>
{ctr}
</button>
);
}
const container = document.createElement('div');
document.body.appendChild(container);
let button;
try {
ReactTestUtils.interact(() => {
ReactDOM.render(<App />, container);
});
button = document.getElementById('button');
expect(button.innerHTML).toBe('0');
ReactTestUtils.interact(() => {
button.dispatchEvent(new MouseEvent('click', {bubbles: true}));
});
expect(button.innerHTML).toBe('1');
} finally {
document.body.removeChild(container);
}
});
});
13 changes: 8 additions & 5 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,13 @@ function createPortal(
return createPortalImpl(children, container, null, key);
}

// a helper for tests to scope their actions on the dom
// batches and flushes effects and updates
function batchedInteraction(callback: () => void) {
batchedUpdates(callback);
flushPassiveEffects();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function should move to ReactTestUtils and instead using the ReactDOM.render(null...) trick to flush the passive effects. That way we don't have to add any unnecessary invasive APIs to the production ReactDOM. We can add private APIs once we have the new bundle that is exclusively for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed, I felt icky doing this. will change,


const ReactDOM: Object = {
createPortal,

Expand Down Expand Up @@ -803,11 +810,6 @@ const ReactDOM: Object = {

unstable_interactiveUpdates: interactiveUpdates,

unstable_interact(callback: () => void) {
batchedUpdates(callback);
flushPassiveEffects();
},

flushSync: flushSync,

unstable_createRoot: createRoot,
Expand All @@ -828,6 +830,7 @@ const ReactDOM: Object = {
restoreStateIfNeeded,
dispatchEvent,
runEventsInBatch,
batchedInteraction,
],
},
};
Expand Down
13 changes: 8 additions & 5 deletions packages/react-dom/src/fire/ReactFire.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,13 @@ function createPortal(
return createPortalImpl(children, container, null, key);
}

// a helper for tests to scope their actions on the dom
// batches and flushes effects and updates
function batchedInteraction(callback: () => void) {
batchedUpdates(callback);
flushPassiveEffects();
}

const ReactDOM: Object = {
createPortal,

Expand Down Expand Up @@ -808,11 +815,6 @@ const ReactDOM: Object = {

unstable_interactiveUpdates: interactiveUpdates,

unstable_interact(callback: () => void) {
batchedUpdates(callback);
flushPassiveEffects();
},

flushSync: flushSync,

unstable_createRoot: createRoot,
Expand All @@ -833,6 +835,7 @@ const ReactDOM: Object = {
restoreStateIfNeeded,
dispatchEvent,
runEventsInBatch,
batchedInteraction,
],
},
};
Expand Down
5 changes: 5 additions & 0 deletions packages/react-dom/src/test-utils/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const [
restoreStateIfNeeded,
dispatchEvent,
runEventsInBatch,
batchedInteraction,
] = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Events;

function Event(suffix) {}
Expand Down Expand Up @@ -380,6 +381,10 @@ const ReactTestUtils = {

Simulate: null,
SimulateNative: {},

interact(callback: () => void) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Drop the inter. Just call it act. It's cleaner.

batchedInteraction(callback);
},
};

/**
Expand Down