From 6e599f965cd3b3867745fe7024ba8cd73763ad23 Mon Sep 17 00:00:00 2001 From: rock-57blocks Date: Thu, 26 Dec 2024 10:44:56 +0800 Subject: [PATCH 1/5] fix: support multiple synchronous timings for act --- test-utils/src/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test-utils/src/index.js b/test-utils/src/index.js index b6efc22d40..3a9d5a4f3a 100644 --- a/test-utils/src/index.js +++ b/test-utils/src/index.js @@ -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) { From d071a3eb9786fa5017dd7c40e1c29a356edca3cb Mon Sep 17 00:00:00 2001 From: rock-57blocks Date: Thu, 26 Dec 2024 15:56:57 +0800 Subject: [PATCH 2/5] add unit test --- test-utils/test/shared/act.test.js | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test-utils/test/shared/act.test.js b/test-utils/test/shared/act.test.js index ca17b93369..553851bc37 100644 --- a/test-utils/test/shared/act.test.js +++ b/test-utils/test/shared/act.test.js @@ -516,4 +516,56 @@ describe('act', () => { }); }); }); + + describe('act function with finish implementations', function () { + let actDepth = 0; + beforeEach(function () { + actDepth = 0; // Reset actDepth before each test + options.requestAnimationFrame = null; // Reset before each test + }); + + it('should execute the flush callback using single flush', function () { + let called = false; + + act(() => { + options.requestAnimationFrame(() => { + called = true; // This should be called + }); + }); + + expect(called).to.be.true; // Verify the callback was executed + }); + + it.only('should execute all callbacks using array flush', function () { + let callCount = 0; + + act(() => { + options.requestAnimationFrame(() => callCount++); + options.requestAnimationFrame(() => callCount++); + options.requestAnimationFrame(() => callCount++); + }); + + expect(callCount).to.equal(3); // Verify all callbacks were executed + }); + + it('should handle errors in single flush', function () { + expect(() => { + act(() => { + options.requestAnimationFrame(() => { + throw new Error('Single flush error'); + }); + }); + }).to.throw('Single flush error'); + }); + + it('should handle errors in array flush', function () { + expect(() => { + act(() => { + options.requestAnimationFrame(() => { + throw new Error('Array flush error'); + }); + }); + }).to.throw('Array flush error'); + }); + }); }); From c67d5d8a2ddac25b4e30ac651f855faa90f03992 Mon Sep 17 00:00:00 2001 From: rock-57blocks Date: Thu, 26 Dec 2024 16:23:11 +0800 Subject: [PATCH 3/5] addressed code feed back --- test-utils/test/shared/act.test.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test-utils/test/shared/act.test.js b/test-utils/test/shared/act.test.js index 553851bc37..d80f80e49c 100644 --- a/test-utils/test/shared/act.test.js +++ b/test-utils/test/shared/act.test.js @@ -517,26 +517,26 @@ describe('act', () => { }); }); - describe('act function with finish implementations', function () { + describe('act function with finish implementations', () => { let actDepth = 0; beforeEach(function () { - actDepth = 0; // Reset actDepth before each test - options.requestAnimationFrame = null; // Reset before each test + actDepth = 0; + options.requestAnimationFrame = null; }); - it('should execute the flush callback using single flush', function () { + it('should execute the flush callback using single flush', () => { let called = false; act(() => { options.requestAnimationFrame(() => { - called = true; // This should be called + called = true; }); }); - expect(called).to.be.true; // Verify the callback was executed + expect(called).to.be.true; }); - it.only('should execute all callbacks using array flush', function () { + it.only('should execute all callbacks using array flush', () => { let callCount = 0; act(() => { @@ -545,10 +545,10 @@ describe('act', () => { options.requestAnimationFrame(() => callCount++); }); - expect(callCount).to.equal(3); // Verify all callbacks were executed + expect(callCount).to.equal(3); }); - it('should handle errors in single flush', function () { + it('should handle errors in single flush', () => { expect(() => { act(() => { options.requestAnimationFrame(() => { @@ -558,7 +558,7 @@ describe('act', () => { }).to.throw('Single flush error'); }); - it('should handle errors in array flush', function () { + it('should handle errors in array flush', () => { expect(() => { act(() => { options.requestAnimationFrame(() => { From c71799bec170403846f2128b9f9ea6a016e45064 Mon Sep 17 00:00:00 2001 From: rock-57blocks <91400547+rock-57blocks@users.noreply.github.com> Date: Fri, 27 Dec 2024 17:26:38 +0800 Subject: [PATCH 4/5] Update test-utils/test/shared/act.test.js Co-authored-by: Jovi De Croock --- test-utils/test/shared/act.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-utils/test/shared/act.test.js b/test-utils/test/shared/act.test.js index d80f80e49c..13c553f22c 100644 --- a/test-utils/test/shared/act.test.js +++ b/test-utils/test/shared/act.test.js @@ -536,7 +536,7 @@ describe('act', () => { expect(called).to.be.true; }); - it.only('should execute all callbacks using array flush', () => { + it('should execute all callbacks using array flush', () => { let callCount = 0; act(() => { From a509f58fc3a7acaf62daeb2b4b337203e918f9e6 Mon Sep 17 00:00:00 2001 From: rock-57blocks Date: Fri, 27 Dec 2024 17:51:18 +0800 Subject: [PATCH 5/5] fix lint --- test-utils/test/shared/act.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test-utils/test/shared/act.test.js b/test-utils/test/shared/act.test.js index 13c553f22c..707718f9ea 100644 --- a/test-utils/test/shared/act.test.js +++ b/test-utils/test/shared/act.test.js @@ -518,9 +518,7 @@ describe('act', () => { }); describe('act function with finish implementations', () => { - let actDepth = 0; beforeEach(function () { - actDepth = 0; options.requestAnimationFrame = null; });