mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-05-29 09:02:03 +00:00
[change] modernize Button
Rewrite Button to use function components. Rewrite the tests to replace enzyme with testing-library.
This commit is contained in:
@@ -3,30 +3,20 @@
|
|||||||
|
|
||||||
import Button from '..';
|
import Button from '..';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import StyleSheet from '../../StyleSheet';
|
import { render } from '@testing-library/react';
|
||||||
import TouchableOpacity from '../../TouchableOpacity';
|
|
||||||
import { render, shallow } from 'enzyme';
|
|
||||||
|
|
||||||
describe('components/Button', () => {
|
describe('components/Button', () => {
|
||||||
test('prop "color"', () => {
|
test('prop "color"', () => {
|
||||||
const onPress = () => {};
|
const onPress = () => {};
|
||||||
const color = 'blue';
|
const color = 'rgb(0, 0, 255)';
|
||||||
const button = shallow(<Button color={color} onPress={onPress} title="" />);
|
const { container } = render(<Button color={color} onPress={onPress} title="" />);
|
||||||
const style = StyleSheet.flatten(button.prop('style'));
|
expect(container.firstChild.style.backgroundColor).toEqual(color);
|
||||||
expect(style.backgroundColor).toEqual(color);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('prop "onPress"', () => {
|
|
||||||
const onPress = jest.fn();
|
|
||||||
const component = shallow(<Button onPress={onPress} title="" />);
|
|
||||||
component.find(TouchableOpacity).simulate('press');
|
|
||||||
expect(onPress).toHaveBeenCalled();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('prop "title"', () => {
|
test('prop "title"', () => {
|
||||||
const onPress = () => {};
|
const onPress = () => {};
|
||||||
const text = 'Click me';
|
const text = 'Click me';
|
||||||
const component = render(<Button onPress={onPress} title={text} />);
|
const { getByText } = render(<Button onPress={onPress} title={text} />);
|
||||||
expect(component.text()).toEqual(text);
|
expect(getByText(text)).toBeDefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+18
-22
@@ -22,27 +22,25 @@ type ButtonProps = {|
|
|||||||
title: string
|
title: string
|
||||||
|};
|
|};
|
||||||
|
|
||||||
class Button extends React.Component<ButtonProps> {
|
export default function Button(props: ButtonProps) {
|
||||||
render() {
|
const { accessibilityLabel, color, disabled, onPress, testID, title } = props;
|
||||||
const { accessibilityLabel, color, disabled, onPress, testID, title } = this.props;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
accessibilityLabel={accessibilityLabel}
|
accessibilityLabel={accessibilityLabel}
|
||||||
accessibilityRole="button"
|
accessibilityRole="button"
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
onPress={onPress}
|
onPress={onPress}
|
||||||
style={[
|
style={[
|
||||||
styles.button,
|
styles.button,
|
||||||
color && { backgroundColor: color },
|
color && { backgroundColor: color },
|
||||||
disabled && styles.buttonDisabled
|
disabled && styles.buttonDisabled
|
||||||
]}
|
]}
|
||||||
testID={testID}
|
testID={testID}
|
||||||
>
|
>
|
||||||
<Text style={[styles.text, disabled && styles.textDisabled]}>{title}</Text>
|
<Text style={[styles.text, disabled && styles.textDisabled]}>{title}</Text>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
@@ -64,5 +62,3 @@ const styles = StyleSheet.create({
|
|||||||
color: '#a1a1a1'
|
color: '#a1a1a1'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export default Button;
|
|
||||||
|
|||||||
Reference in New Issue
Block a user