Reorganize tests for PerformanceObserver and User Timing API (#52587)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/52587

Changelog: [internal]

This just re-organizes the tests for the `Performance` API and `PerformanceObserver` as the previous organization didn't make much sense. Now it's a test for `PerformanceObserver` and another for the User Timing API.

Reviewed By: huntie

Differential Revision: D78193070

fbshipit-source-id: f15524bf07d2dc9edc155214279ce3af705cde67
This commit is contained in:
Rubén Norte authored and Facebook GitHub Bot committed 2025-07-16 02:48:43 -07:00
1 parent 5050d0a89e
commit cb59eb2b57
3 files changed
+110 -77

No files matched your search

@@ -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<PerformanceObserverT>;
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);
});
});
@@ -36,7 +36,7 @@ function toJSON(entries: PerformanceEntryList): Array<PerformanceEntryJSON> {
return entries.map(entry => entry.toJSON());
}
describe('Performance', () => {
describe('User Timing', () => {
beforeEach(() => {
performance.clearMarks();
performance.clearMeasures();
@@ -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);
});
});
});