From 9f8179afa7c6d8ca98ea4cb871bde047e4fc5331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 7 Jul 2025 06:42:23 -0700 Subject: [PATCH] Add tests for durationThreshold option for PerformanceObserver (#52464) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/52464 Changelog: [internal] This adds tests for the `durationThreshold` option for `PerformanceObserver.prototype.observe`. Reviewed By: huntie Differential Revision: D77860882 fbshipit-source-id: 1e56391166523d2c4f837f758bc367b58fe8e82e --- .../__tests__/EventTimingAPI-itest.js | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js index 4139eaaa29d..9f9e6615ccf 100644 --- a/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js +++ b/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js @@ -248,4 +248,65 @@ describe('Event Timing API', () => { expect([...performance.eventCounts.values()]).toEqual([1, 1, 3]); }); + + describe('durationThreshold option', () => { + it('works when used with `type`', () => { + const callback = jest.fn(); + + const observer = new PerformanceObserver(callback); + observer.observe({type: 'event', durationThreshold: 50}); + + let forceDelay = false; + + function MyComponent() { + const [count, setCount] = useState(0); + + return ( + { + if (forceDelay) { + sleep(50); + } + setCount(count + 1); + }}> + {count} + + ); + } + + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render(); + }); + + const element = nullthrows( + root.document.documentElement.firstElementChild, + ); + + expect(callback).not.toHaveBeenCalled(); + + Fantom.dispatchNativeEvent(element, 'click'); + + expect(callback).toHaveBeenCalledTimes(0); + + forceDelay = true; + + Fantom.dispatchNativeEvent(element, 'click'); + + expect(callback).toHaveBeenCalledTimes(1); + }); + + it('throws when used together with `entryTypes`', () => { + const observer = new PerformanceObserver(() => {}); + + expect(() => { + observer.observe({ + entryTypes: ['event', 'mark'], + durationThreshold: 100, + }); + }).toThrow( + `Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and durationThreshold arguments.`, + ); + }); + }); });