MnS*_*MnS 9 jestjs react-native react-navigation
我目前正致力于为react-navigation添加jest单元测试,例如:My StackNavigator
const Nav = StackNavigator({
Home: {
screen: Home,
},
Second: {
screen: Second,
}
});
export default class App extends Component<{}> {
render() {
return (
<Nav/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我的主页组件
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>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我的第二个组件导出默认类Second扩展Component <{}> {
render() {
return (
<View>
<Text>Second</Text>
</View>
);
}
Run Code Online (Sandbox Code Playgroud)
}
我应该如何编写jest unit test来测试"当我单击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')));
});
Run Code Online (Sandbox Code Playgroud)
我将通过简单地渲染第二个组件并根据快照检查它来将单击按钮的关注点与正确渲染的验证分开的方式进行测试。
// inside your second component test - still using testing-library
it('matches the snapshot', () => {
expect(render(<Second />).container).toMatchSnapshot();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
536 次 |
| 最近记录: |