Skip to content

Commit a32924c

Browse files
fix: support multiple synchronous timings for act (#4619)
* 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]>
1 parent 3618771 commit a32924c

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

test-utils/src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ export function act(cb) {
5757
const rerender = setupRerender();
5858

5959
/** @type {() => void} */
60-
let flush, toFlush;
60+
let flushes = [], toFlush;
6161

6262
// Override requestAnimationFrame so we can flush pending hooks.
63-
options.requestAnimationFrame = fc => (flush = fc);
63+
options.requestAnimationFrame = fc => flushes.push(fc);
6464

6565
const finish = () => {
6666
try {
6767
rerender();
68-
while (flush) {
69-
toFlush = flush;
70-
flush = null;
68+
while (flushes.length) {
69+
toFlush = flushes;
70+
flushes = [];
7171

72-
toFlush();
72+
toFlush.forEach(x => x());
7373
rerender();
7474
}
7575
} catch (e) {

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,54 @@ describe('act', () => {
516516
});
517517
});
518518
});
519+
520+
describe('act function with finish implementations', () => {
521+
beforeEach(function () {
522+
options.requestAnimationFrame = null;
523+
});
524+
525+
it('should execute the flush callback using single flush', () => {
526+
let called = false;
527+
528+
act(() => {
529+
options.requestAnimationFrame(() => {
530+
called = true;
531+
});
532+
});
533+
534+
expect(called).to.be.true;
535+
});
536+
537+
it('should execute all callbacks using array flush', () => {
538+
let callCount = 0;
539+
540+
act(() => {
541+
options.requestAnimationFrame(() => callCount++);
542+
options.requestAnimationFrame(() => callCount++);
543+
options.requestAnimationFrame(() => callCount++);
544+
});
545+
546+
expect(callCount).to.equal(3);
547+
});
548+
549+
it('should handle errors in single flush', () => {
550+
expect(() => {
551+
act(() => {
552+
options.requestAnimationFrame(() => {
553+
throw new Error('Single flush error');
554+
});
555+
});
556+
}).to.throw('Single flush error');
557+
});
558+
559+
it('should handle errors in array flush', () => {
560+
expect(() => {
561+
act(() => {
562+
options.requestAnimationFrame(() => {
563+
throw new Error('Array flush error');
564+
});
565+
});
566+
}).to.throw('Array flush error');
567+
});
568+
});
519569
});

0 commit comments

Comments
 (0)