diff --git a/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js new file mode 100644 index 00000000000..c3c65486e34 --- /dev/null +++ b/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js @@ -0,0 +1,109 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; + +import type Performance from '../Performance'; +import type { + PerformanceObserver as PerformanceObserverT, + PerformanceObserverEntryList, +} from '../PerformanceObserver'; + +import setUpPerformanceObserver from '../../../setup/setUpPerformanceObserver'; +import * as Fantom from '@react-native/fantom'; + +setUpPerformanceObserver(); + +declare var performance: Performance; +declare var PerformanceObserver: Class; + +describe('PerformanceObserver', () => { + it('receives notifications for marks and measures', () => { + const callback = jest.fn(); + const observer = new PerformanceObserver(callback); + observer.observe({entryTypes: ['mark', 'measure']}); + + expect(callback).not.toHaveBeenCalled(); + + let mark1; + let mark2; + let measure1; + let measure2; + + Fantom.runTask(() => { + mark1 = performance.mark('mark1', {startTime: 20}); + mark2 = performance.mark('mark2', {startTime: 10}); + measure1 = performance.measure('measure1', { + start: 50, + duration: 20, + }); + measure2 = performance.measure('measure2', { + start: 15, + duration: 10, + }); + + // The notification should be dispatched asynchronously. + expect(callback).not.toHaveBeenCalled(); + + Fantom.scheduleTask(() => { + // The notification should be dispatched with a low priority. + expect(callback).not.toHaveBeenCalled(); + }); + }); + + expect(callback).toHaveBeenCalledTimes(1); + + const entries: PerformanceObserverEntryList = callback.mock.calls[0][0]; + + // Sorted by startTime. + expect(entries.getEntries()).toEqual([mark2, measure2, mark1, measure1]); + + // Sorted by startTime. + expect(entries.getEntriesByName('mark1')).toEqual([mark1]); + + // Sorted by startTime. + expect(entries.getEntriesByType('mark')).toEqual([mark2, mark1]); + + expect(callback.mock.calls[0][1]).toBe(observer); + }); + + // eslint-disable-next-line jest/no-disabled-tests + it.skip('provides the same performance entry references from mark/measure to all observers', () => { + const callback1 = jest.fn(); + const callback2 = jest.fn(); + const observer1 = new PerformanceObserver(callback1); + const observer2 = new PerformanceObserver(callback2); + observer1.observe({entryTypes: ['mark', 'measure']}); + observer2.observe({entryTypes: ['mark', 'measure']}); + + let mark; + let measure; + + Fantom.runTask(() => { + mark = performance.mark('mark', {startTime: 20}); + measure = performance.measure('measure', { + start: 25, + duration: 10, + }); + }); + + expect(callback1).toHaveBeenCalledTimes(1); + expect(callback2).toHaveBeenCalledTimes(1); + + const entries1: PerformanceObserverEntryList = callback1.mock.calls[0][0]; + const entries2: PerformanceObserverEntryList = callback2.mock.calls[0][0]; + + expect(entries1.getEntries()[0]).toBe(mark); + expect(entries2.getEntries()[0]).toBe(mark); + + expect(entries1.getEntries()[1]).toBe(measure); + expect(entries2.getEntries()[1]).toBe(measure); + }); +}); diff --git a/packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/UserTiming-itest.js similarity index 99% rename from packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js rename to packages/react-native/src/private/webapis/performance/__tests__/UserTiming-itest.js index 7d927ca4712..3a786cc058b 100644 --- a/packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js +++ b/packages/react-native/src/private/webapis/performance/__tests__/UserTiming-itest.js @@ -36,7 +36,7 @@ function toJSON(entries: PerformanceEntryList): Array { return entries.map(entry => entry.toJSON()); } -describe('Performance', () => { +describe('User Timing', () => { beforeEach(() => { performance.clearMarks(); performance.clearMeasures(); diff --git a/packages/react-native/src/private/webapis/performance/__tests__/UserTimingAPI-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/UserTimingAPI-itest.js deleted file mode 100644 index 52ef87e9232..00000000000 --- a/packages/react-native/src/private/webapis/performance/__tests__/UserTimingAPI-itest.js +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow strict-local - * @format - */ - -import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; - -import type Performance from 'react-native/src/private/webapis/performance/Performance'; - -import ensureInstance from '../../../__tests__/utilities/ensureInstance'; -import * as Fantom from '@react-native/fantom'; -import setUpPerformanceObserver from 'react-native/src/private/setup/setUpPerformanceObserver'; -import {PerformanceObserver} from 'react-native/src/private/webapis/performance/PerformanceObserver'; -import {PerformanceMark} from 'react-native/src/private/webapis/performance/UserTiming'; - -setUpPerformanceObserver(); - -declare var performance: Performance; - -describe('User Timing API', () => { - describe('performance.mark()', () => { - it('reports marks to observers', () => { - const callback = jest.fn(); - - const observer = new PerformanceObserver(callback); - observer.observe({type: 'mark'}); - - expect(callback).not.toHaveBeenCalled(); - - // const mark1Detail = {mark1: 'detail1'}; - performance.mark('mark1', { - startTime: 100, - // detail: mark1Detail, - }); - - // const mark2Detail = {mark2: 'detail2'}; - performance.mark('mark2', { - startTime: 200, - // detail: mark2Detail, - }); - - expect(callback).not.toHaveBeenCalled(); - - Fantom.runWorkLoop(); - - expect(callback).toHaveBeenCalledTimes(1); - - const entries = callback.mock.lastCall[0].getEntries(); - expect(entries.length).toBe(2); - - const mark1 = ensureInstance(entries[0], PerformanceMark); - const mark2 = ensureInstance(entries[1], PerformanceMark); - - expect(mark1.entryType).toBe('mark'); - expect(mark1.name).toBe('mark1'); - expect(mark1.startTime).toBe(100); - expect(mark1.duration).toBe(0); - // This doesn't work through PerformanceObserver yet - // expect(mark1.detail).toEqual(mark1Detail); - // expect(mark1.detail).not.toBe(mark1Detail); - - expect(mark2.entryType).toBe('mark'); - expect(mark2.name).toBe('mark2'); - expect(mark2.startTime).toBe(200); - expect(mark2.duration).toBe(0); - // This doesn't work through PerformanceObserver yet - // expect(mark2.detail).toEqual(mark2Detail); - // expect(mark2.detail).not.toBe(mark2Detail); - }); - }); -});