/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow strict-local * @format */ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; import type {HostInstance} from 'react-native'; import ensureInstance from '../../../../src/private/__tests__/utilities/ensureInstance'; import * as Fantom from '@react-native/fantom'; import * as React from 'react'; import {createRef} from 'react'; import {View} from 'react-native'; import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement'; describe('', () => { describe('props', () => { describe('style', () => { describe('width and height style', () => { it('handles correct percentage-based dimensions', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect( root.getRenderedOutput({includeLayoutMetrics: true}).toJSX(), ).toEqual( , ); }); it('handles numeric values passed in as strings', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect( root.getRenderedOutput({includeLayoutMetrics: true}).toJSX(), ).toEqual( , ); }); it('handles invalid values, falling back to default', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect( root.getRenderedOutput({includeLayoutMetrics: true}).toJSX(), ).toEqual( , ); }); }); describe('margin style', () => { it('handles correct percentage-based values', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect( root.getRenderedOutput({includeLayoutMetrics: true}).toJSX(), ).toEqual( , ); }); it('handles numeric values passed in as strings', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect( root.getRenderedOutput({includeLayoutMetrics: true}).toJSX(), ).toEqual( , ); }); }); describe('transform style', () => { it('causes view to be unflattened', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['transform']}).toJSX(), ).toEqual(); }); [ [undefined, {x: -5, y: 0, width: 20, height: 10}], ['50% 50%', {x: -5, y: 0, width: 20, height: 10}], ['top left', {x: 0, y: 0, width: 20, height: 10}], ['right bottom', {x: -10, y: 0, width: 20, height: 10}], ].forEach(([transformOrigin, expectedBounds]) => { it(`applies transformOrigin correctly for ${String(transformOrigin)}`, () => { const root = Fantom.createRoot(); const viewRef = createRef(); Fantom.runTask(() => { root.render( , ); }); const viewElement = ensureInstance( viewRef.current, ReactNativeElement, ); const viewBounds = viewElement.getBoundingClientRect(); expect(viewBounds.x).toBe(expectedBounds.x); expect(viewBounds.y).toBe(expectedBounds.y); expect(viewBounds.width).toBe(expectedBounds.width); expect(viewBounds.height).toBe(expectedBounds.height); }); }); }); }); describe('pointerEvents', () => { it('auto does not propagate to the mounting layer, it is the default', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['pointerEvents']}).toJSX(), ).toEqual(); }); it('box-none propagates to the mounting layer, does not unflatten', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['pointerEvents']}).toJSX(), ).toEqual(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['pointerEvents']}).toJSX(), ).toEqual(null); }); it('box-only propagates to the mounting layer, does not unflatten', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['pointerEvents']}).toJSX(), ).toEqual(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['pointerEvents']}).toJSX(), ).toEqual(null); }); it('none propagates to the mounting layer, unflattens', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['pointerEvents']}).toJSX(), ).toEqual(); }); }); describe('accessibility', () => { describe('accessibilityActions', () => { it('is propagated to the mounting layer', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect( root.getRenderedOutput({props: ['accessibilityActions']}).toJSX(), ).toEqual( , ); }); it('does not unflatten view', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect( root.getRenderedOutput({props: ['accessibilityRole']}).toJSX(), ).toEqual(null); }); }); describe('accessibilityElementsHidden', () => { it("is an iOS-only prop and ignored by Fantom (Android)'s BaseViewConfig", () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect(root.getRenderedOutput().toJSX()).toEqual(); }); }); describe('aria-hidden', () => { it('is mapped to importantForAccessibility', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect(root.getRenderedOutput().toJSX()).toEqual( , ); }); }); describe('accessibilityHint', () => { it('is propagated to the mounting layer', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['accessibilityHint']}).toJSX(), ).toEqual(); }); it('does not unflatten view', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['accessibilityHint']}).toJSX(), ).toEqual(null); }); }); describe('accessibilityLabel', () => { it('is propagated to the mounting layer', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect( root.getRenderedOutput({props: ['accessibilityLabel']}).toJSX(), ).toEqual(); }); it('does not unflatten view', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['accessibilityLabel']}).toJSX(), ).toEqual(null); }); }); describe('accessibilityLiveRegion', () => { it('is propagated to the mounting layer', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect( root .getRenderedOutput({props: ['accessibilityLiveRegion']}) .toJSX(), ).toEqual(); }); it('does not unflatten view', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect( root .getRenderedOutput({props: ['accessibilityLiveRegion']}) .toJSX(), ).toEqual(null); }); }); describe('accessibilityRole', () => { it('is propagated to the mounting layer', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); expect( root.getRenderedOutput({props: ['accessibilityRole']}).toJSX(), ).toEqual(); }); it('does not unflatten view', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect( root.getRenderedOutput({props: ['accessibilityRole']}).toJSX(), ).toEqual(null); }); }); }); describe('accessible', () => { it('unflattens view', () => { const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect(root.getRenderedOutput({props: ['accessible']}).toJSX()).toEqual( null, ); Fantom.runTask(() => { root.render(); }); expect(root.getRenderedOutput({props: ['accessible']}).toJSX()).toEqual( , ); }); }); }); describe('ref', () => { it('is an element node', () => { const elementRef = createRef(); const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); expect(elementRef.current).toBeInstanceOf(ReactNativeElement); }); it('uses the "RN:View" tag name', () => { const elementRef = createRef(); const root = Fantom.createRoot(); Fantom.runTask(() => { root.render(); }); const element = ensureInstance(elementRef.current, ReactNativeElement); expect(element.tagName).toBe('RN:View'); }); }); });