|
| 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 {HighResTimeStampMock} from '@react-native/fantom/src/HighResTimeStampMock'; |
| 14 | +import type {HostInstance} from 'react-native/src/private/types/HostInstance'; |
| 15 | + |
| 16 | +import * as Fantom from '@react-native/fantom'; |
| 17 | +import * as React from 'react'; |
| 18 | +import {createRef} from 'react'; |
| 19 | +import {View} from 'react-native'; |
| 20 | + |
| 21 | +const INSETS = {top: 44, right: 0, bottom: 34, left: 0}; |
| 22 | + |
| 23 | +function renderObservingView(): {current: HostInstance | null} { |
| 24 | + const nodeRef = createRef<HostInstance>(); |
| 25 | + const root = Fantom.createRoot(); |
| 26 | + Fantom.runTask(() => { |
| 27 | + root.render( |
| 28 | + <View ref={nodeRef} experimental_onSafeAreaInsetsChange={() => {}} />, |
| 29 | + ); |
| 30 | + }); |
| 31 | + return nodeRef; |
| 32 | +} |
| 33 | + |
| 34 | +function dispatchInsetsChange(nodeRef: {current: HostInstance | null}) { |
| 35 | + Fantom.dispatchNativeEvent(nodeRef, 'safeAreaInsetsChange', { |
| 36 | + insets: INSETS, |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +describe('experimental_onSafeAreaInsetsChange warning', () => { |
| 41 | + const originalConsoleWarn = console.warn; |
| 42 | + let mockConsoleWarn: JestMockFn<ReadonlyArray<unknown>, void>; |
| 43 | + let mockClock: ?HighResTimeStampMock; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + mockConsoleWarn = jest.fn(); |
| 47 | + // $FlowFixMe[cannot-write] |
| 48 | + console.warn = mockConsoleWarn; |
| 49 | + mockClock = Fantom.installHighResTimeStampMock(); |
| 50 | + }); |
| 51 | + |
| 52 | + afterEach(() => { |
| 53 | + // $FlowFixMe[cannot-write] |
| 54 | + console.warn = originalConsoleWarn; |
| 55 | + mockClock?.uninstall(); |
| 56 | + mockClock = null; |
| 57 | + }); |
| 58 | + |
| 59 | + it('stays silent while the insets change at a plausible rate', () => { |
| 60 | + const nodeRef = renderObservingView(); |
| 61 | + |
| 62 | + // A rotation, a keyboard, a split view: a handful of changes, spread out. |
| 63 | + for (let i = 0; i < 20; i++) { |
| 64 | + dispatchInsetsChange(nodeRef); |
| 65 | + mockClock?.advanceTimeBy(200); |
| 66 | + } |
| 67 | + |
| 68 | + expect(mockConsoleWarn).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('warns once when a single view loops within the window', () => { |
| 72 | + const nodeRef = renderObservingView(); |
| 73 | + |
| 74 | + for (let i = 0; i < 11; i++) { |
| 75 | + dispatchInsetsChange(nodeRef); |
| 76 | + mockClock?.advanceTimeBy(16); |
| 77 | + } |
| 78 | + |
| 79 | + expect(mockConsoleWarn).toHaveBeenCalledTimes(1); |
| 80 | + expect(mockConsoleWarn.mock.lastCall[0]).toContain( |
| 81 | + '`experimental_onSafeAreaInsetsChange` fired more than 10 times in 1000ms', |
| 82 | + ); |
| 83 | + |
| 84 | + // The loop keeps running; the warning does not. |
| 85 | + for (let i = 0; i < 50; i++) { |
| 86 | + dispatchInsetsChange(nodeRef); |
| 87 | + mockClock?.advanceTimeBy(16); |
| 88 | + } |
| 89 | + |
| 90 | + expect(mockConsoleWarn).toHaveBeenCalledTimes(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('counts each view separately', () => { |
| 94 | + const nodeRefA = renderObservingView(); |
| 95 | + const nodeRefB = renderObservingView(); |
| 96 | + |
| 97 | + for (let i = 0; i < 10; i++) { |
| 98 | + dispatchInsetsChange(nodeRefA); |
| 99 | + dispatchInsetsChange(nodeRefB); |
| 100 | + mockClock?.advanceTimeBy(16); |
| 101 | + } |
| 102 | + |
| 103 | + expect(mockConsoleWarn).not.toHaveBeenCalled(); |
| 104 | + |
| 105 | + dispatchInsetsChange(nodeRefA); |
| 106 | + |
| 107 | + expect(mockConsoleWarn).toHaveBeenCalledTimes(1); |
| 108 | + }); |
| 109 | + |
| 110 | + it('still delivers the event to the handler', () => { |
| 111 | + const nodeRef = createRef<HostInstance>(); |
| 112 | + const onSafeAreaInsetsChange = jest.fn(); |
| 113 | + const root = Fantom.createRoot(); |
| 114 | + Fantom.runTask(() => { |
| 115 | + root.render( |
| 116 | + <View |
| 117 | + ref={nodeRef} |
| 118 | + experimental_onSafeAreaInsetsChange={event => { |
| 119 | + onSafeAreaInsetsChange(event.nativeEvent); |
| 120 | + }} |
| 121 | + />, |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + dispatchInsetsChange(nodeRef); |
| 126 | + |
| 127 | + expect(onSafeAreaInsetsChange).toHaveBeenCalledTimes(1); |
| 128 | + expect(onSafeAreaInsetsChange.mock.lastCall[0].insets).toEqual(INSETS); |
| 129 | + }); |
| 130 | +}); |
0 commit comments