我看到了一些与此相关的问题,但没有一个回答我的查询。谁能告诉我如何测试这些文件。例如,我具有以下功能组件(仅相关代码):
NewsFeed.js
const NewsFeed = (props) => {
const renderitem = ({item}: props) => {
if (item.id === '1') {
return (
<View>
<View style={styles.headertext}>
<Text>News Feed</Text>
</View>
<View style={styles.border} />
</View>
)
}
if (item.id === '2') {
return (
<View>
<View style={styles.header2}>
// playing with screen
</View>
<View style={styles.border} />
</View>
)
}
return (
<View />
)
}
return (
<View style={styles.container}>
<View style={styles.container1}>
<View>
<FlatList
ref={flatlist}
data={DATA}
renderItem={renderitem}
keyExtractor={(item) => item.id}
/>
</View>
</View>
</View>
)
}
现在我尝试为此执行测试,但是我只能潜入renderitem,因此我的快照仅包含View标记。如何检查条件语句?
newsfeed.test.js
const DATA = [
{
id: '1',
title: 'header1'
},
{
id: '2',
title: 'header2'
},
{
id: '3',
title: 'feed'
}
]
describe('NewsFeed', () => {
let props
let wrapper
beforeEach(() => {
props = createTestProps({})
wrapper = shallow(<NewsFeed{...props} />)
})
it('NewsFeed component renders correctly', () => {
expect(wrapper).toMatchSnapshot()
})
// Higher Order Function Testing
it('render the flatlist properties', () => {
const flatlist = wrapper.find(FlatList);
const itemWrapper = flatlist.renderProp('renderItem')({ item: DATA });
expect(itemWrapper).toMatchSnapshot();
})
})