使用jest进行Redux测试

时间:2017-12-29 11:34:00

标签: reactjs react-native enzyme jest

我正在尝试测试使用redux的反应导航无状态组件,如下所示

import 'react-native';
import React from 'react';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json';
import { createStore } from 'redux';
import AppNavigator, { mapStateToProps } from '../../src/navigation';
import { Pages } from '../../src/data';

const nav = { routeName: Pages.AFFIRMATION };

describe('Testing App Navigation', () => {
    let state = null;
    it('renders as expected', () => {
        const wrapper = shallow(
            <AppNavigator store={createStore(() => nav)} />
        );
        expect(toJson(wrapper)).toMatchSnapshot();
    });
});

AppNavigator组件如下所示:

import React from 'react';
import { addNavigationHelpers } from 'react-navigation';
import { connect } from 'react-redux';
import { Navigator } from './routes';

const AppNavigator = ({ dispatch, nav }) => (
    <Navigator navigation={addNavigationHelpers({ dispatch, state: nav })} />
);

const mapStateToProps = state => ({
    nav: state.nav,
});

export default connect(mapStateToProps)(AppNavigator);

我检查代码覆盖率时遇到的挑战npm test -- --coveraage我发现这一行(11)<Navigator navigation={addNavigationHelpers({ dispatch, state: nav })} />和调用它的函数都被发现了。

line 11 is uncovered

我错过了什么,为什么11号线被发现?

1 个答案:

答案 0 :(得分:0)

解决方案是将AppNnavigator功能分离到另一个文件中,然后进行测试