-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using the wrong renderer's act() should warn (#15756)
* warn when using the wrong renderer's act around another renderer's updates like it says. it uses a real object as the sigil (instead of just a boolean). specifically, it uses a renderer's flushPassiveEffects as the sigil. We also run tests for this separate from our main suite (which doesn't allow loading multiple renderers in a suite), but makes sure to run this in CI as well. * unneeded (and wrong) comment * run the dom fixture on CI * update the sigil only in __DEV__ * remove the obnoxious comment * use an explicit export for the sigil
- Loading branch information
Sunil Pai
authored
May 29, 2019
1 parent
8ce8b9a
commit 9aad17d
Showing
16 changed files
with
241 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,44 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>sanity test for ReactTestUtils.act</title> | ||
</head> | ||
<body> | ||
this page tests whether act runs properly in a browser. | ||
<br/> | ||
your console should say "5" | ||
<script src='scheduler-unstable_mock.development.js'></script> | ||
<script src='react.development.js'></script> | ||
<script type="text/javascript"> | ||
window.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler = window.SchedulerMock | ||
</script> | ||
<script src='react-dom.development.js'></script> | ||
<script src='react-dom-test-utils.development.js'></script> | ||
<script> | ||
async function run(){ | ||
<head> | ||
<title>sanity test for ReactTestUtils.act</title> | ||
</head> | ||
<body> | ||
this page tests whether act runs properly in a browser. | ||
<br /> | ||
your console should say "5" | ||
<script src="scheduler-unstable_mock.development.js"></script> | ||
<script src="react.development.js"></script> | ||
<script type="text/javascript"> | ||
window.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler = | ||
window.SchedulerMock; | ||
</script> | ||
<script src="react-dom.development.js"></script> | ||
<script src="react-dom-test-utils.development.js"></script> | ||
<script> | ||
// from ReactTestUtilsAct-test.js | ||
function App() { | ||
let [state, setState] = React.useState(0); | ||
async function ticker() { | ||
await null; | ||
setState(x => x + 1); | ||
} | ||
React.useEffect( | ||
() => { | ||
ticker(); | ||
}, | ||
[Math.min(state, 4)], | ||
); | ||
React.useEffect(() => { | ||
ticker(); | ||
}, [Math.min(state, 4)]); | ||
return state; | ||
} | ||
const el = document.createElement('div'); | ||
await ReactTestUtils.act(async () => { | ||
ReactDOM.render(React.createElement(App), el); | ||
}); | ||
// all 5 ticks present and accounted for | ||
console.log(el.innerHTML); | ||
} | ||
run(); | ||
|
||
</script> | ||
</body> | ||
|
||
async function testAsyncAct() { | ||
const el = document.createElement("div"); | ||
await ReactTestUtils.act(async () => { | ||
ReactDOM.render(React.createElement(App), el); | ||
}); | ||
// all 5 ticks present and accounted for | ||
console.log(el.innerHTML); | ||
} | ||
|
||
testAsyncAct(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import TestUtils from 'react-dom/test-utils'; | ||
import TestRenderer from 'react-test-renderer'; | ||
|
||
let spy; | ||
beforeEach(() => { | ||
spy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
function confirmWarning() { | ||
expect(spy).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
"It looks like you're using the wrong act() around your test interactions." | ||
), | ||
'' | ||
); | ||
} | ||
|
||
function App(props) { | ||
return 'hello world'; | ||
} | ||
|
||
it("doesn't warn when you use the right act + renderer: dom", () => { | ||
TestUtils.act(() => { | ||
TestUtils.renderIntoDocument(<App />); | ||
}); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("doesn't warn when you use the right act + renderer: test", () => { | ||
TestRenderer.act(() => { | ||
TestRenderer.create(<App />); | ||
}); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('works with createRoot().render combo', () => { | ||
const root = ReactDOM.unstable_createRoot(document.createElement('div')); | ||
TestRenderer.act(() => { | ||
root.render(<App />); | ||
}); | ||
confirmWarning(); | ||
}); | ||
|
||
it('warns when using the wrong act version - test + dom: render', () => { | ||
TestRenderer.act(() => { | ||
TestUtils.renderIntoDocument(<App />); | ||
}); | ||
confirmWarning(); | ||
}); | ||
|
||
it('warns when using the wrong act version - test + dom: updates', () => { | ||
let setCtr; | ||
function Counter(props) { | ||
const [ctr, _setCtr] = React.useState(0); | ||
setCtr = _setCtr; | ||
return ctr; | ||
} | ||
TestUtils.renderIntoDocument(<Counter />); | ||
TestRenderer.act(() => { | ||
setCtr(1); | ||
}); | ||
confirmWarning(); | ||
}); | ||
|
||
it('warns when using the wrong act version - dom + test: .create()', () => { | ||
TestUtils.act(() => { | ||
TestRenderer.create(<App />); | ||
}); | ||
confirmWarning(); | ||
}); | ||
|
||
it('warns when using the wrong act version - dom + test: .update()', () => { | ||
let root; | ||
// use the right one here so we don't get the first warning | ||
TestRenderer.act(() => { | ||
root = TestRenderer.create(<App key="one" />); | ||
}); | ||
TestUtils.act(() => { | ||
root.update(<App key="two" />); | ||
}); | ||
confirmWarning(); | ||
}); | ||
|
||
it('warns when using the wrong act version - dom + test: updates', () => { | ||
let setCtr; | ||
function Counter(props) { | ||
const [ctr, _setCtr] = React.useState(0); | ||
setCtr = _setCtr; | ||
return ctr; | ||
} | ||
const root = TestRenderer.create(<Counter />); | ||
TestUtils.act(() => { | ||
setCtr(1); | ||
}); | ||
confirmWarning(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.