Skip to content

Commit 3a66967

Browse files
rubennortefacebook-github-bot
authored andcommitted
Adds basic Fantom test for <Button> (facebook#51380)
Summary: Changelog: [internal] Adding more examples of tests for core components with Fantom :) Reviewed By: lenaic Differential Revision: D74874581
1 parent 2fa9c85 commit 3a66967

File tree

1 file changed

+258
-0
lines changed

1 file changed

+258
-0
lines changed
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
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 * as Fantom from '@react-native/fantom';
15+
import nullthrows from 'nullthrows';
16+
import * as React from 'react';
17+
import {Button} from 'react-native';
18+
19+
describe('<Button>', () => {
20+
describe('props', () => {
21+
describe('title', () => {
22+
it('sets the text of the button', () => {
23+
const root = Fantom.createRoot();
24+
25+
Fantom.runTask(() => {
26+
root.render(<Button title="Hello" />);
27+
});
28+
29+
expect(
30+
root
31+
.getRenderedOutput({props: ['foregroundColor', 'backgroundColor']})
32+
.toJSX(),
33+
).toEqual(
34+
// Upper case on Android (also used by Fantom)
35+
<rn-view backgroundColor="rgba(33, 150, 243, 255)">
36+
<rn-paragraph
37+
backgroundColor="rgba(255, 255, 255, 127)"
38+
foregroundColor="rgba(255, 255, 255, 255)">
39+
HELLO
40+
</rn-paragraph>
41+
</rn-view>,
42+
);
43+
});
44+
});
45+
46+
describe('color', () => {
47+
it('sets the background color of the button (non-iOS)', () => {
48+
const root = Fantom.createRoot();
49+
50+
Fantom.runTask(() => {
51+
root.render(<Button title="Hello" color="blue" />);
52+
});
53+
54+
expect(
55+
root
56+
.getRenderedOutput({props: ['foregroundColor', 'backgroundColor']})
57+
.toJSX(),
58+
).toEqual(
59+
<rn-view backgroundColor="rgba(0, 0, 255, 255)">
60+
<rn-paragraph
61+
backgroundColor="rgba(255, 255, 255, 127)"
62+
foregroundColor="rgba(255, 255, 255, 255)">
63+
HELLO
64+
</rn-paragraph>
65+
</rn-view>,
66+
);
67+
});
68+
});
69+
70+
describe('onPress', () => {
71+
it('is called when the button is pressed', () => {
72+
const root = Fantom.createRoot();
73+
74+
const onPressCallback = jest.fn();
75+
76+
Fantom.runTask(() => {
77+
root.render(<Button title="Hello" onPress={onPressCallback} />);
78+
});
79+
80+
expect(onPressCallback).toHaveBeenCalledTimes(0);
81+
82+
// This is necessary because `<Button>` doesn't provide a `<View>` ref
83+
// but an instance of `Touchable` that isn't a ReactNativeElement.
84+
const buttonViewNode = nullthrows(
85+
root.document.documentElement.firstElementChild,
86+
);
87+
88+
Fantom.dispatchNativeEvent(buttonViewNode, 'click', {});
89+
90+
expect(onPressCallback).toHaveBeenCalledTimes(1);
91+
});
92+
});
93+
94+
describe('disabled', () => {
95+
it('sets different default colors', () => {
96+
const root = Fantom.createRoot();
97+
98+
Fantom.runTask(() => {
99+
root.render(<Button title="Hello" disabled={true} />);
100+
});
101+
102+
expect(
103+
root
104+
.getRenderedOutput({props: ['foregroundColor', 'backgroundColor']})
105+
.toJSX(),
106+
).toEqual(
107+
<rn-view backgroundColor="rgba(223, 223, 223, 255)">
108+
<rn-paragraph
109+
backgroundColor="rgba(255, 255, 255, 127)"
110+
foregroundColor="rgba(161, 161, 161, 255)">
111+
HELLO
112+
</rn-paragraph>
113+
</rn-view>,
114+
);
115+
});
116+
117+
it('prevents the button onPress callback from being called', () => {
118+
const root = Fantom.createRoot();
119+
120+
const onPressCallback = jest.fn();
121+
122+
Fantom.runTask(() => {
123+
root.render(
124+
<Button title="Hello" disabled={true} onPress={onPressCallback} />,
125+
);
126+
});
127+
128+
expect(onPressCallback).toHaveBeenCalledTimes(0);
129+
130+
// This is necessary because `<Button>` doesn't provide a `<View>` ref
131+
// but an instance of `Touchable` that isn't a ReactNativeElement.
132+
const buttonViewNode = nullthrows(
133+
root.document.documentElement.firstElementChild,
134+
);
135+
136+
Fantom.dispatchNativeEvent(buttonViewNode, 'click', {});
137+
138+
expect(onPressCallback).toHaveBeenCalledTimes(0);
139+
});
140+
141+
it('takes precendence over accessibilityState (set to true)', () => {
142+
const root = Fantom.createRoot();
143+
144+
const onPressCallback = jest.fn();
145+
146+
Fantom.runTask(() => {
147+
root.render(
148+
<Button
149+
title="Hello"
150+
disabled={true}
151+
accessibilityState={{disabled: false}}
152+
onPress={onPressCallback}
153+
/>,
154+
);
155+
});
156+
157+
expect(onPressCallback).toHaveBeenCalledTimes(0);
158+
159+
// This is necessary because `<Button>` doesn't provide a `<View>` ref
160+
// but an instance of `Touchable` that isn't a ReactNativeElement.
161+
const buttonViewNode = nullthrows(
162+
root.document.documentElement.firstElementChild,
163+
);
164+
165+
Fantom.dispatchNativeEvent(buttonViewNode, 'click', {});
166+
167+
expect(onPressCallback).toHaveBeenCalledTimes(0);
168+
});
169+
170+
it('takes precendence over accessibilityState (set to false)', () => {
171+
const root = Fantom.createRoot();
172+
173+
const onPressCallback = jest.fn();
174+
175+
Fantom.runTask(() => {
176+
root.render(
177+
<Button
178+
title="Hello"
179+
disabled={false}
180+
accessibilityState={{disabled: true}}
181+
onPress={onPressCallback}
182+
/>,
183+
);
184+
});
185+
186+
expect(
187+
root
188+
.getRenderedOutput({props: ['foregroundColor', 'backgroundColor']})
189+
.toJSX(),
190+
).toEqual(
191+
<rn-view backgroundColor="rgba(33, 150, 243, 255)">
192+
<rn-paragraph
193+
backgroundColor="rgba(255, 255, 255, 127)"
194+
foregroundColor="rgba(255, 255, 255, 255)">
195+
HELLO
196+
</rn-paragraph>
197+
</rn-view>,
198+
);
199+
200+
expect(onPressCallback).toHaveBeenCalledTimes(0);
201+
202+
// This is necessary because `<Button>` doesn't provide a `<View>` ref
203+
// but an instance of `Touchable` that isn't a ReactNativeElement.
204+
const buttonViewNode = nullthrows(
205+
root.document.documentElement.firstElementChild,
206+
);
207+
208+
Fantom.dispatchNativeEvent(buttonViewNode, 'click', {});
209+
210+
expect(onPressCallback).toHaveBeenCalledTimes(1);
211+
});
212+
});
213+
214+
describe('accessibilityState', () => {
215+
it('disables the button when disabled property is true and disabled prop is NOT set', () => {
216+
const root = Fantom.createRoot();
217+
218+
const onPressCallback = jest.fn();
219+
220+
Fantom.runTask(() => {
221+
root.render(
222+
<Button
223+
title="Hello"
224+
accessibilityState={{disabled: true}}
225+
onPress={onPressCallback}
226+
/>,
227+
);
228+
});
229+
230+
expect(
231+
root
232+
.getRenderedOutput({props: ['foregroundColor', 'backgroundColor']})
233+
.toJSX(),
234+
).toEqual(
235+
<rn-view backgroundColor="rgba(223, 223, 223, 255)">
236+
<rn-paragraph
237+
backgroundColor="rgba(255, 255, 255, 127)"
238+
foregroundColor="rgba(161, 161, 161, 255)">
239+
HELLO
240+
</rn-paragraph>
241+
</rn-view>,
242+
);
243+
244+
expect(onPressCallback).toHaveBeenCalledTimes(0);
245+
246+
// This is necessary because `<Button>` doesn't provide a `<View>` ref
247+
// but an instance of `Touchable` that isn't a ReactNativeElement.
248+
const buttonViewNode = nullthrows(
249+
root.document.documentElement.firstElementChild,
250+
);
251+
252+
Fantom.dispatchNativeEvent(buttonViewNode, 'click', {});
253+
254+
expect(onPressCallback).toHaveBeenCalledTimes(0);
255+
});
256+
});
257+
});
258+
});

0 commit comments

Comments
 (0)