Skip to content

Commit 5a370e1

Browse files
rubennortefacebook-github-bot
authored andcommitted
Adds basic Fantom test for <Button> (#51380)
Summary: Changelog: [internal] Adding more examples of tests for core components with Fantom :) Differential Revision: D74874581
1 parent 001d046 commit 5a370e1

File tree

1 file changed

+248
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)