Skip to content

Add Functional Tests for TouchableWithoutFeedback Component #14766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,107 @@ class TouchableHitSlop extends React.Component<{...}, $FlowFixMeState> {
}
}

class TouchableWithoutFeedbackHitSlop extends React.Component<
{...},
$FlowFixMeState,
> {
state: any | {timesPressed: number} = {
timesPressed: 0,
};

onPress = () => {
this.setState({
timesPressed: this.state.timesPressed + 1,
});
};

render(): React.Node {
let log = '';
if (this.state.timesPressed > 1) {
log = this.state.timesPressed + 'x onPress';
} else if (this.state.timesPressed > 0) {
log = 'onPress';
}

return (
<View testID="touchable_without_feedback_hit_slop">
<View style={[styles.row, styles.centered]}>
<TouchableWithoutFeedback
onPress={this.onPress}
hitSlop={{top: 30, bottom: 30, left: 60, right: 60}}
testID="touchable_without_feedback_hit_slop_button">
<View style={styles.hitSlopWrapper}>
<RNTesterText style={styles.hitSlopButton}>
Press Outside This View
</RNTesterText>
</View>
</TouchableWithoutFeedback>
</View>
<View style={styles.logBox}>
<RNTesterText testID="touchable_without_feedback_hit_slop_console">
{log}
</RNTesterText>
</View>
</View>
);
}
}

class TouchableWithoutFeedbackStyleUpdate extends React.Component<
{...},
$FlowFixMeState,
> {
state: any | {dynamicColor: string, timesPressed: number} = {
dynamicColor: '#007AFF',
timesPressed: 0,
};

onPress = () => {
const colors = ['#007AFF', '#FF6B35', '#4ECDC4', '#45B7D1', '#96CEB4'];
const nextColor = colors[(this.state.timesPressed + 1) % colors.length];
this.setState({
dynamicColor: nextColor,
timesPressed: this.state.timesPressed + 1,
});
};

render(): React.Node {
const dynamicStyle = {
backgroundColor: this.state.dynamicColor,
padding: 10,
borderRadius: 8,
};

let log = '';
if (this.state.timesPressed > 1) {
log = this.state.timesPressed + 'x style updated';
} else if (this.state.timesPressed > 0) {
log = 'style updated';
}

return (
<View testID="touchable_without_feedback_style_update">
<View style={[styles.row, styles.centered]}>
<TouchableWithoutFeedback
onPress={this.onPress}
testID="touchable_without_feedback_style_update_button">
<View style={[styles.wrapperCustom, dynamicStyle]}>
<RNTesterText style={[styles.text, {color: 'white'}]}>
Press to Update Style!
</RNTesterText>
</View>
</TouchableWithoutFeedback>
</View>
<View style={styles.logBox}>
<RNTesterText testID="touchable_without_feedback_style_update_console">
{log}
</RNTesterText>
</View>
</View>
);
}
}

function TouchableNativeMethodChecker<
T: component(ref?: React.RefSetter<any>, ...any),
>(props: {Component: T, name: string}): React.Node {
Expand Down Expand Up @@ -776,6 +877,23 @@ exports.examples = [
return <TouchableHitSlop />;
},
},
{
title: 'TouchableWithoutFeedback Hit Slop',
description:
('TouchableWithoutFeedback accepts hitSlop prop which extends the touch area ' +
'without changing the view bounds.': string),
render(): React.MixedElement {
return <TouchableWithoutFeedbackHitSlop />;
},
},
{
title: 'TouchableWithoutFeedback Style Update',
description:
('TouchableWithoutFeedback can update styles dynamically and should support fast refresh.': string),
render(): React.MixedElement {
return <TouchableWithoutFeedbackStyleUpdate />;
},
},
{
title: 'Touchable Native Methods',
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,42 @@ describe('Touchable Tests', () => {
expect(dump2).toMatchSnapshot();
await searchBox('');
});
test('TouchableWithoutFeedback should register press in clicked within hitSlop range', async () => {
await searchBox('TouchableWithoutFeedback Hit Slop');
const component = await app.findElementByTestID(
'touchable_without_feedback_hit_slop_button',
);
await component.waitForDisplayed({timeout: 5000});
const dump = await dumpVisualTree(
'touchable_without_feedback_hit_slop_button',
);
expect(dump).toMatchSnapshot();
await component.click();
const dump2 = await dumpVisualTree(
'touchable_without_feedback_hit_slop_console',
);
expect(dump2).toMatchSnapshot();
await searchBox('');
});
test('TouchableWithoutFeedback should update style upon fast refresh', async () => {
await searchBox('TouchableWithoutFeedback Style Update');
const component = await app.findElementByTestID(
'touchable_without_feedback_style_update_button',
);
await component.waitForDisplayed({timeout: 5000});
const dump = await dumpVisualTree(
'touchable_without_feedback_style_update_button',
);
expect(dump).toMatchSnapshot();
await component.click();
const dump2 = await dumpVisualTree(
'touchable_without_feedback_style_update_button',
);
expect(dump2).toMatchSnapshot();
const dump3 = await dumpVisualTree(
'touchable_without_feedback_style_update_console',
);
expect(dump3).toMatchSnapshot();
await searchBox('');
});
});
Loading