Skip to content

Commit cb59eb2

Browse files
rubennortefacebook-github-bot
authored andcommitted
Reorganize tests for PerformanceObserver and User Timing API (#52587)
Summary: Pull Request resolved: #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
1 parent 5050d0a commit cb59eb2

File tree

3 files changed

+110
-77
lines changed

3 files changed

+110
-77
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
12+
13+
import type Performance from '../Performance';
14+
import type {
15+
PerformanceObserver as PerformanceObserverT,
16+
PerformanceObserverEntryList,
17+
} from '../PerformanceObserver';
18+
19+
import setUpPerformanceObserver from '../../../setup/setUpPerformanceObserver';
20+
import * as Fantom from '@react-native/fantom';
21+
22+
setUpPerformanceObserver();
23+
24+
declare var performance: Performance;
25+
declare var PerformanceObserver: Class<PerformanceObserverT>;
26+
27+
describe('PerformanceObserver', () => {
28+
it('receives notifications for marks and measures', () => {
29+
const callback = jest.fn();
30+
const observer = new PerformanceObserver(callback);
31+
observer.observe({entryTypes: ['mark', 'measure']});
32+
33+
expect(callback).not.toHaveBeenCalled();
34+
35+
let mark1;
36+
let mark2;
37+
let measure1;
38+
let measure2;
39+
40+
Fantom.runTask(() => {
41+
mark1 = performance.mark('mark1', {startTime: 20});
42+
mark2 = performance.mark('mark2', {startTime: 10});
43+
measure1 = performance.measure('measure1', {
44+
start: 50,
45+
duration: 20,
46+
});
47+
measure2 = performance.measure('measure2', {
48+
start: 15,
49+
duration: 10,
50+
});
51+
52+
// The notification should be dispatched asynchronously.
53+
expect(callback).not.toHaveBeenCalled();
54+
55+
Fantom.scheduleTask(() => {
56+
// The notification should be dispatched with a low priority.
57+
expect(callback).not.toHaveBeenCalled();
58+
});
59+
});
60+
61+
expect(callback).toHaveBeenCalledTimes(1);
62+
63+
const entries: PerformanceObserverEntryList = callback.mock.calls[0][0];
64+
65+
// Sorted by startTime.
66+
expect(entries.getEntries()).toEqual([mark2, measure2, mark1, measure1]);
67+
68+
// Sorted by startTime.
69+
expect(entries.getEntriesByName('mark1')).toEqual([mark1]);
70+
71+
// Sorted by startTime.
72+
expect(entries.getEntriesByType('mark')).toEqual([mark2, mark1]);
73+
74+
expect(callback.mock.calls[0][1]).toBe(observer);
75+
});
76+
77+
// eslint-disable-next-line jest/no-disabled-tests
78+
it.skip('provides the same performance entry references from mark/measure to all observers', () => {
79+
const callback1 = jest.fn();
80+
const callback2 = jest.fn();
81+
const observer1 = new PerformanceObserver(callback1);
82+
const observer2 = new PerformanceObserver(callback2);
83+
observer1.observe({entryTypes: ['mark', 'measure']});
84+
observer2.observe({entryTypes: ['mark', 'measure']});
85+
86+
let mark;
87+
let measure;
88+
89+
Fantom.runTask(() => {
90+
mark = performance.mark('mark', {startTime: 20});
91+
measure = performance.measure('measure', {
92+
start: 25,
93+
duration: 10,
94+
});
95+
});
96+
97+
expect(callback1).toHaveBeenCalledTimes(1);
98+
expect(callback2).toHaveBeenCalledTimes(1);
99+
100+
const entries1: PerformanceObserverEntryList = callback1.mock.calls[0][0];
101+
const entries2: PerformanceObserverEntryList = callback2.mock.calls[0][0];
102+
103+
expect(entries1.getEntries()[0]).toBe(mark);
104+
expect(entries2.getEntries()[0]).toBe(mark);
105+
106+
expect(entries1.getEntries()[1]).toBe(measure);
107+
expect(entries2.getEntries()[1]).toBe(measure);
108+
});
109+
});

packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js renamed to packages/react-native/src/private/webapis/performance/__tests__/UserTiming-itest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function toJSON(entries: PerformanceEntryList): Array<PerformanceEntryJSON> {
3636
return entries.map(entry => entry.toJSON());
3737
}
3838

39-
describe('Performance', () => {
39+
describe('User Timing', () => {
4040
beforeEach(() => {
4141
performance.clearMarks();
4242
performance.clearMeasures();

packages/react-native/src/private/webapis/performance/__tests__/UserTimingAPI-itest.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)