|
| 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 | +/* global console: writable */ |
| 12 | + |
| 13 | +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
| 14 | + |
| 15 | +import type {Root, RootConfig} from '@react-native/fantom'; |
| 16 | + |
| 17 | +import * as Fantom from '@react-native/fantom'; |
| 18 | +import * as React from 'react'; |
| 19 | +import {Dimensions, PixelRatio, View, useWindowDimensions} from 'react-native'; |
| 20 | + |
| 21 | +let roots: Array<Root>; |
| 22 | +let originalConsole: typeof console; |
| 23 | +let warn: JestMockFn<Array<unknown>, void>; |
| 24 | + |
| 25 | +beforeEach(() => { |
| 26 | + roots = []; |
| 27 | + originalConsole = console; |
| 28 | + warn = jest.fn(); |
| 29 | + console = Object.create(originalConsole, {warn: {value: warn}}); |
| 30 | +}); |
| 31 | + |
| 32 | +afterEach(() => { |
| 33 | + for (const root of roots) { |
| 34 | + Fantom.runTask(() => root.render(<></>)); |
| 35 | + root.destroy(); |
| 36 | + } |
| 37 | + console = originalConsole; |
| 38 | +}); |
| 39 | + |
| 40 | +function createRoot(config?: RootConfig): Root { |
| 41 | + const root = Fantom.createRoot(config); |
| 42 | + roots.push(root); |
| 43 | + return root; |
| 44 | +} |
| 45 | + |
| 46 | +component WindowMetrics() { |
| 47 | + const {width, height, scale, fontScale} = useWindowDimensions(); |
| 48 | + return <View testID={`${width}x${height}@${scale}:${fontScale}`} />; |
| 49 | +} |
| 50 | + |
| 51 | +it('uses root defaults before any root is created', () => { |
| 52 | + for (const dimension of ['window', 'screen']) { |
| 53 | + const metrics = Dimensions.get(dimension); |
| 54 | + expect(metrics.width).toBe(390); |
| 55 | + expect(metrics.height).toBe(844); |
| 56 | + expect(metrics.scale).toBe(3); |
| 57 | + expect(metrics.fontScale).toBe(1); |
| 58 | + } |
| 59 | + expect(PixelRatio.get()).toBe(3); |
| 60 | + expect(PixelRatio.getFontScale()).toBe(1); |
| 61 | +}); |
| 62 | + |
| 63 | +it('sets default window and screen metrics', () => { |
| 64 | + createRoot(); |
| 65 | + for (const dimension of ['window', 'screen']) { |
| 66 | + const metrics = Dimensions.get(dimension); |
| 67 | + expect(metrics.width).toBe(390); |
| 68 | + expect(metrics.height).toBe(844); |
| 69 | + expect(metrics.scale).toBe(3); |
| 70 | + expect(metrics.fontScale).toBe(1); |
| 71 | + } |
| 72 | +}); |
| 73 | + |
| 74 | +it('sets custom viewport, pixel ratio, and font scale', () => { |
| 75 | + createRoot({ |
| 76 | + viewportWidth: 640, |
| 77 | + viewportHeight: 480, |
| 78 | + devicePixelRatio: 2, |
| 79 | + fontScale: 1.5, |
| 80 | + }); |
| 81 | + for (const dimension of ['window', 'screen']) { |
| 82 | + const metrics = Dimensions.get(dimension); |
| 83 | + expect(metrics.width).toBe(640); |
| 84 | + expect(metrics.height).toBe(480); |
| 85 | + expect(metrics.scale).toBe(2); |
| 86 | + expect(metrics.fontScale).toBe(1.5); |
| 87 | + } |
| 88 | + expect(PixelRatio.get()).toBe(2); |
| 89 | + expect(PixelRatio.getFontScale()).toBe(1.5); |
| 90 | +}); |
| 91 | + |
| 92 | +it('does not warn for matching metrics, including different viewport offsets', () => { |
| 93 | + createRoot(); |
| 94 | + createRoot({viewportOffsetX: 20, viewportOffsetY: 90}); |
| 95 | + expect(warn).not.toHaveBeenCalled(); |
| 96 | +}); |
| 97 | + |
| 98 | +const conflictingConfigs: Array<[string, RootConfig]> = [ |
| 99 | + ['width', {viewportWidth: 402}], |
| 100 | + ['height', {viewportHeight: 874}], |
| 101 | + ['pixel ratio', {devicePixelRatio: 2}], |
| 102 | + ['font scale', {fontScale: 1.5}], |
| 103 | +]; |
| 104 | + |
| 105 | +for (const [name, config] of conflictingConfigs) { |
| 106 | + it(`warns and applies new metrics when an active root has a different ${name}`, () => { |
| 107 | + createRoot(); |
| 108 | + createRoot(config); |
| 109 | + expect(warn).toHaveBeenCalledTimes(1); |
| 110 | + expect(warn.mock.calls[0][0]).toContain('another active root'); |
| 111 | + const metrics = Dimensions.get('window'); |
| 112 | + expect(metrics.width).toBe(config.viewportWidth ?? 390); |
| 113 | + expect(metrics.height).toBe(config.viewportHeight ?? 844); |
| 114 | + expect(metrics.scale).toBe(config.devicePixelRatio ?? 3); |
| 115 | + expect(metrics.fontScale).toBe(config.fontScale ?? 1); |
| 116 | + }); |
| 117 | +} |
| 118 | + |
| 119 | +it('checks all active roots, including one that differs from the latest root', () => { |
| 120 | + createRoot(); |
| 121 | + createRoot({viewportWidth: 640}); |
| 122 | + createRoot({viewportWidth: 640}); |
| 123 | + expect(warn).toHaveBeenCalledTimes(2); |
| 124 | + expect(Dimensions.get('window').width).toBe(640); |
| 125 | +}); |
| 126 | + |
| 127 | +it('does not warn for a destroyed root', () => { |
| 128 | + const first = createRoot(); |
| 129 | + first.destroy(); |
| 130 | + roots.shift(); |
| 131 | + createRoot({viewportWidth: 640}); |
| 132 | + expect(warn).not.toHaveBeenCalled(); |
| 133 | +}); |
| 134 | + |
| 135 | +it('restores the latest surviving root and then defaults when all roots are destroyed', () => { |
| 136 | + const first = createRoot(); |
| 137 | + const second = createRoot({ |
| 138 | + viewportWidth: 640, |
| 139 | + viewportHeight: 480, |
| 140 | + devicePixelRatio: 2, |
| 141 | + fontScale: 1.5, |
| 142 | + }); |
| 143 | + const third = createRoot({viewportWidth: 900}); |
| 144 | + third.destroy(); |
| 145 | + roots.pop(); |
| 146 | + for (const dimension of ['window', 'screen']) { |
| 147 | + const metrics = Dimensions.get(dimension); |
| 148 | + expect(metrics.width).toBe(640); |
| 149 | + expect(metrics.height).toBe(480); |
| 150 | + expect(metrics.scale).toBe(2); |
| 151 | + expect(metrics.fontScale).toBe(1.5); |
| 152 | + } |
| 153 | + first.destroy(); |
| 154 | + roots.shift(); |
| 155 | + for (const dimension of ['window', 'screen']) { |
| 156 | + const metrics = Dimensions.get(dimension); |
| 157 | + expect(metrics.width).toBe(640); |
| 158 | + expect(metrics.height).toBe(480); |
| 159 | + expect(metrics.scale).toBe(2); |
| 160 | + expect(metrics.fontScale).toBe(1.5); |
| 161 | + } |
| 162 | + second.destroy(); |
| 163 | + roots.pop(); |
| 164 | + for (const dimension of ['window', 'screen']) { |
| 165 | + const metrics = Dimensions.get(dimension); |
| 166 | + expect(metrics.width).toBe(390); |
| 167 | + expect(metrics.height).toBe(844); |
| 168 | + expect(metrics.scale).toBe(3); |
| 169 | + expect(metrics.fontScale).toBe(1); |
| 170 | + } |
| 171 | + expect(PixelRatio.get()).toBe(3); |
| 172 | + expect(PixelRatio.getFontScale()).toBe(1); |
| 173 | +}); |
| 174 | + |
| 175 | +it('notifies mounted components when another root changes the metrics', () => { |
| 176 | + const first = createRoot(); |
| 177 | + Fantom.runTask(() => first.render(<WindowMetrics />)); |
| 178 | + expect(first.getRenderedOutput({props: ['testID']}).toJSX()).toEqual( |
| 179 | + <rn-view testID="390x844@3:1" />, |
| 180 | + ); |
| 181 | + const second = createRoot({ |
| 182 | + viewportWidth: 640, |
| 183 | + viewportHeight: 480, |
| 184 | + devicePixelRatio: 2, |
| 185 | + fontScale: 1.5, |
| 186 | + }); |
| 187 | + Fantom.runTask(() => second.render(<WindowMetrics />)); |
| 188 | + expect(warn).toHaveBeenCalledTimes(1); |
| 189 | + for (const root of [first, second]) { |
| 190 | + expect(root.getRenderedOutput({props: ['testID']}).toJSX()).toEqual( |
| 191 | + <rn-view testID="640x480@2:1.5" />, |
| 192 | + ); |
| 193 | + } |
| 194 | + Fantom.runTask(() => second.render(<></>)); |
| 195 | + second.destroy(); |
| 196 | + roots.pop(); |
| 197 | + Fantom.runWorkLoop(); |
| 198 | + expect(first.getRenderedOutput({props: ['testID']}).toJSX()).toEqual( |
| 199 | + <rn-view testID="390x844@3:1" />, |
| 200 | + ); |
| 201 | +}); |
0 commit comments