Reactjs &引用;无法读取未定义“的属性包含”;戏谑中的错误
我有一个React组件,用于渲染复选框。组件代码具有如下功能:Reactjs &引用;无法读取未定义“的属性包含”;戏谑中的错误,reactjs,tdd,enzyme,jestjs,Reactjs,Tdd,Enzyme,Jestjs,我有一个React组件,用于渲染复选框。组件代码具有如下功能: const generateCheckboxes = (array, filterType) => { return array.map((filter, i) => { if (!isNullOrUndefined(filter) && filter !== "") { const applied = props.appliedFi
const generateCheckboxes = (array, filterType) => {
return array.map((filter, i) => {
if (!isNullOrUndefined(filter) && filter !== "") {
const applied = props.appliedFilterList[filterType].includes(filter);
console.log("yes", props.appliedFilterList[filterType]);
return (
<Row key={i} noLine={i === 0}>
<Checkbox checked={applied} onClick={() => props.toggleFilter(filterType, filter)}>
{filter}
</Checkbox>
</Row>
);
}
return false;
});
};
return (
<div>
<div>Access</div>
{generateCheckboxes(props.accessFilters, "access")}
<div>Bandwidth</div>
{generateCheckboxes(props.bandwidthFilters, "bandwidth")}
</div>
);
const generateCheckboxes=(数组,过滤器类型)=>{
返回array.map((filter,i)=>{
如果(!isNullOrUndefined(filter)&&filter!==“”){
const applied=props.appliedFilterList[filterType]。包括(筛选器);
log(“yes”,props.appliedFilterList[filterType]);
返回(
props.toggleFilter(filterType,filter)}>
{filter}
);
}
返回false;
});
};
渲染函数如下所示:
const generateCheckboxes = (array, filterType) => {
return array.map((filter, i) => {
if (!isNullOrUndefined(filter) && filter !== "") {
const applied = props.appliedFilterList[filterType].includes(filter);
console.log("yes", props.appliedFilterList[filterType]);
return (
<Row key={i} noLine={i === 0}>
<Checkbox checked={applied} onClick={() => props.toggleFilter(filterType, filter)}>
{filter}
</Checkbox>
</Row>
);
}
return false;
});
};
return (
<div>
<div>Access</div>
{generateCheckboxes(props.accessFilters, "access")}
<div>Bandwidth</div>
{generateCheckboxes(props.bandwidthFilters, "bandwidth")}
</div>
);
返回(
通道
{generateCheckboxes(props.accessFilters,“access”)}
带宽
{generateCheckboxes(props.bandwidthFilters,“带宽”)}
);
现在我正试图为这个组件编写一个测试,我只是通过这个组件和酶的浅层方法中的道具。我只是想通过在测试用例中传递一些模拟数据来检查组件是否正确呈现。测试结果如下所示:
import React from "react";
import { mount, shallow } from "enzyme";
import Checkbox from "../../lib/Checkbox";
import FilterDropdownContent, { Header } from "../components/FilterDropdownContent";
import { generateCheckboxes } from "../components/FilterDropdownContent";
import { access } from "fs";
const accessData = ["Access Type Of The Service"];
const bandwidthData = ["the allowed band width", "the allowed band width"];
const termsFiltersData = ["term associated with the service"];
const appliedFilters = [
{
access: ["Access Type Of The Service"],
bandwidth: ["the allowed band width", "the allowed band width"],
term: ["term associated with the service"]
}
];
describe("test the FilterDropdown component", () => {
it("renders correctly", () => {
const wrapper = mount(
<FilterDropdownContent
accessFilters={accessData}
bandwidthFilters={bandwidthData}
appliedFilterList={appliedFilters}
termsFilters={termsFiltersData}
toggleFilter={false}
/>
);
});
});
从“React”导入React;
从“酶”中导入{mount,shallow};
从“../../lib/Checkbox”导入复选框;
从“./components/FilterDropdownContent”导入FilterDropdownContent,{Header};
从“./components/FilterDropdownContent”导入{GenerateCheckBox};
从“fs”导入{access};
const accessData=[“服务的访问类型”];
常量带宽数据=[“允许的带宽”,“允许的带宽”];
const termsFiltersData=[“与服务关联的术语”];
常量appliedFilters=[
{
访问:[“服务的访问类型”],
带宽:[“允许的带宽”,“允许的带宽”],
期限:[“与服务相关的期限”]
}
];
描述(“测试过滤器下拉组件”,()=>{
它(“正确渲染”,()=>{
常量包装器=装入(
);
});
});
但我的测试失败,错误如下:
TypeError:无法读取未定义的属性“includes”
includes属性位于generateCECKBoxes()函数中。我已在测试用例中通过了所需的道具。但我不确定是什么导致了问题。有人能帮我解决这个问题吗?因为AppliedFilter是一个数组:
const appliedFilters = [
{
access: ["Access Type Of The Service"],
bandwidth: ["the allowed band width", "the allowed band width"],
term: ["term associated with the service"]
}
];
这部分代码将解析为未定义的代码:
props.appliedFilterList[filterType]
因为filterType=='access'
而不是数组索引
尝试将appliedFilters
更改为:
const appliedFilters = {
access: ["Access Type Of The Service"],
bandwidth: ["the allowed band width", "the allowed band width"],
term: ["term associated with the service"]
};
因为appliedFilters是一个数组:
const appliedFilters = [
{
access: ["Access Type Of The Service"],
bandwidth: ["the allowed band width", "the allowed band width"],
term: ["term associated with the service"]
}
];
这部分代码将解析为未定义的代码:
props.appliedFilterList[filterType]
因为filterType=='access'
而不是数组索引
尝试将appliedFilters
更改为:
const appliedFilters = {
access: ["Access Type Of The Service"],
bandwidth: ["the allowed band width", "the allowed band width"],
term: ["term associated with the service"]
};
使用Jest时,如果使用toEqual而不是toMatchObject,则也可能出现错误
TypeError:Cannot read未定义的属性“includes”
以下代码将给出未定义的无法读取属性“includes”错误:
test(“应该返回正确的数据”,async()=>{
const response=wait fetch({url:http://localhost/" });
期待(回应)({
正文:新Blob([“原始数据]),
现状:200,
状态文本:“确定”,
});
});
改用toMatchObject
将修复它:
/。。。
期望(响应)。toMatchObject({
// ...
使用Jest时,使用toEqual而不是toMatchObject时,也会出现错误TypeError:无法读取未定义的属性“includes”
以下代码将给出未定义的无法读取属性“includes”错误:
test(“应该返回正确的数据”,async()=>{
const response=wait fetch({url:http://localhost/" });
期待(回应)({
正文:新Blob([“原始数据]),
现状:200,
状态文本:“确定”,
});
});
改用toMatchObject
将修复它:
/。。。
期望(响应)。toMatchObject({
// ...
Wow,真是太好了。非常感谢。我已经坚持了几个小时。你救了我的命。再次非常感谢:)哇,真是太好了。非常感谢。我坚持了几个小时。你救了我的命。再次非常感谢:)