首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误store酶反应找不到"store“

错误store酶反应找不到"store“
EN

Stack Overflow用户
提问于 2019-09-15 15:59:55
回答 1查看 1.4K关注 0票数 1

我在测试我的组件时遇到了一个有趣的错误,现在不需要修复(

我用jest,酶,react,redux

在应用程序的前端部分,一切都运行正常。

我现在可以看到什么错误:

代码语言:javascript
复制
  ● Body Component › Body Component is loading › Is loading

    Invariant Violation: Could not find "store" in the context of "Connect(Body)". Either wrap the root component in a <Provider>, or pass a custom React context provider to <Provider> and the corresponding React context consumer to Connect(Body) in connect options.

      25 |       };
      26 | 
    > 27 |       const BodyComponent = shallow(<Body />);
         |                             ^
      28 |     });
      29 |   });
      30 | });

我的setupTests.js:

代码语言:javascript
复制
import Enzyme, { shallow, render, mount } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
//import toJson from 'enzyme-to-json'

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

global.shallow = shallow;
global.render = render;
global.mount = mount;
//global.toJson = toJson

// Fail tests on any warning
console.error = message => {
  throw new Error(message);
};

我的组件测试:

代码语言:javascript
复制
import React from "react";
import Body from "./Body";
import  { shallow, render, mount } from "enzyme";

describe("Body Component", () => {
  const props = {
    data: {
      time: undefined,
      products: [],
      filteredProducts: [],
      search: undefined
    }
  };

  describe("Body Component is loading", () => {
    it("Is loading", () => {
      const nextProps = {
        ...props,
        data: {
          products: [{}, {}],
          filteredProducts: [{ name: "sdffd" }, { name: "sdf" }],
          search: "sdf"
        }
      };

      const BodyComponent = shallow(<Body  />);
    });
  });
});

我也可以发送我的组件:

代码语言:javascript
复制
import React from "react";
import ProductListItem from "./ProductListItem";
import { connect } from "react-redux";

const names = ["Seattle", "Bellevue", "Tacoma", "Puyallup"];

const Body = props => {
  const { products, filteredProducts, search } = props;
  console.log("FILTERED IN BODY", filteredProducts);
  console.log(products);
  return (
    <div className="main">
      {names.map(name => (
        <ProductListItem
          name={name}
          data={search ? filteredProducts : products}
          key={Math.random()}
        />
      ))}
    </div>
  );
};

const mapStatesToProps = state => {
  return {
    products: state.data.products,
    filteredProducts: state.data.filteredProducts,
    search: state.data.search
  };
};

export default connect(mapStatesToProps)(Body);

谁能告诉我我做错了什么?我觉得{浅}出了点问题。也许有人知道如何修复这个错误?

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-15 19:20:24

您需要创建一个模拟的store,并将其作为其道具传递给组件。connect(mapStatesToProps)(Body)语句将创建一个包装的组件。因此,您需要使用enzymewrapper.find(Body)来获取Body无状态功能组件。

index.tsx

代码语言:javascript
复制
import React from 'react';
import ProductListItem from './ProductListItem';
import { connect } from 'react-redux';

const names = ['Seattle', 'Bellevue', 'Tacoma', 'Puyallup'];

export const Body = props => {
  const { products, filteredProducts, search } = props;
  console.log('FILTERED IN BODY', filteredProducts);
  console.log(products);
  return (
    <div className="main">
      {names.map(name => (
        <ProductListItem name={name} data={search ? filteredProducts : products} key={Math.random()} />
      ))}
    </div>
  );
};

const mapStatesToProps = state => {
  return {
    products: state.data.products,
    filteredProducts: state.data.filteredProducts,
    search: state.data.search
  };
};

export default connect(mapStatesToProps)(Body);

index.spex.tsx

代码语言:javascript
复制
import React from 'react';
import ConnectedBody, { Body } from './';
import { shallow } from 'enzyme';
import configureMockStore from 'redux-mock-store';

const initialState = {
  data: {
    time: undefined,
    products: [],
    filteredProducts: [],
    search: undefined
  }
};
const mockStore = configureMockStore();
const store = mockStore(initialState);

describe('Body Component', () => {
  const props = {
    data: {
      time: undefined,
      products: [],
      filteredProducts: [],
      search: undefined
    }
  };

  describe('Body Component is loading', () => {
    it('Is loading', () => {
      const nextProps = {
        ...props,
        data: {
          products: [{}, {}],
          filteredProducts: [{ name: 'sdffd' }, { name: 'sdf' }],
          search: 'sdf'
        }
      };

      const wrapper = shallow(<ConnectedBody store={store} />);
      const BodyComponent = wrapper.find(Body);
      expect(BodyComponent.prop('products')).toEqual(props.data.products);
      expect(BodyComponent.prop('filteredProducts')).toEqual(props.data.filteredProducts);
      expect(BodyComponent.prop('search')).toEqual(props.data.search);
    });
  });
});

单元测试结果:

代码语言:javascript
复制
PASS  src/stackoverflow/57942218/index.spec.tsx
  Body Component
    Body Component is loading
      ✓ Is loading (37ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        4.771s, estimated 5s

下面是完成的演示:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/57942218

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57942218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档