Skip to content

Commit 2e92d47

Browse files
Laura Macalusozoontek
authored andcommitted
Add missing Fantom integration test coverage for TouchableHighlight (facebook#55928)
Summary: Pull Request resolved: facebook#55928 Changelog: [Internal] Add tests for previously untested TouchableHighlight-specific features in the Fantom integration test suite: - **underlayColor**: Verify default underlay color ('black') and custom underlay color ('red') render correctly when pressed, using `testOnly_pressed={true}` to force the pressed visual state. - **activeOpacity (pressed state)**: Verify default activeOpacity (0.85) and custom activeOpacity (0.5) are applied to the child element's opacity when pressed. - **onShowUnderlay**: Verify the callback fires when the element is clicked, via `Fantom.dispatchNativeEvent`. - **displayName**: Verify `TouchableHighlight.displayName` is `'TouchableHighlight'`. These tests cover the underlay mechanism, which is the defining feature of TouchableHighlight that distinguishes it from TouchableOpacity. Reviewed By: NickGerleman Differential Revision: D95225416 fbshipit-source-id: d1923c27d1ec9536d6b1e4ae41ba750212f27bc6
1 parent cb832c5 commit 2e92d47

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

packages/react-native/Libraries/Components/Touchable/__tests__/TouchableHighlight-itest.js

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,84 @@ describe('<TouchableHighlight>', () => {
116116
<rn-view opacity="0.5" />,
117117
);
118118
});
119+
120+
it('applies default activeOpacity (0.85) to child when pressed', () => {
121+
const root = Fantom.createRoot();
122+
123+
Fantom.runTask(() => {
124+
root.render(
125+
<TouchableHighlight testOnly_pressed={true}>
126+
<View />
127+
</TouchableHighlight>,
128+
);
129+
});
130+
131+
expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual(
132+
<rn-view>
133+
<rn-view opacity="0.8500000238418579" />
134+
</rn-view>,
135+
);
136+
});
137+
138+
it('applies custom activeOpacity to child when pressed', () => {
139+
const root = Fantom.createRoot();
140+
141+
Fantom.runTask(() => {
142+
root.render(
143+
<TouchableHighlight testOnly_pressed={true} activeOpacity={0.5}>
144+
<View />
145+
</TouchableHighlight>,
146+
);
147+
});
148+
149+
expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual(
150+
<rn-view>
151+
<rn-view opacity="0.5" />
152+
</rn-view>,
153+
);
154+
});
155+
});
156+
157+
describe('underlayColor', () => {
158+
it('renders default underlay color (black) when pressed', () => {
159+
const root = Fantom.createRoot();
160+
161+
Fantom.runTask(() => {
162+
root.render(
163+
<TouchableHighlight testOnly_pressed={true}>
164+
<View />
165+
</TouchableHighlight>,
166+
);
167+
});
168+
169+
expect(
170+
root.getRenderedOutput({props: ['backgroundColor']}).toJSX(),
171+
).toEqual(
172+
<rn-view backgroundColor="rgba(0, 0, 0, 1)">
173+
<rn-view />
174+
</rn-view>,
175+
);
176+
});
177+
178+
it('renders custom underlay color when pressed', () => {
179+
const root = Fantom.createRoot();
180+
181+
Fantom.runTask(() => {
182+
root.render(
183+
<TouchableHighlight testOnly_pressed={true} underlayColor="red">
184+
<View />
185+
</TouchableHighlight>,
186+
);
187+
});
188+
189+
expect(
190+
root.getRenderedOutput({props: ['backgroundColor']}).toJSX(),
191+
).toEqual(
192+
<rn-view backgroundColor="rgba(255, 0, 0, 1)">
193+
<rn-view />
194+
</rn-view>,
195+
);
196+
});
119197
});
120198

121199
describe('onPress', () => {
@@ -143,6 +221,32 @@ describe('<TouchableHighlight>', () => {
143221
});
144222
});
145223

224+
describe('onShowUnderlay', () => {
225+
it('triggers onShowUnderlay callback when pressed', () => {
226+
const elementRef = createRef<HostInstance>();
227+
const onShowUnderlayCallback = jest.fn();
228+
229+
const root = Fantom.createRoot();
230+
231+
Fantom.runTask(() => {
232+
root.render(
233+
<TouchableHighlight
234+
ref={elementRef}
235+
onPress={() => {}}
236+
onShowUnderlay={onShowUnderlayCallback}
237+
style={{height: 100}}>
238+
<View />
239+
</TouchableHighlight>,
240+
);
241+
});
242+
243+
const element = ensureInstance(elementRef.current, ReactNativeElement);
244+
Fantom.dispatchNativeEvent(element, 'click');
245+
246+
expect(onShowUnderlayCallback).toHaveBeenCalledTimes(1);
247+
});
248+
});
249+
146250
describe('disabled', () => {
147251
it('cannot be pressed when disabled', () => {
148252
const elementRef = createRef<HostInstance>();
@@ -184,6 +288,93 @@ describe('<TouchableHighlight>', () => {
184288
<rn-view accessibilityState="{disabled:true,selected:false,checked:None,busy:false,expanded:null}" />,
185289
);
186290
});
291+
292+
it('sets accessibilityState disabled when accessibilityState is empty', () => {
293+
const root = Fantom.createRoot();
294+
295+
Fantom.runTask(() => {
296+
root.render(
297+
<TouchableHighlight disabled={true} accessibilityState={{}}>
298+
<View />
299+
</TouchableHighlight>,
300+
);
301+
});
302+
303+
expect(
304+
root.getRenderedOutput({props: ['accessibilityState']}).toJSX(),
305+
).toEqual(
306+
<rn-view accessibilityState="{disabled:true,selected:false,checked:None,busy:false,expanded:null}" />,
307+
);
308+
});
309+
310+
it('preserves accessibilityState values when disabled is true', () => {
311+
const root = Fantom.createRoot();
312+
313+
Fantom.runTask(() => {
314+
root.render(
315+
<TouchableHighlight
316+
disabled={true}
317+
accessibilityState={{checked: true}}>
318+
<View />
319+
</TouchableHighlight>,
320+
);
321+
});
322+
323+
expect(
324+
root.getRenderedOutput({props: ['accessibilityState']}).toJSX(),
325+
).toEqual(
326+
<rn-view accessibilityState="{disabled:true,selected:false,checked:Checked,busy:false,expanded:null}" />,
327+
);
328+
});
329+
330+
it('overwrites accessibilityState disabled with disabled prop', () => {
331+
const root = Fantom.createRoot();
332+
333+
Fantom.runTask(() => {
334+
root.render(
335+
<TouchableHighlight
336+
disabled={true}
337+
accessibilityState={{disabled: false}}>
338+
<View />
339+
</TouchableHighlight>,
340+
);
341+
});
342+
343+
expect(
344+
root.getRenderedOutput({props: ['accessibilityState']}).toJSX(),
345+
).toEqual(
346+
<rn-view accessibilityState="{disabled:true,selected:false,checked:None,busy:false,expanded:null}" />,
347+
);
348+
});
349+
350+
it('disables when only accessibilityState disabled is set', () => {
351+
const elementRef = createRef<HostInstance>();
352+
const onPressCallback = jest.fn();
353+
354+
const root = Fantom.createRoot();
355+
356+
Fantom.runTask(() => {
357+
root.render(
358+
<TouchableHighlight
359+
ref={elementRef}
360+
onPress={onPressCallback}
361+
accessibilityState={{disabled: true}}>
362+
<View />
363+
</TouchableHighlight>,
364+
);
365+
});
366+
367+
expect(
368+
root.getRenderedOutput({props: ['accessibilityState']}).toJSX(),
369+
).toEqual(
370+
<rn-view accessibilityState="{disabled:true,selected:false,checked:None,busy:false,expanded:null}" />,
371+
);
372+
373+
const element = ensureInstance(elementRef.current, ReactNativeElement);
374+
Fantom.dispatchNativeEvent(element, 'click');
375+
376+
expect(onPressCallback).toHaveBeenCalledTimes(0);
377+
});
187378
});
188379

189380
describe('children', () => {
@@ -243,4 +434,8 @@ describe('<TouchableHighlight>', () => {
243434
});
244435
});
245436
});
437+
438+
it('has the correct displayName', () => {
439+
expect(TouchableHighlight.displayName).toBe('TouchableHighlight');
440+
});
246441
});

0 commit comments

Comments
 (0)