Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 10 additions & 2 deletions test-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ 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
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;
}
50 changes: 39 additions & 11 deletions test-utils/test/shared/act.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { options, createElement as h, render } from 'preact';
import { useEffect, useState } from 'preact/hooks';
import sinon from 'sinon';

import { setupScratch, teardown } from '../../../test/_util/helpers';
import { act } from '../../src';
Expand All @@ -24,9 +25,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 +82,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