简体   繁体   English

如何在React TDD中使用Jest和Enzyme测试功能是否更改了组件的状态

[英]How to test if state of a component is changed by the function using jest and Enzyme in React TDD

I have a function which is used to change the state of the component to empty arrays. 我有一个函数,用于将组件的状态更改为空数组。 I want to test this function. 我想测试这个功能。 My function looks like this: 我的函数如下所示:

clearAppliedFilters = () => {
  this.setState({
    appliedFilterList: {
      access: [],
      bandwidth: [],
      term: [],
    },
  });
};

I am writing a test case as shown below for this function: 我正在为此功能编写如下所示的测试用例:

it('checks clearAppliedFilters function', () => {
  const wrapper = shallow(
    <ProductList
      headerText="Hello World"
      productList={data}
      paginationSize="10"
      accessFilters={['a 1', 'a 2']}
      bandwidthFilters={['b 1', 'b 2']}
      termsFilters={['t 1', 't 2']}
      appliedFilterList={appliedFilter}
    />,
  );
  wrapper.setState({
    appliedFilterList: {
      access: [],
      bandwidth: [],
      term: [],
    },
  });

  expect(wrapper.instance().clearAppliedFilters()).toBe();
});

But I am not able to see my test running. 但是我看不到我的测试正在运行。 So what may be the issue? 那么可能是什么问题呢? Can anyone please help me if I this looks wrong. 如果我看错了,谁能帮我。 I am new to TDD and I am struggling to write these tests. 我是TDD的新手,正在努力编写这些测试。 Can someone please suggest how to proceed? 有人可以建议如何进行吗?

Here is the working test case, this might help you further: 这是有效的测试用例,可能会进一步帮助您:

https://codesandbox.io/s/xr9295ov3q https://codesandbox.io/s/xr9295ov3q

import React from "react";
import { shallow } from "enzyme";
import ProductList from "./ProductList";

    describe("ProductList Components", () => {
      it("should update correct ProductList state", () => {
        let wrapper = shallow(<ProductList />);

        wrapper.instance().clearAppliedFilters();

        expect(wrapper.state().appliedFilterList.access.length).toBe(0);
        expect(wrapper.state().appliedFilterList.bandwidth.length).toBe(0);
        expect(wrapper.state().appliedFilterList.term.length).toBe(0);
      });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React with Jest(无酶)中测试功能组件的 state - How to test the state of a functional component in React with Jest (No Enzyme) 如何在此组件中测试 filterBooks function? (反应 - 开玩笑 - 酶) - How to test filterBooks function in this component?? (React - Jest - Enzyme ) 如何使用玩笑和酶测试异步数据获取反应组件? - How to test async data fetching react component using jest and enzyme? 如何使用 Jest/Enzyme 在功能性 React 组件中测试 lambda 函数? - How to test lambda functions in a functional React Component using Jest/Enzyme? 如何在反应中使用 jest 和酶对高阶组件进行单元测试 - How to unit test a higher order component using jest and enzyme in react 如何使用 Jest + Enzyme 覆盖 Svg 反应组件的测试用例 - How to cover test cases for Svg react component using Jest + Enzyme 如何使用Jest和Enzyme为简单的React组件编写测试用例 - How to write a test case for a simple React component using Jest and Enzyme 如何使用 Jest 和 Enzyme 在 React 中测试 OnSubmit - how to test OnSubmit in React using Jest and Enzyme 使用 Jest/Enzyme 在 React 功能组件中测试封闭组件 - Test enclosing component inside a React functional component using Jest/Enzyme Jest/Enzyme Test 在 React 功能组件中将 Child prop 触发为 function - Jest/Enzyme Test firing Child prop as function in react functional component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM