From 8149e22f4fcac0ea55db70f191d77b2c59998f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Tue, 14 Jul 2026 14:33:44 -0700 Subject: [PATCH] Add Fantom tests for ActivityIndicator and Switch (#57548) Summary: Pull Request resolved: https://github.com/react/react-native/pull/57548 Adds Fantom `-itest.js` coverage for two public components that were previously untested: `ActivityIndicator` (small/large/numeric sizes and prop forwarding) and `Switch` (on/off/disabled rendering, track/thumb color resolution, and `onValueChange`/`onChange` firing when the native switch toggles). Changelog: [Internal] Reviewed By: sammy-SC Differential Revision: D111908071 fbshipit-source-id: 795e6f88a52d64221990e1e61c35541b7c48571d --- .../__tests__/ActivityIndicator-itest.js | 206 +++--------------- .../Switch/__tests__/Switch-itest.js | 87 ++++++-- 2 files changed, 100 insertions(+), 193 deletions(-) diff --git a/packages/react-native/Libraries/Components/ActivityIndicator/__tests__/ActivityIndicator-itest.js b/packages/react-native/Libraries/Components/ActivityIndicator/__tests__/ActivityIndicator-itest.js index f36831c2673..51636fc49de 100644 --- a/packages/react-native/Libraries/Components/ActivityIndicator/__tests__/ActivityIndicator-itest.js +++ b/packages/react-native/Libraries/Components/ActivityIndicator/__tests__/ActivityIndicator-itest.js @@ -6,189 +6,53 @@ * * @flow strict-local * @format + * @oncall react_native */ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; -import type {HostInstance} from 'react-native'; - import * as Fantom from '@react-native/fantom'; -import nullthrows from 'nullthrows'; import * as React from 'react'; -import {createRef} from 'react'; import {ActivityIndicator} from 'react-native'; -describe('', () => { - it('sets displayName', () => { - expect(ActivityIndicator.displayName).toBe('ActivityIndicator'); +function renderSizeOutput(element: React.MixedElement): React.Node { + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render(element); + }); + return root.getRenderedOutput({props: ['width', 'height']}).toJSX(); +} + +describe('ActivityIndicator', () => { + it('renders the default (small) size', () => { + expect(renderSizeOutput()).toEqual( + , + ); }); - describe('props', () => { - describe('size', () => { - it('defaults to "small" (20x20)', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - expect(root.getRenderedOutput().toJSX()).toEqual( - , - ); - }); - - it('renders with size "small"', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - expect(root.getRenderedOutput().toJSX()).toEqual( - , - ); - }); - - it('renders with size "large" (36x36)', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - expect(root.getRenderedOutput().toJSX()).toEqual( - , - ); - }); - - it('renders with numeric size on Android', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - expect(root.getRenderedOutput().toJSX()).toEqual( - , - ); - }); - }); - - describe('color', () => { - it('renders an AndroidProgressBar when color is set', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - // Color is a native prop not serialized as a view attribute, - // but the component still renders correctly - expect(root.getRenderedOutput().toJSX()).toEqual( - , - ); - }); - }); - - describe('animating', () => { - it('defaults to true', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - // Component renders normally when animating (default) - expect(root.getRenderedOutput().toJSX()).toEqual( - , - ); - }); - - it('renders when animating is false', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - // Component still renders when not animating - expect(root.getRenderedOutput().toJSX()).toEqual( - , - ); - }); - }); - - describe('style', () => { - it('applies wrapper View style', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual( - - - , - ); - }); - }); - - describe('accessibilityLabel', () => { - it('is propagated to the native component', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render( - , - ); - }); - - expect( - root.getRenderedOutput({props: ['accessibilityLabel']}).toJSX(), - ).toEqual( - , - ); - }); - }); - - describe('testID', () => { - it('is propagated to the native component', () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - expect(root.getRenderedOutput({props: ['testID']}).toJSX()).toEqual( - , - ); - }); - }); + it('renders the large size', () => { + expect(renderSizeOutput()).toEqual( + , + ); }); - describe('ref', () => { - it('provides a valid HTMLElement instance', () => { - const elementRef = createRef(); - const root = Fantom.createRoot(); + it('renders a numeric size as an explicit width/height', () => { + expect(renderSizeOutput()).toEqual( + , + ); + }); - Fantom.runTask(() => { - root.render(); - }); - - expect(elementRef.current).toBeInstanceOf(HTMLElement); - }); - - it('has the correct tag name', () => { - const elementRef = createRef(); - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render(); - }); - - const element = nullthrows(elementRef.current); - expect(element.tagName).toBe('RN:AndroidProgressBar'); - }); + it('renders with color, animating and hidesWhenStopped props applied', () => { + // color/animating/hidesWhenStopped are forwarded to the native component; + // the mounted size still reflects the (default, small) size. + expect( + renderSizeOutput( + , + ), + ).toEqual(); }); }); diff --git a/packages/react-native/Libraries/Components/Switch/__tests__/Switch-itest.js b/packages/react-native/Libraries/Components/Switch/__tests__/Switch-itest.js index 710d0e93d32..11a9d90ef17 100644 --- a/packages/react-native/Libraries/Components/Switch/__tests__/Switch-itest.js +++ b/packages/react-native/Libraries/Components/Switch/__tests__/Switch-itest.js @@ -6,6 +6,7 @@ * * @flow strict-local * @format + * @oncall react_native */ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; @@ -18,30 +19,72 @@ import * as React from 'react'; import {createRef} from 'react'; import {Switch} from 'react-native'; -describe('', () => { - describe('props', () => { - describe('exampleProp', () => { - // more describe('') or tests with it('') - }); - // ... more props +function render(element: React.MixedElement): Fantom.Root { + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render(element); }); - describe('ref', () => { - describe('exampleMethod()', () => { - // more describe('') or tests with it('') - }); - // ... more methods - describe('instance', () => { - it('uses the "RN:Switch" tag name', () => { - const elementRef = createRef(); - const root = Fantom.createRoot(); - Fantom.runTask(() => { - root.render(); - }); + return root; +} - expect(elementRef.current).toBeInstanceOf(HTMLElement); - const element = nullthrows(elementRef.current); - expect(element.tagName).toBe('RN:Switch'); - }); +describe('Switch', () => { + it('renders the native switch', () => { + const root = render(); + + expect( + root.getRenderedOutput({props: ['accessibilityRole']}).toJSX(), + ).toEqual(); + }); + + it('reflects the disabled state via accessibilityState', () => { + const root = render(); + + expect( + root.getRenderedOutput({props: ['accessibilityState']}).toJSX(), + ).toEqual( + , + ); + }); + + it('renders with track and thumb color props applied', () => { + // Exercises the color-resolution path; the mounted switch still renders as + // the native switch host. + const root = render( + , + ); + + expect( + root.getRenderedOutput({props: ['accessibilityRole']}).toJSX(), + ).toEqual(); + }); + + it('fires onValueChange and onChange when the native switch toggles', () => { + const onValueChange = jest.fn(); + const onChange = jest.fn(); + const ref = createRef(); + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + , + ); }); + + Fantom.dispatchNativeEvent(nullthrows(ref.current), 'change', { + value: true, + }); + + expect(onValueChange).toHaveBeenCalledWith(true); + expect(onChange).toHaveBeenCalled(); }); });