i当前正在为添加React-Navigation的开玩笑单元测试,例如:我的stacknavigator
const Nav = StackNavigator({
Home: {
screen: Home,
},
Second: {
screen: Second,
}
});
export default class App extends Component<{}> {
render() {
return (
<Nav/>
);
}
}
我的家组件
export default class Home extends Component<{}> {
_goToNextPage = () => {
this.props.navigation.navigate('Second');
}
render() {
return (
<View>
<Text>Home</Text>
<Button
onPress={this._goToNextPage}
title="Go to Second Page"
>Click to next page</Button>
</View>
);
}
}
我的第二个组件导出默认类Second扩展了组件&lt; {}> {
render() {
return (
<View>
<Text>Second</Text>
</View>
);
}
}
我应该如何编写开玩笑的单元测试来测试"当我单击gotOnextPage按钮时,第二个组件应正确渲染?"
我找不到有关开玩笑的任何有用的信息,并没有得到任何帮助!!!
!非常感谢〜
我个人喜欢@testing-library/react
,因为从用户角度来看,他们围绕测试的理念。我想您的测试看起来会如下:
it('shows the second component when the next button is clicked', async () => {
// Renders your app
const { getByText, findByText } = render(<App />);
// Clicks your button
fireEvent.click(getByText('Click to next page'));
// Waits for the 'Second' text to be visible, could be async or sync, in this
// case I'm using the async expectation style
await wait(() => expect(getByText('Second')));
});
我将以单击按钮的关注点的关注以及它通过简单地渲染第二个组件并根据快照对其进行检查而正确渲染的验证的方式进行测试。
。// inside your second component test - still using testing-library
it('matches the snapshot', () => {
expect(render(<Second />).container).toMatchSnapshot();
});