ale*_*.ac 16 mocking jestjs react-native react-navigation
我正在研究一个也使用Redux的React Native应用程序,我想用Jest编写测试.我无法模仿反应导航添加的"导航"道具.
这是我的组件:
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Text, View } from 'react-native';
const Loading = (props) => {
if (props.rehydrated === true) {
const { navigate } = props.navigation;
navigate('Main');
}
return (
<View>
<Text>Loading...</Text>
</View>
);
};
Loading.propTypes = {
rehydrated: PropTypes.bool.isRequired,
navigation: PropTypes.shape({
navigate: PropTypes.func.isRequired,
}).isRequired,
};
const mapStateToProps = state => ({
rehydrated: state.rehydrated,
});
export default connect(mapStateToProps)(Loading);
Run Code Online (Sandbox Code Playgroud)
Loading组件作为屏幕添加到DrawerNavigator.
以下是测试:
import React from 'react';
import renderer from 'react-test-renderer';
import mockStore from 'redux-mock-store';
import Loading from '../';
describe('Loading screen', () => {
it('should display loading text if not rehydrated', () => {
const store = mockStore({
rehydrated: false,
navigation: { navigate: jest.fn() },
});
expect(renderer.create(<Loading store={store} />)).toMatchSnapshot();
});
});
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我收到以下错误:
Warning: Failed prop type: The prop `navigation` is marked as required in `Loading`, but its value is `undefined`.
in Loading (created by Connect(Loading))
in Connect(Loading)
Run Code Online (Sandbox Code Playgroud)
有关如何模拟导航属性的任何想法?
quo*_*Bro 25
尝试navigation
直接通过道具传递:
it('should display loading text if not rehydrated', () => {
const store = mockStore({
rehydrated: false,
});
const navigation = { navigate: jest.fn() };
expect(renderer.create(<Loading store={store} navigation={navigation} />)).toMatchSnapshot();
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11258 次 |
最近记录: |