|
| 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 {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 {Pressable} from 'react-native'; |
| 20 | +import {Text} from 'react-native'; |
| 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('<Pressable>', () => { |
| 25 | + describe('props', () => { |
| 26 | + describe('style', () => { |
| 27 | + it('can be set with ViewStyle', () => { |
| 28 | + const root = Fantom.createRoot(); |
| 29 | + |
| 30 | + Fantom.runTask(() => { |
| 31 | + root.render( |
| 32 | + <Pressable |
| 33 | + style={{ |
| 34 | + width: 100, |
| 35 | + height: 50, |
| 36 | + backgroundColor: 'blue', |
| 37 | + borderColor: 'red', |
| 38 | + borderWidth: 3, |
| 39 | + opacity: 40, |
| 40 | + }} |
| 41 | + />, |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + expect(root.getRenderedOutput().toJSX()).toEqual( |
| 46 | + <rn-view |
| 47 | + accessible="true" |
| 48 | + backgroundColor="rgba(0, 0, 255, 1)" |
| 49 | + borderWidth="3.000000" |
| 50 | + height="50.000000" |
| 51 | + opacity="40" |
| 52 | + width="100.000000" |
| 53 | + />, |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('function that receives a boolean reflecting whether the component is currently pressed ', () => { |
| 58 | + const root = Fantom.createRoot(); |
| 59 | + Fantom.runTask(() => { |
| 60 | + root.render( |
| 61 | + <Pressable |
| 62 | + style={({pressed}) => ({ |
| 63 | + backgroundColor: pressed ? 'red' : 'gray', |
| 64 | + })} |
| 65 | + />, |
| 66 | + ); |
| 67 | + }); |
| 68 | + expect(root.getRenderedOutput().toJSX()).toEqual( |
| 69 | + <rn-view |
| 70 | + accessible="true" |
| 71 | + backgroundColor="rgba(128, 128, 128, 1)" |
| 72 | + />, |
| 73 | + ); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('onPress', () => { |
| 78 | + it('triggers callback when the element is pressed', () => { |
| 79 | + const elementRef = createRef<HostInstance>(); |
| 80 | + const onPressCallback = jest.fn(); |
| 81 | + |
| 82 | + const root = Fantom.createRoot(); |
| 83 | + |
| 84 | + Fantom.runTask(() => { |
| 85 | + root.render( |
| 86 | + <Pressable |
| 87 | + ref={elementRef} |
| 88 | + onPress={onPressCallback} |
| 89 | + style={{height: 100}} |
| 90 | + />, |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + const element = ensureInstance(elementRef.current, ReactNativeElement); |
| 95 | + Fantom.dispatchNativeEvent(element, 'click'); |
| 96 | + |
| 97 | + expect(onPressCallback).toHaveBeenCalledTimes(1); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('disabled', () => { |
| 102 | + it('cannot be pressed', () => { |
| 103 | + const elementRef = createRef<HostInstance>(); |
| 104 | + |
| 105 | + const root = Fantom.createRoot(); |
| 106 | + const onPressCallback = jest.fn(); |
| 107 | + |
| 108 | + Fantom.runTask(() => { |
| 109 | + root.render( |
| 110 | + <Pressable |
| 111 | + ref={elementRef} |
| 112 | + onPress={onPressCallback} |
| 113 | + disabled={true} |
| 114 | + />, |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + const element = ensureInstance(elementRef.current, ReactNativeElement); |
| 119 | + Fantom.dispatchNativeEvent(element, 'change', {value: true}); |
| 120 | + |
| 121 | + expect(onPressCallback).toHaveBeenCalledTimes(0); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('children', () => { |
| 126 | + it('adds children to the component', () => { |
| 127 | + const elementRef = createRef<HostInstance>(); |
| 128 | + const root = Fantom.createRoot(); |
| 129 | + |
| 130 | + Fantom.runTask(() => { |
| 131 | + root.render( |
| 132 | + <Pressable ref={elementRef}> |
| 133 | + <Text>the quick brown fox</Text> |
| 134 | + </Pressable>, |
| 135 | + ); |
| 136 | + }); |
| 137 | + const element = ensureInstance(elementRef.current, ReactNativeElement); |
| 138 | + |
| 139 | + expect(element.childNodes.length).toBe(1); |
| 140 | + |
| 141 | + expect(root.getRenderedOutput().toJSX()).toEqual( |
| 142 | + <rn-view accessible="true"> |
| 143 | + <rn-paragraph |
| 144 | + allowFontScaling="true" |
| 145 | + ellipsizeMode="tail" |
| 146 | + fontSize="NaN" |
| 147 | + fontSizeMultiplier="NaN" |
| 148 | + foregroundColor="rgba(0, 0, 0, 0)"> |
| 149 | + the quick brown fox |
| 150 | + </rn-paragraph> |
| 151 | + </rn-view>, |
| 152 | + ); |
| 153 | + }); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('ref', () => { |
| 158 | + describe('instance', () => { |
| 159 | + it('is an element node', () => { |
| 160 | + const elementRef = createRef<HostInstance>(); |
| 161 | + |
| 162 | + const root = Fantom.createRoot(); |
| 163 | + |
| 164 | + Fantom.runTask(() => { |
| 165 | + root.render(<Pressable ref={elementRef} />); |
| 166 | + }); |
| 167 | + |
| 168 | + expect(elementRef.current).toBeInstanceOf(ReactNativeElement); |
| 169 | + }); |
| 170 | + |
| 171 | + it('uses the "RN:View" tag name', () => { |
| 172 | + const elementRef = createRef<HostInstance>(); |
| 173 | + |
| 174 | + const root = Fantom.createRoot(); |
| 175 | + |
| 176 | + Fantom.runTask(() => { |
| 177 | + root.render(<Pressable ref={elementRef} />); |
| 178 | + }); |
| 179 | + |
| 180 | + const element = ensureInstance(elementRef.current, ReactNativeElement); |
| 181 | + // Pressable is implemented with a <View> under the hood |
| 182 | + expect(element.tagName).toBe('RN:View'); |
| 183 | + }); |
| 184 | + }); |
| 185 | + }); |
| 186 | +}); |
0 commit comments