React Native单元测试实战: 使用Jest测试收藏为空组件
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { getImageUrlForPath, scaleSize } from '@utils/layout';
import LColors from '@assets/Strings/LColors';
import RouteHelper from '@common/Web/RouteHelper';
import LNetImage from '@components/LNetImage';
import LMainButton from '@components/LMainButton';
const CollectionEmpty = ({ navigation }) => {
return (
<View style={{ flex: 1, alignItems: 'center' }}>
<LNetImage
style={{
width: scaleSize(97),
height: scaleSize(110),
marginTop: scaleSize(200),
}}
source={{ uri: getImageUrlForPath('/emptyImg/Like_empty@2x.png') }}
testID='empty-image'
/>
<Text style={{ marginTop: 15, fontSize: 15, color: LColors.N2 }}>还没收藏任何商品哟</Text>
<LMainButton
sensorsDataParams={{ '$element_id': 'm_orderList_empty_btn_dwji2' }}
title={'去逛逛'}
colors={[LColors.N6, LColors.N6]}
style={{
borderColor: LColors.C1,
borderWidth: StyleSheet.hairlineWidth,
borderRadius: 15,
height: 30,
marginTop: 15,
paddingHorizontal: 15,
}}
textStyle={{
color: LColors.C1,
fontSize: 14,
}}
onPress={() => {
RouteHelper.navigate('SortScreen', navigation, null);
}}
/>
</View>
);
};
export default CollectionEmpty;
import React from 'react';
import { View, Text } from 'react-native';
import { render, fireEvent } from '@testing-library/react-native';
import CollectionEmpty from './CollectionEmpty';
describe('CollectionEmpty', () => {
test('renders correctly', () => {
const { getByText, getByTestId } = render(<CollectionEmpty navigation={{ navigate: jest.fn() }}/>);
const image = getByTestId('empty-image');
expect(image.props.source.uri).toBe('https://example.com/emptyImg/Like_empty@2x.png');
const text = getByText('还没收藏任何商品哟');
expect(text.props.style.color).toBe('#333333');
const button = getByText('去逛逛');
expect(button.props.title).toBe('去逛逛');
fireEvent.press(button);
// assert that navigation to SortScreen is triggered
});
});
原文地址: https://www.cveoy.top/t/topic/fxaQ 著作权归作者所有。请勿转载和采集!