gpt4 book ai didi

reactjs - 使用 Enzyme 测试 Redux 连接的组件

转载 作者:行者123 更新时间:2023-12-03 13:36:33 25 4
gpt4 key购买 nike

使用 enzyme 测试 redux 连接组件时遇到问题

import React from 'react'
import { shallow, mount, render } from 'enzyme'
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-15';
import Login from '../../src/routes/login/components/Login'

configure({ adapter: new Adapter() })

describe('<Login />', () => {
test('component has form-group nodes', () => {
const component = shallow(<Login />).dive()
expect(component.find('.form-group')).to.have.length(2)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

但控制台中出现错误 - 不变违规:无法在“Connect(Login)”的上下文或属性中找到“store”。

如何处理?

最佳答案

我遇到了类似的问题,我设法使用 redux-mock-store 解决了它对于我的商店模拟,我使用 mount 而不是shallow 来通过 redux 到达连接的组件,如下所示:

import React, { Component } from 'react';
import { mount } from 'enzyme';
import { expect } from 'chai';
import { Provider } from 'react-redux';
import configureStore from 'redux-mock-store';
import App from '../../src/App';

it('renders without crashing', () => {

const mockStore = configureStore();
const initialState = {
someState: [],
someReducer: {},
};

const store = mockStore(initialState);

const wrapper = mount(
<Provider store={store}>
<App />
</Provider>
);

console.log(wrapper.debug())

expect(wrapper.find('.app-container')).to.have.lengthOf(1);
});

关于reactjs - 使用 Enzyme 测试 Redux 连接的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49446530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com