如何通过模拟状态并将状态传递给组件来测试 React redux 应用程序

问题描述

我想从 redux 商店模拟状态并使用 jest 测试它们

import React from 'react';
import { mount,shallow } from 'enzyme';
import toJson from 'enzyme-to-json';

import{ users,question,questions } from '../utils/_DATA';
import '../setupTests';

import { leaderBoard } from '../containers/leaderBoard';

import configureStore from 'redux-mock-store';
import renderer from 'react-test-renderer';
import { Provider } from 'react-redux';

const mockStore = configureStore([]);


const ComponentTest =(component) =>  expect(toJson(component)).toMatchSnapshot();
describe('Containers tests',() => {
    let store;
    let MyComponent;
    beforeEach(() =>{
       
        store = mockStore({
            users,signUser:'tylermcginnis',questions
        });
        store.dispatch = jest.fn();
        MyComponent = renderer.create(
            <Provider store={store}>
                <leaderBoard />
            </Provider>
        );
    });
    it('should render leaderboard without error',() => {
        
        expect(MyComponent.toJson()).toMatchSnapshot();
    })

写入 jest --watch 后,我发现此错误显示状态数据无法传递给组件

TypeError: Cannot convert undefined or null to object
        at Function.values (<anonymous>)

The above error occurred in the <leaderBoard> component:

leaderBoard 组件可能会从商店检索传递的数据

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...