Skip to content

Commit c4e40b8

Browse files
brunohkbxfacebook-github-bot
authored andcommitted
feat: add displayName to touchables (#29531)
Summary: Since TouchableHighlight and TouchableOpacity are being exported using `forwardRef`, it's messing up jest's snapshots and some matchers. This commit 4b935ae fixed this for components being mocked on [setup.js](https://github.com/facebook/react-native/blob/master/jest/setup.js). However, these Touchables aren't being mocked. It resolves #27721 ## Changelog [General] [Added] - Add displayName to TouchableHighlight and TouchableOpacity Pull Request resolved: #29531 Test Plan: Check the new snapshots. Reviewed By: kacieb Differential Revision: D27485269 Pulled By: yungsters fbshipit-source-id: ba2082a4ae9f97ebe93ba92971d58c9195bdf26d
1 parent 679f38f commit c4e40b8

11 files changed

+189
-9
lines changed

Libraries/Components/Touchable/TouchableHighlight.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,13 @@ class TouchableHighlight extends React.Component<Props, State> {
357357
}
358358
}
359359

360-
module.exports = (React.forwardRef((props, hostRef) => (
360+
const Touchable = (React.forwardRef((props, hostRef) => (
361361
<TouchableHighlight {...props} hostRef={hostRef} />
362362
)): React.AbstractComponent<
363363
$ReadOnly<$Diff<Props, {|hostRef: React.Ref<typeof View>|}>>,
364364
React.ElementRef<typeof View>,
365365
>);
366+
367+
Touchable.displayName = 'TouchableHighlight';
368+
369+
module.exports = Touchable;

Libraries/Components/Touchable/TouchableNativeFeedback.js

+2
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,6 @@ const getBackgroundProp =
313313
: {nativeBackgroundAndroid: background}
314314
: (background, useForeground) => null;
315315

316+
TouchableNativeFeedback.displayName = 'TouchableNativeFeedback';
317+
316318
module.exports = TouchableNativeFeedback;

Libraries/Components/Touchable/TouchableOpacity.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ class TouchableOpacity extends React.Component<Props, State> {
265265
}
266266
}
267267

268-
module.exports = (React.forwardRef((props, ref) => (
268+
const Touchable = (React.forwardRef((props, ref) => (
269269
<TouchableOpacity {...props} hostRef={ref} />
270270
)): React.AbstractComponent<Props, React.ElementRef<typeof Animated.View>>);
271+
272+
Touchable.displayName = 'TouchableOpacity';
273+
274+
module.exports = Touchable;

Libraries/Components/Touchable/TouchableWithoutFeedback.js

+2
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,6 @@ function createPressabilityConfig(props: Props): PressabilityConfig {
157157
};
158158
}
159159

160+
TouchableWithoutFeedback.displayName = 'TouchableWithoutFeedback';
161+
160162
module.exports = TouchableWithoutFeedback;

Libraries/Components/Touchable/__tests__/TouchableHighlight-test.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,32 @@
1111
'use strict';
1212

1313
import * as React from 'react';
14-
import ReactTestRenderer from 'react-test-renderer';
1514
import Text from '../../../Text/Text';
1615
import View from '../../View/View';
1716
import TouchableHighlight from '../TouchableHighlight';
1817

18+
const render = require('../../../../jest/renderer');
19+
1920
describe('TouchableHighlight', () => {
2021
it('renders correctly', () => {
21-
const instance = ReactTestRenderer.create(
22+
const instance = render.create(
2223
<TouchableHighlight style={{}}>
2324
<Text>Touchable</Text>
2425
</TouchableHighlight>,
2526
);
2627

2728
expect(instance.toJSON()).toMatchSnapshot();
2829
});
30+
31+
it('has displayName', () => {
32+
expect(TouchableHighlight.displayName).toEqual('TouchableHighlight');
33+
});
2934
});
3035

3136
describe('TouchableHighlight with disabled state', () => {
3237
it('should be disabled when disabled is true', () => {
3338
expect(
34-
ReactTestRenderer.create(
39+
render.create(
3540
<TouchableHighlight disabled={true}>
3641
<View />
3742
</TouchableHighlight>,
@@ -41,7 +46,7 @@ describe('TouchableHighlight with disabled state', () => {
4146

4247
it('should be disabled when disabled is true and accessibilityState is empty', () => {
4348
expect(
44-
ReactTestRenderer.create(
49+
render.create(
4550
<TouchableHighlight disabled={true} accessibilityState={{}}>
4651
<View />
4752
</TouchableHighlight>,
@@ -51,7 +56,7 @@ describe('TouchableHighlight with disabled state', () => {
5156

5257
it('should keep accessibilityState when disabled is true', () => {
5358
expect(
54-
ReactTestRenderer.create(
59+
render.create(
5560
<TouchableHighlight
5661
disabled={true}
5762
accessibilityState={{checked: true}}>
@@ -63,7 +68,7 @@ describe('TouchableHighlight with disabled state', () => {
6368

6469
it('should overwrite accessibilityState with value of disabled prop', () => {
6570
expect(
66-
ReactTestRenderer.create(
71+
render.create(
6772
<TouchableHighlight
6873
disabled={true}
6974
accessibilityState={{disabled: false}}>
@@ -75,7 +80,7 @@ describe('TouchableHighlight with disabled state', () => {
7580

7681
it('should disable button when accessibilityState is disabled', () => {
7782
expect(
78-
ReactTestRenderer.create(
83+
render.create(
7984
<TouchableHighlight accessibilityState={{disabled: true}}>
8085
<View />
8186
</TouchableHighlight>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
const React = require('react');
14+
const Text = require('../../../Text/Text');
15+
const TouchableNativeFeedback = require('../TouchableNativeFeedback');
16+
17+
const render = require('../../../../jest/renderer');
18+
19+
describe('TouchableWithoutFeedback', () => {
20+
it('renders correctly', () => {
21+
const instance = render.create(
22+
<TouchableNativeFeedback style={{}}>
23+
<Text>Touchable</Text>
24+
</TouchableNativeFeedback>,
25+
);
26+
27+
expect(instance.toJSON()).toMatchSnapshot();
28+
});
29+
30+
it('has displayName', () => {
31+
expect(TouchableNativeFeedback.displayName).toEqual(
32+
'TouchableNativeFeedback',
33+
);
34+
});
35+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
const React = require('react');
14+
const Text = require('../../../Text/Text');
15+
const TouchableOpacity = require('../TouchableOpacity');
16+
17+
const render = require('../../../../jest/renderer');
18+
19+
describe('TouchableOpacity', () => {
20+
it('renders correctly', () => {
21+
const instance = render.create(
22+
<TouchableOpacity style={{}}>
23+
<Text>Touchable</Text>
24+
</TouchableOpacity>,
25+
);
26+
27+
expect(instance.toJSON()).toMatchSnapshot();
28+
});
29+
30+
it('has displayName', () => {
31+
expect(TouchableOpacity.displayName).toEqual('TouchableOpacity');
32+
});
33+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
const React = require('react');
14+
const Text = require('../../../Text/Text');
15+
const TouchableWithoutFeedback = require('../TouchableWithoutFeedback');
16+
17+
const render = require('../../../../jest/renderer');
18+
19+
describe('TouchableWithoutFeedback', () => {
20+
it('renders correctly', () => {
21+
const instance = render.create(
22+
<TouchableWithoutFeedback style={{}}>
23+
<Text>Touchable</Text>
24+
</TouchableWithoutFeedback>,
25+
);
26+
27+
expect(instance.toJSON()).toMatchSnapshot();
28+
});
29+
30+
it('has displayName', () => {
31+
expect(TouchableWithoutFeedback.displayName).toEqual(
32+
'TouchableWithoutFeedback',
33+
);
34+
});
35+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`TouchableWithoutFeedback renders correctly 1`] = `
4+
<Text
5+
accessible={true}
6+
focusable={false}
7+
onClick={[Function]}
8+
onResponderGrant={[Function]}
9+
onResponderMove={[Function]}
10+
onResponderRelease={[Function]}
11+
onResponderTerminate={[Function]}
12+
onResponderTerminationRequest={[Function]}
13+
onStartShouldSetResponder={[Function]}
14+
>
15+
Touchable
16+
</Text>
17+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`TouchableOpacity renders correctly 1`] = `
4+
<View
5+
accessible={true}
6+
collapsable={false}
7+
focusable={false}
8+
nativeID="animatedComponent"
9+
onClick={[Function]}
10+
onResponderGrant={[Function]}
11+
onResponderMove={[Function]}
12+
onResponderRelease={[Function]}
13+
onResponderTerminate={[Function]}
14+
onResponderTerminationRequest={[Function]}
15+
onStartShouldSetResponder={[Function]}
16+
style={
17+
Object {
18+
"opacity": 1,
19+
}
20+
}
21+
>
22+
<Text>
23+
Touchable
24+
</Text>
25+
</View>
26+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`TouchableWithoutFeedback renders correctly 1`] = `
4+
<Text
5+
accessible={true}
6+
focusable={false}
7+
onClick={[Function]}
8+
onResponderGrant={[Function]}
9+
onResponderMove={[Function]}
10+
onResponderRelease={[Function]}
11+
onResponderTerminate={[Function]}
12+
onResponderTerminationRequest={[Function]}
13+
onStartShouldSetResponder={[Function]}
14+
>
15+
Touchable
16+
</Text>
17+
`;

0 commit comments

Comments
 (0)