diff --git a/packages/react-native/Libraries/Components/Touchable/__tests__/TouchableHighlight-itest.js b/packages/react-native/Libraries/Components/Touchable/__tests__/TouchableHighlight-itest.js new file mode 100644 index 000000000000..5073c21afe54 --- /dev/null +++ b/packages/react-native/Libraries/Components/Touchable/__tests__/TouchableHighlight-itest.js @@ -0,0 +1,246 @@ +/** + * 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 + * @oncall react_native + */ + +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; + +import type {AccessibilityProps, HostInstance} from 'react-native'; + +import * as Fantom from '@react-native/fantom'; +import * as React from 'react'; +import {createRef} from 'react'; +import {Text, TouchableHighlight, View} from 'react-native'; +import accessibilityPropsSuite from 'react-native/src/private/__tests__/utilities/accessibilityPropsSuite'; +import ensureInstance from 'react-native/src/private/__tests__/utilities/ensureInstance'; +import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement'; + +describe('', () => { + describe('props', () => { + describe('rendering', () => { + it('renders as a view with accessible="true"', () => { + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + expect(root.getRenderedOutput({props: ['accessible']}).toJSX()).toEqual( + , + ); + }); + }); + + component ComponentWithAccessibilityProps(...props: AccessibilityProps) { + return ( + + Touchable + + ); + } + + accessibilityPropsSuite(ComponentWithAccessibilityProps); + + describe('style', () => { + it('applies style props', () => { + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + expect( + root + .getRenderedOutput({ + props: ['width', 'height', 'backgroundColor'], + }) + .toJSX(), + ).toEqual( + , + ); + }); + }); + + describe('activeOpacity', () => { + it('does not render explicit opacity when using default', () => { + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual( + , + ); + }); + + it('renders with custom style opacity', () => { + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual( + , + ); + }); + }); + + describe('onPress', () => { + it('triggers callback when the element is pressed', () => { + const elementRef = createRef(); + const onPressCallback = jest.fn(); + + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + const element = ensureInstance(elementRef.current, ReactNativeElement); + Fantom.dispatchNativeEvent(element, 'click'); + + expect(onPressCallback).toHaveBeenCalledTimes(1); + }); + }); + + describe('disabled', () => { + it('cannot be pressed when disabled', () => { + const elementRef = createRef(); + const onPressCallback = jest.fn(); + + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + const element = ensureInstance(elementRef.current, ReactNativeElement); + Fantom.dispatchNativeEvent(element, 'click'); + + expect(onPressCallback).toHaveBeenCalledTimes(0); + }); + + it('sets accessibilityState disabled to true', () => { + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + expect( + root.getRenderedOutput({props: ['accessibilityState']}).toJSX(), + ).toEqual( + , + ); + }); + }); + + describe('children', () => { + it('renders children inside the touchable', () => { + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + Press me + , + ); + }); + + expect(root.getRenderedOutput({props: ['accessible']}).toJSX()).toEqual( + + Press me + , + ); + }); + }); + }); + + describe('ref', () => { + describe('instance', () => { + 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'); + }); + }); + }); +}); diff --git a/packages/react-native/Libraries/Components/Touchable/__tests__/TouchableHighlight-test.js b/packages/react-native/Libraries/Components/Touchable/__tests__/TouchableHighlight-test.js deleted file mode 100644 index 338e22cdd3a2..000000000000 --- a/packages/react-native/Libraries/Components/Touchable/__tests__/TouchableHighlight-test.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * 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 - */ - -'use strict'; - -import Text from '../../../Text/Text'; -import View from '../../View/View'; -import TouchableHighlight from '../TouchableHighlight'; -import * as React from 'react'; - -const render = require('@react-native/jest-preset/jest/renderer'); - -describe('TouchableHighlight', () => { - it('renders correctly', async () => { - const instance = await render.create( - - Touchable - , - ); - - expect(instance.toJSON()).toMatchSnapshot(); - }); - - it('has displayName', () => { - expect(TouchableHighlight.displayName).toEqual('TouchableHighlight'); - }); -}); - -describe('TouchableHighlight with disabled state', () => { - it('should be disabled when disabled is true', async () => { - expect( - await render.create( - - - , - ), - ).toMatchSnapshot(); - }); - - it('should be disabled when disabled is true and accessibilityState is empty', async () => { - expect( - await render.create( - - - , - ), - ).toMatchSnapshot(); - }); - - it('should keep accessibilityState when disabled is true', async () => { - expect( - await render.create( - - - , - ), - ).toMatchSnapshot(); - }); - - it('should overwrite accessibilityState with value of disabled prop', async () => { - expect( - await render.create( - - - , - ), - ).toMatchSnapshot(); - }); - - it('should disable button when accessibilityState is disabled', async () => { - expect( - await render.create( - - - , - ), - ).toMatchSnapshot(); - }); -}); diff --git a/packages/react-native/Libraries/Components/Touchable/__tests__/__snapshots__/TouchableHighlight-test.js.snap b/packages/react-native/Libraries/Components/Touchable/__tests__/__snapshots__/TouchableHighlight-test.js.snap deleted file mode 100644 index 35c845e97493..000000000000 --- a/packages/react-native/Libraries/Components/Touchable/__tests__/__snapshots__/TouchableHighlight-test.js.snap +++ /dev/null @@ -1,174 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`TouchableHighlight renders correctly 1`] = ` - - - Touchable - - -`; - -exports[`TouchableHighlight with disabled state should be disabled when disabled is true 1`] = ` - - - -`; - -exports[`TouchableHighlight with disabled state should be disabled when disabled is true and accessibilityState is empty 1`] = ` - - - -`; - -exports[`TouchableHighlight with disabled state should disable button when accessibilityState is disabled 1`] = ` - - - -`; - -exports[`TouchableHighlight with disabled state should keep accessibilityState when disabled is true 1`] = ` - - - -`; - -exports[`TouchableHighlight with disabled state should overwrite accessibilityState with value of disabled prop 1`] = ` - - - -`;