[英]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.