Skip to content

Commit

Permalink
fix: support multiple synchronous timings for act (#4619)
Browse files Browse the repository at this point in the history
* fix: support multiple synchronous timings for act

* add unit test

* addressed code feed back

* Update test-utils/test/shared/act.test.js

Co-authored-by: Jovi De Croock <[email protected]>

* fix lint

---------

Co-authored-by: Jovi De Croock <[email protected]>
  • Loading branch information
rock-57blocks and JoviDeCroock authored Dec 27, 2024
1 parent 3618771 commit a32924c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
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
50 changes: 50 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,54 @@ describe('act', () => {
});
});
});

describe('act function with finish implementations', () => {
beforeEach(function () {
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('should execute all callbacks using array flush', () => {
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');
});
});
});

0 comments on commit a32924c

Please sign in to comment.