Files
react-native/Libraries/WebPerformance/__mocks__/NativePerformance.js
T
Ruslan Shestopalyuk dc7941d732 Shim implementation for NativePerformance on JS side
Summary:
Changelog: [Internal]

Implements a shim for the NativePerformance TurboModule, which can be used as a mock to unit test the JS side of WebPerformance functionality (in particular, the higher level API logic of `Performance` and `PerformanceObserver` implementations in JS) without need to have the native NativePerformance implementation available (which would otherwise require running some heavy weight e2e tests).

Reviewed By: rubennorte

Differential Revision: D43985454

fbshipit-source-id: da10387c1d70cb0300c077972d3925bc2fad4f5e
2023-03-14 04:48:02 -07:00

66 lines
1.6 KiB
JavaScript

/**
* 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
* @format
*/
import type {
NativeMemoryInfo,
ReactNativeStartupTiming,
Spec as NativePerformance,
} from '../NativePerformance';
import NativePerformanceObserver from '../NativePerformanceObserver';
import {RawPerformanceEntryTypeValues} from '../RawPerformanceEntry';
const marks: Map<string, number> = new Map();
const NativePerformanceMock: NativePerformance = {
mark: (name: string, startTime: number, duration: number): void => {
NativePerformanceObserver?.logRawEntry({
name,
entryType: RawPerformanceEntryTypeValues.MARK,
startTime,
duration,
});
marks.set(name, startTime);
},
measure: (
name: string,
startTime: number,
endTime: number,
duration?: number,
startMark?: string,
endMark?: string,
): void => {
const start = startMark != null ? marks.get(startMark) ?? 0 : startTime;
const end = endMark != null ? marks.get(endMark) ?? 0 : endTime;
NativePerformanceObserver?.logRawEntry({
name,
entryType: RawPerformanceEntryTypeValues.MEASURE,
startTime: start,
duration: duration ?? (end ? end - start : 0),
});
},
getSimpleMemoryInfo: (): NativeMemoryInfo => {
return {};
},
getReactNativeStartupTiming: (): ReactNativeStartupTiming => {
return {
startTime: 0,
endTime: 0,
executeJavaScriptBundleEntryPointStart: 0,
executeJavaScriptBundleEntryPointEnd: 0,
};
},
};
export default NativePerformanceMock;