|
| 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 | +}); |
0 commit comments