Skip to content

Commit 426cae6

Browse files
sammy-SCmeta-codesync[bot]
authored andcommitted
Add Fantom integration tests (#55760)
Summary: Pull Request resolved: #55760 Add new Fantom integration tests for TouchableOpacity, achieving 40.54% line coverage for TouchableOpacity.js (from 0%). Changelog: [Internal] Reviewed By: CalixTang Differential Revision: D94360684 fbshipit-source-id: 474bd7b8d82dea65d8104f231b22199faacae254
1 parent 078f5ca commit 426cae6

1 file changed

Lines changed: 215 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)