Error shallow Jest Enzyme React cannot find "store"

Anon

I have an interesting error in testing my component and don't now to fix(

I use jest, enzyme, react, redux.

In front end part of the app all is working correctly.

What error I can see now:

  ● 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 | });

my setupTests.js:

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);
};

My Test for component:

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  />);
    });
  });
});

Also I can send my component:

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);

Can somebody tell me what I do incorrectly? I think something is wrong with {shallow}. Maybe someone knows how to fix this error?

Thank you so much!

slideshowp2

You need to create a mocked store and pass it to the component as its props. connect(mapStatesToProps)(Body) statement will create a wrapped component. So you need to use wrapper.find(Body) of enzyme to get the Body stateless functional component.

index.tsx:

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:

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);
    });
  });
});

Unit test result:

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

Here is the completed demo: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/57942218

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to solve an Enzyme/Jest error "Cannot find module 'react' from 'react-router.js'" in a React project

Jest Enzyme test error :Could not find "store" in the context of "Connect(App)"

Cannot read properties of undefined (reading 'find') in Jest and Enzyme React 17

Jest/Enzyme shallow testing React stateless component - wrapper.find() not working

React jest test with enzyme error

Testing React with enzyme and jest giving error Cannot find module '@babel/polyfill/lib/noConflict' from 'index.js'

Jest/Enzyme Unit Testing: How to pass store to shallow component that uses redux 4 and react-redux 6 connect function

Jest enzyme shallow test is not rendering all elements of the React component

in jest test enzyme.shallow throws Unexpected token < error

Jest enzyme shallow unexpected token <

Setting up Jest and Enzyme to test React 15 cannot find module react/lib/ReactTestUtils

react jest and enzyme, find 'option' with hidden attribute

Jest & Enzyme test error with React Context API

React / Enzyme: Invariant Violation error when running Jest / Enzyme test

Enzyme/Jest React Testing - Shallow connected component with react-redux > 6

React Enzyme Jest error jest.fn() should be called

travis cannot build because Error: Cannot find module 'react-test-renderer/shallow'

How to use Enzyme Shallow with Jest snapshots

Jest fails with error: Cannot find module 'react/lib/ReactComponentTreeHook'

How to input text into form during React/Jest/Enzyme Shallow Rendering testing?

Using React Jest Enzyme - How to pass a constructor object to my component with Shallow?

TypeError: Cannot read property 'scrollIntoView' of null - react. jest enzyme

Cannot read property 'displayName' of undefined. React+Jest+Enzyme

"Cannot read property includes of undefined" error in Jest and Enzyme

How to find count of a div containing specific value in React, Jest and Enzyme

Jest/Enzyme Shallow testing RFC - not firing jest.fn()

Shallow render with enzyme can't find elements

Testing React Router with Jest and Enzyme

Test a form in React with jest and enzyme

TOP Ranking

HotTag

Archive