|
| 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 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
| 13 | + |
| 14 | +import type {AccessibilityProps, HostInstance} from 'react-native'; |
| 15 | + |
| 16 | +import * as Fantom from '@react-native/fantom'; |
| 17 | +import * as React from 'react'; |
| 18 | +import {createRef} from 'react'; |
| 19 | +import {Text, TouchableHighlight, View} from 'react-native'; |
| 20 | +import accessibilityPropsSuite from 'react-native/src/private/__tests__/utilities/accessibilityPropsSuite'; |
| 21 | +import ensureInstance from 'react-native/src/private/__tests__/utilities/ensureInstance'; |
| 22 | +import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement'; |
| 23 | + |
| 24 | +describe('<TouchableHighlight>', () => { |
| 25 | + describe('props', () => { |
| 26 | + describe('rendering', () => { |
| 27 | + it('renders as a view with accessible="true"', () => { |
| 28 | + const root = Fantom.createRoot(); |
| 29 | + |
| 30 | + Fantom.runTask(() => { |
| 31 | + root.render( |
| 32 | + <TouchableHighlight> |
| 33 | + <View /> |
| 34 | + </TouchableHighlight>, |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + expect(root.getRenderedOutput({props: ['accessible']}).toJSX()).toEqual( |
| 39 | + <rn-view accessible="true" />, |
| 40 | + ); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + component ComponentWithAccessibilityProps(...props: AccessibilityProps) { |
| 45 | + return ( |
| 46 | + <TouchableHighlight {...props}> |
| 47 | + <Text>Touchable</Text> |
| 48 | + </TouchableHighlight> |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + accessibilityPropsSuite(ComponentWithAccessibilityProps); |
| 53 | + |
| 54 | + describe('style', () => { |
| 55 | + it('applies style props', () => { |
| 56 | + const root = Fantom.createRoot(); |
| 57 | + |
| 58 | + Fantom.runTask(() => { |
| 59 | + root.render( |
| 60 | + <TouchableHighlight |
| 61 | + style={{ |
| 62 | + width: 100, |
| 63 | + height: 50, |
| 64 | + backgroundColor: 'blue', |
| 65 | + }}> |
| 66 | + <View /> |
| 67 | + </TouchableHighlight>, |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + expect( |
| 72 | + root |
| 73 | + .getRenderedOutput({ |
| 74 | + props: ['width', 'height', 'backgroundColor'], |
| 75 | + }) |
| 76 | + .toJSX(), |
| 77 | + ).toEqual( |
| 78 | + <rn-view |
| 79 | + backgroundColor="rgba(0, 0, 255, 1)" |
| 80 | + height="50.000000" |
| 81 | + width="100.000000" |
| 82 | + />, |
| 83 | + ); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('activeOpacity', () => { |
| 88 | + it('does not render explicit opacity when using default', () => { |
| 89 | + const root = Fantom.createRoot(); |
| 90 | + |
| 91 | + Fantom.runTask(() => { |
| 92 | + root.render( |
| 93 | + <TouchableHighlight> |
| 94 | + <View /> |
| 95 | + </TouchableHighlight>, |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual( |
| 100 | + <rn-view />, |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('renders with custom style opacity', () => { |
| 105 | + const root = Fantom.createRoot(); |
| 106 | + |
| 107 | + Fantom.runTask(() => { |
| 108 | + root.render( |
| 109 | + <TouchableHighlight style={{opacity: 0.5}}> |
| 110 | + <View /> |
| 111 | + </TouchableHighlight>, |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual( |
| 116 | + <rn-view opacity="0.5" />, |
| 117 | + ); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('onPress', () => { |
| 122 | + it('triggers callback when the element is pressed', () => { |
| 123 | + const elementRef = createRef<HostInstance>(); |
| 124 | + const onPressCallback = jest.fn(); |
| 125 | + |
| 126 | + const root = Fantom.createRoot(); |
| 127 | + |
| 128 | + Fantom.runTask(() => { |
| 129 | + root.render( |
| 130 | + <TouchableHighlight |
| 131 | + ref={elementRef} |
| 132 | + onPress={onPressCallback} |
| 133 | + style={{height: 100}}> |
| 134 | + <View /> |
| 135 | + </TouchableHighlight>, |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + const element = ensureInstance(elementRef.current, ReactNativeElement); |
| 140 | + Fantom.dispatchNativeEvent(element, 'click'); |
| 141 | + |
| 142 | + expect(onPressCallback).toHaveBeenCalledTimes(1); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('disabled', () => { |
| 147 | + it('cannot be pressed when disabled', () => { |
| 148 | + const elementRef = createRef<HostInstance>(); |
| 149 | + const onPressCallback = jest.fn(); |
| 150 | + |
| 151 | + const root = Fantom.createRoot(); |
| 152 | + |
| 153 | + Fantom.runTask(() => { |
| 154 | + root.render( |
| 155 | + <TouchableHighlight |
| 156 | + ref={elementRef} |
| 157 | + onPress={onPressCallback} |
| 158 | + disabled={true}> |
| 159 | + <View /> |
| 160 | + </TouchableHighlight>, |
| 161 | + ); |
| 162 | + }); |
| 163 | + |
| 164 | + const element = ensureInstance(elementRef.current, ReactNativeElement); |
| 165 | + Fantom.dispatchNativeEvent(element, 'click'); |
| 166 | + |
| 167 | + expect(onPressCallback).toHaveBeenCalledTimes(0); |
| 168 | + }); |
| 169 | + |
| 170 | + it('sets accessibilityState disabled to true', () => { |
| 171 | + const root = Fantom.createRoot(); |
| 172 | + |
| 173 | + Fantom.runTask(() => { |
| 174 | + root.render( |
| 175 | + <TouchableHighlight disabled={true}> |
| 176 | + <View /> |
| 177 | + </TouchableHighlight>, |
| 178 | + ); |
| 179 | + }); |
| 180 | + |
| 181 | + expect( |
| 182 | + root.getRenderedOutput({props: ['accessibilityState']}).toJSX(), |
| 183 | + ).toEqual( |
| 184 | + <rn-view accessibilityState="{disabled:true,selected:false,checked:None,busy:false,expanded:null}" />, |
| 185 | + ); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + describe('children', () => { |
| 190 | + it('renders children inside the touchable', () => { |
| 191 | + const root = Fantom.createRoot(); |
| 192 | + |
| 193 | + Fantom.runTask(() => { |
| 194 | + root.render( |
| 195 | + <TouchableHighlight> |
| 196 | + <Text>Press me</Text> |
| 197 | + </TouchableHighlight>, |
| 198 | + ); |
| 199 | + }); |
| 200 | + |
| 201 | + expect(root.getRenderedOutput({props: ['accessible']}).toJSX()).toEqual( |
| 202 | + <rn-view accessible="true"> |
| 203 | + <rn-paragraph>Press me</rn-paragraph> |
| 204 | + </rn-view>, |
| 205 | + ); |
| 206 | + }); |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
| 210 | + describe('ref', () => { |
| 211 | + describe('instance', () => { |
| 212 | + it('is an element node', () => { |
| 213 | + const elementRef = createRef<HostInstance>(); |
| 214 | + |
| 215 | + const root = Fantom.createRoot(); |
| 216 | + |
| 217 | + Fantom.runTask(() => { |
| 218 | + root.render( |
| 219 | + <TouchableHighlight ref={elementRef}> |
| 220 | + <View /> |
| 221 | + </TouchableHighlight>, |
| 222 | + ); |
| 223 | + }); |
| 224 | + |
| 225 | + expect(elementRef.current).toBeInstanceOf(ReactNativeElement); |
| 226 | + }); |
| 227 | + |
| 228 | + it('uses the "RN:View" tag name', () => { |
| 229 | + const elementRef = createRef<HostInstance>(); |
| 230 | + |
| 231 | + const root = Fantom.createRoot(); |
| 232 | + |
| 233 | + Fantom.runTask(() => { |
| 234 | + root.render( |
| 235 | + <TouchableHighlight ref={elementRef}> |
| 236 | + <View /> |
| 237 | + </TouchableHighlight>, |
| 238 | + ); |
| 239 | + }); |
| 240 | + |
| 241 | + const element = ensureInstance(elementRef.current, ReactNativeElement); |
| 242 | + expect(element.tagName).toBe('RN:View'); |
| 243 | + }); |
| 244 | + }); |
| 245 | + }); |
| 246 | +}); |
0 commit comments