0

我正在尝试进行 DOM 测试,以检查单击按钮时是否打开了对话框。然后我得到了这个错误

不变违规:在>“连接(照片)”的上下文或道具中找不到“商店”。要么将根组件包装在 a 中,要么 > 明确地将“store”作为道具传递给“Connect(Photos)”。

我使用了 jest,并且我的组件使用连接到 redux 商店

导出默认连接(mapToProps)(照片);

配饰Photos.js

class Photos extends React.Component {
    constructor() {
        super();
        this.state = {
            open: false,

        }
        this.handleOpen = this.handleOpen.bind(this);
    }
    handleOpen = () => {
        this.setState({ open: true });
    };

    render (){
        return (....)
    }

    const mapToProps = (state) => {
        return {
          Search: state.Search,
          App: state.App
        }
      }


    export default connect(mapToProps)(Photos);
}

附件Photos.test.js

import React from 'react';
import Enzyme, {shallow} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { Photos } from '../../../Photos/Bike/AccessoriesPhotos';

Enzyme.configure({adapter: new Adapter()});

it('Dialog box opens after click', () => {

  const openDialogButton = shallow(<Photos open="true" close="false" />);

  expect(openDialogButton.text()).toEqual('true');

  openDialogButton.find('input').simulate('change');

  expect(openDialogButton.text()).toEqual('false');
});

这就是我得到的结果。

不变违规:在>“连接(照片)”的上下文或道具中找不到“商店”。要么将根组件包装在 a 中,要么 > 明确地将“store”作为道具传递给“Connect(Photos)”.strong 文本

4

1 回答 1

1

您的Photos组件没有得到 a store,因为您没有在测试中将它作为子组件渲染Provider。如果您也将其包装在 aProvider中进行测试,它将按预期工作。

const openDialogButton = shallow(
  <Provider store={store}>
    <Photos open="true" close="false" />
  </Provider>
);
于 2019-04-05T12:00:31.723 回答