Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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: 6 additions & 6 deletions test-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ export function act(cb) {
const rerender = setupRerender();

/** @type {() => void} */
let flush, toFlush;
let flushes = [], toFlush;

// Override requestAnimationFrame so we can flush pending hooks.
options.requestAnimationFrame = fc => (flush = fc);
options.requestAnimationFrame = fc => flushes.push(fc);

const finish = () => {
try {
rerender();
while (flush) {
toFlush = flush;
flush = null;
while (flushes.length) {
toFlush = flushes;
flushes = [];

toFlush();
toFlush.forEach(x => x());
rerender();
}
} catch (e) {
Expand Down
52 changes: 52 additions & 0 deletions test-utils/test/shared/act.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,56 @@ describe('act', () => {
});
});
});

describe('act function with finish implementations', () => {
let actDepth = 0;
beforeEach(function () {
actDepth = 0;
options.requestAnimationFrame = null;
});

it('should execute the flush callback using single flush', () => {
let called = false;

act(() => {
options.requestAnimationFrame(() => {
called = true;
});
});

expect(called).to.be.true;
});

it.only('should execute all callbacks using array flush', () => {
Comment thread
rock-57blocks marked this conversation as resolved.
Outdated
let callCount = 0;

act(() => {
options.requestAnimationFrame(() => callCount++);
options.requestAnimationFrame(() => callCount++);
options.requestAnimationFrame(() => callCount++);
});

expect(callCount).to.equal(3);
});

it('should handle errors in single flush', () => {
expect(() => {
act(() => {
options.requestAnimationFrame(() => {
throw new Error('Single flush error');
});
});
}).to.throw('Single flush error');
});

it('should handle errors in array flush', () => {
expect(() => {
act(() => {
options.requestAnimationFrame(() => {
throw new Error('Array flush error');
});
});
}).to.throw('Array flush error');
});
});
});