Skip to content

Fix/inconsistent act #1437

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

Merged
merged 5 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 9 additions & 2 deletions test-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ export function setupRerender() {
}

export function act(cb) {
const previousDebounce = options.debounceRendering;
const previousRequestAnimationFrame = options.requestAnimationFrame;
const rerender = setupRerender();
let flush;
// Override requestAnimationFrame so we can flush pending hooks.
options.requestAnimationFrame = (fc) => flush = fc;
// Execute the callback we were passed.
cb();
// State COULD be built up flush it.
if (flush) {
flush();
}
rerender();
options.debounceRendering = previousDebounce;
// If rerendering with new state has triggered effects
// flush them aswell since options.raf will have repopulated this.
if (flush) {
flush();
}
options.debounceRendering = Component.__test__previousDebounce;
options.requestAnimationFrame = previousRequestAnimationFrame;
}
49 changes: 38 additions & 11 deletions test-utils/test/shared/act.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,47 @@ describe('act', () => {
expect(options.requestAnimationFrame).to.equal(undefined);
});

it('should flush pending effects', () => {
let spy = sinon.spy();
function StateContainer() {
useEffect(spy);
return <div />;
}
act(() => render(<StateContainer />, scratch));
expect(spy).to.be.calledOnce;
});

it('should flush pending and initial effects', () => {
const spy = sinon.spy();
function StateContainer() {
const [count, setCount] = useState(0);
useEffect(() => spy(), [count]);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(c => c + 11)} />
</div>
);
}

act(() => render(<StateContainer />, scratch));
expect(spy).to.be.calledOnce;
expect(scratch.textContent).to.include('Count: 0');
act(() => {
const button = scratch.querySelector('button');
button.click();
expect(spy).to.be.calledOnce;
expect(scratch.textContent).to.include('Count: 0');
});
expect(spy).to.be.calledTwice;
expect(scratch.textContent).to.include('Count: 1');
});

it('should drain the queue of hooks', () => {
const spy = sinon.spy();
function StateContainer() {
const [count, setCount] = useState(0);
useEffect(() => spy());
return (<div>
<p>Count: {count}</p>
<button onClick={() => setCount(c => c + 11)} />
Expand All @@ -43,17 +81,6 @@ describe('act', () => {
expect(scratch.textContent).to.include('Count: 1');
});

it('should flush pending effects', () => {
let spy = sinon.spy();
function StateContainer() {
useEffect(spy);
return <div />;
}

act(() => render(<StateContainer />, scratch));
expect(spy).to.be.calledOnce;
});

it('should restore options.requestAnimationFrame', () => {
const spy = sinon.spy();

Expand Down