Skip to content

Commit ae5949c

Browse files
star-hamstershenzhiqing
authored andcommitted
fix(card): 增加card测试用例,修改组件样式 (#414)
* feat(result): 增加jest测试 * test(card): 增加card测试用例 Co-authored-by: shenzhiqing <shenzhiqing@nihaosi.com>
1 parent b5657e4 commit ae5949c

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

example/examples/src/routes/Card/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const CardDemo = (props: any) => {
1212
const [loading, setLoad] = useState(true);
1313
const basicRender = (
1414
<Fragment>
15-
<View
16-
style={{display: 'flex', alignItems: 'center', flexDirection: 'row'}}>
15+
<View style={{display: 'flex', alignItems: 'center', flexDirection: 'row'}}>
1716
<Image
1817
source={{
1918
uri: 'https://c-ssl.duitang.com/uploads/item/202004/13/20200413013923_25wds.jpeg',
@@ -35,9 +34,8 @@ const CardDemo = (props: any) => {
3534
</View>
3635
<View style={{marginTop: 10}}>
3736
<Text style={{color: '#20303C'}}>
38-
Welcome to @uiw/react-native! Title two is a long title that will not
39-
get cut by default, but can be limited, Title number three, Title
40-
number four, Title number five!
37+
Welcome to @uiw/react-native! Title two is a long title that will not get cut by default, but can be limited, Title number three, Title number four,
38+
Title number five!
4139
</Text>
4240
</View>
4341
<Loader loading={loading}>
@@ -96,6 +94,7 @@ const CardDemo = (props: any) => {
9694
// style: {
9795
// borderColor: "#fd8a00",
9896
// }
97+
// iconStyle: {alignItems: 'flex-end', justifyContent: 'flex-start'},
9998
// }}
10099
>
101100
{basicRender}

packages/core/src/Card/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type CardProps = {
2626
selectedStyle?: {
2727
icon?: React.ReactNode;
2828
style?: ViewStyle;
29+
iconStyle?: ViewStyle;
2930
};
3031
};
3132

@@ -40,6 +41,7 @@ const Card = ({
4041
selectedStyle = {
4142
icon: <Icon xml={checked} size={30} />,
4243
style: {},
44+
iconStyle: {},
4345
},
4446
...attributes
4547
}: CardProps) => {
@@ -66,7 +68,7 @@ const Card = ({
6668
]}
6769
pointerEvents="none"
6870
>
69-
<View style={styles.selectedIndicator}>{selectedStyle?.icon}</View>
71+
<View style={[styles.selectedIndicator, selectedStyle?.iconStyle]}>{selectedStyle?.icon}</View>
7072
</Animated.View>
7173
);
7274
};
@@ -96,7 +98,7 @@ const Card = ({
9698
const styles = StyleSheet.create({
9799
container: {
98100
backgroundColor: colors.white,
99-
borderWidth: 1,
101+
// borderWidth: 1,
100102
padding: 15,
101103
margin: 15,
102104
marginBottom: 0,
@@ -120,7 +122,7 @@ const styles = StyleSheet.create({
120122
borderColor: colors.colorsPalette.violet30,
121123
},
122124
selectedIndicator: {
123-
borderRadius: 999,
125+
// borderRadius: 999,
124126
position: 'absolute',
125127
top: 0,
126128
right: 0,

test-ci/src/__tests__/card.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @format
3+
*/
4+
5+
import 'react-native';
6+
import React from 'react';
7+
import Card from '../lib/Card';
8+
import { Image } from 'react-native';
9+
// Note: test renderer must be required after react-native.
10+
import renderer from 'react-test-renderer';
11+
12+
it('Card', () => {
13+
const onPressMock = jest.fn();
14+
15+
const component = renderer.create(
16+
<Card selected={true} borderRadius={12} onPress={onPressMock}>
17+
<Image
18+
source={{
19+
uri: 'https://iknow-pic.cdn.bcebos.com/810a19d8bc3eb135828572d2ab1ea8d3fd1f441d',
20+
}}
21+
style={{ height: 200, marginTop: 10 }}
22+
/>
23+
</Card>,
24+
);
25+
// 判断selected是否为真
26+
expect(component.root.props.selected).toBeTruthy();
27+
expect(component.root.props.borderRadius).toBe(12);
28+
// 调用点击事件,确保模拟函数被调用的确切次数
29+
component.root.props.onPress(onPressMock);
30+
expect(onPressMock).toHaveBeenCalledTimes(1);
31+
});

0 commit comments

Comments
 (0)