|
| 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 | + * @fantom_flags enableFlushSync:* |
| 8 | + * @flow strict-local |
| 9 | + * @format |
| 10 | + */ |
| 11 | + |
| 12 | +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
| 13 | + |
| 14 | +import type {HostInstance} from 'react-native'; |
| 15 | + |
| 16 | +import * as ReactNativeFeatureFlags from '../../../src/private/featureflags/ReactNativeFeatureFlags'; |
| 17 | +import * as Fantom from '@react-native/fantom'; |
| 18 | +import nullthrows from 'nullthrows'; |
| 19 | +import * as React from 'react'; |
| 20 | +import {createRef, startTransition, useLayoutEffect, useState} from 'react'; |
| 21 | +import {View, unstable_flushSync as maybeFlushSync} from 'react-native'; |
| 22 | + |
| 23 | +let log: Array<string> = []; |
| 24 | +let setN: (n: number) => void = () => {}; |
| 25 | +const counterRef = createRef<HostInstance>(); |
| 26 | + |
| 27 | +type CounterProps = {onPointerUp?: () => void}; |
| 28 | + |
| 29 | +function Counter({onPointerUp}: CounterProps): React.Node { |
| 30 | + const [n, _setN] = useState(0); |
| 31 | + setN = _setN; |
| 32 | + log.push(`render:${n}`); |
| 33 | + useLayoutEffect(() => { |
| 34 | + log.push(`layout:${n}`); |
| 35 | + }, [n]); |
| 36 | + return <View ref={counterRef} onPointerUp={onPointerUp} />; |
| 37 | +} |
| 38 | + |
| 39 | +function renderCounter(props?: CounterProps): Fantom.Root { |
| 40 | + const root = Fantom.createRoot(); |
| 41 | + Fantom.runTask(() => { |
| 42 | + root.render(<Counter {...props} />); |
| 43 | + }); |
| 44 | + expect(log).toEqual(['render:0', 'layout:0']); |
| 45 | + log = []; |
| 46 | + return root; |
| 47 | +} |
| 48 | + |
| 49 | +beforeEach(() => { |
| 50 | + log = []; |
| 51 | +}); |
| 52 | + |
| 53 | +(ReactNativeFeatureFlags.enableFlushSync() ? it.skip : it)( |
| 54 | + 'is not exported when enableFlushSync is disabled', |
| 55 | + () => { |
| 56 | + expect(maybeFlushSync).toBe(undefined); |
| 57 | + }, |
| 58 | +); |
| 59 | + |
| 60 | +(ReactNativeFeatureFlags.enableFlushSync() ? describe : describe.skip)( |
| 61 | + 'flushSync', |
| 62 | + () => { |
| 63 | + let flushSync: NonNullable<typeof maybeFlushSync>; |
| 64 | + |
| 65 | + beforeEach(() => { |
| 66 | + flushSync = nullthrows(maybeFlushSync); |
| 67 | + }); |
| 68 | + |
| 69 | + it('is exported from react-native', () => { |
| 70 | + expect(typeof maybeFlushSync).toBe('function'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('flushes updates synchronously before returning', () => { |
| 74 | + renderCounter(); |
| 75 | + |
| 76 | + Fantom.runTask(() => { |
| 77 | + flushSync(() => setN(1)); |
| 78 | + log.push('after'); |
| 79 | + }); |
| 80 | + |
| 81 | + expect(log).toEqual(['render:1', 'layout:1', 'after']); |
| 82 | + }); |
| 83 | + |
| 84 | + it('without flushSync, updates are deferred', () => { |
| 85 | + renderCounter(); |
| 86 | + |
| 87 | + Fantom.runTask(() => { |
| 88 | + setN(1); |
| 89 | + log.push('after'); |
| 90 | + }); |
| 91 | + |
| 92 | + expect(log).toEqual(['after', 'render:1', 'layout:1']); |
| 93 | + }); |
| 94 | + |
| 95 | + it('opts out of transitions', () => { |
| 96 | + renderCounter(); |
| 97 | + |
| 98 | + Fantom.runTask(() => { |
| 99 | + startTransition(() => { |
| 100 | + flushSync(() => setN(1)); |
| 101 | + log.push('after'); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + expect(log).toEqual(['render:1', 'layout:1', 'after']); |
| 106 | + }); |
| 107 | + |
| 108 | + it("returns the callback's return value", () => { |
| 109 | + expect(flushSync(() => 42)).toBe(42); |
| 110 | + expect(flushSync()).toBe(undefined); |
| 111 | + }); |
| 112 | + |
| 113 | + it('propagates errors from the callback and still flushes updates', () => { |
| 114 | + renderCounter(); |
| 115 | + |
| 116 | + Fantom.runTask(() => { |
| 117 | + expect(() => |
| 118 | + flushSync(() => { |
| 119 | + setN(1); |
| 120 | + throw new Error('boom'); |
| 121 | + }), |
| 122 | + ).toThrow('boom'); |
| 123 | + log.push('after'); |
| 124 | + }); |
| 125 | + |
| 126 | + expect(log).toEqual(['render:1', 'layout:1', 'after']); |
| 127 | + }); |
| 128 | + |
| 129 | + it('flushes pending sync work when called without a callback', () => { |
| 130 | + renderCounter({ |
| 131 | + onPointerUp: () => { |
| 132 | + setN(1); |
| 133 | + flushSync(); |
| 134 | + log.push('after'); |
| 135 | + }, |
| 136 | + }); |
| 137 | + |
| 138 | + Fantom.dispatchNativeEvent( |
| 139 | + nullthrows(counterRef.current), |
| 140 | + 'onPointerUp', |
| 141 | + {x: 0, y: 0}, |
| 142 | + {category: Fantom.NativeEventCategory.Discrete}, |
| 143 | + ); |
| 144 | + |
| 145 | + expect(log).toEqual(['render:1', 'layout:1', 'after']); |
| 146 | + }); |
| 147 | + |
| 148 | + it('warns and defers the update when called during commit', () => { |
| 149 | + const originalConsoleError = console.error; |
| 150 | + const consoleErrorSpy = jest.fn(); |
| 151 | + // $FlowFixMe[cannot-write] |
| 152 | + console.error = consoleErrorSpy; |
| 153 | + try { |
| 154 | + let didFlush = false; |
| 155 | + function App(): React.Node { |
| 156 | + useLayoutEffect(() => { |
| 157 | + if (!didFlush) { |
| 158 | + didFlush = true; |
| 159 | + flushSync(() => setN(1)); |
| 160 | + log.push('after'); |
| 161 | + } |
| 162 | + }); |
| 163 | + return <Counter />; |
| 164 | + } |
| 165 | + |
| 166 | + const root = Fantom.createRoot(); |
| 167 | + Fantom.runTask(() => { |
| 168 | + root.render(<App />); |
| 169 | + }); |
| 170 | + |
| 171 | + expect(log).toEqual([ |
| 172 | + 'render:0', |
| 173 | + 'layout:0', |
| 174 | + 'after', |
| 175 | + 'render:1', |
| 176 | + 'layout:1', |
| 177 | + ]); |
| 178 | + if (__DEV__) { |
| 179 | + expect(consoleErrorSpy).toHaveBeenCalledTimes(1); |
| 180 | + expect(consoleErrorSpy.mock.calls[0][0]).toBe( |
| 181 | + 'flushSync was called from inside a lifecycle method. React ' + |
| 182 | + 'cannot flush when React is already rendering. Consider moving ' + |
| 183 | + 'this call to a scheduler task or micro task.', |
| 184 | + ); |
| 185 | + } else { |
| 186 | + expect(consoleErrorSpy).not.toHaveBeenCalled(); |
| 187 | + } |
| 188 | + } finally { |
| 189 | + // $FlowFixMe[cannot-write] |
| 190 | + console.error = originalConsoleError; |
| 191 | + } |
| 192 | + }); |
| 193 | + }, |
| 194 | +); |
0 commit comments