[英]How to find element with enzyme in reactjs app?
In my react app I have a Container component with a couple of selects: 在我的react应用程序中,我有一个带有两个选择的Container组件:
import React, { Component } from "react";
import DebtType from "./DebtType";
import mockOptions from "./mockData.json";
import ClearDebtType from "./ClearDebt";
import { reduxForm, formValueSelector, Field } from "redux-form";
import { connect } from "react-redux";
export class MyContainer extends Component {
handleChangeDebtType = event => {
console.log("handleChangeDebtType value", event.target.value);
this.props.change("debtType", event.target.value);
if (event.target.value === "4" || event.target.value === "5") {
this.props.change("newLimit", 0);
}
if (
event.target.value === "0" ||
event.target.value === "3" ||
event.target.value === "7"
) {
this.props.change("newLimit", this.props.currentLimit);
}
if (
event.target.value === "1" ||
event.target.value === "2" ||
event.target.value === "6"
) {
this.props.change("newLimit", "");
}
};
render() {
const { debtType, newLimit } = this.props;
return (
<div>
<DebtType
options={mockOptions.DEBT_TYPE}
handleChangeDebtType={this.handleChangeDebtType}
/>
{(debtType === "1" || debtType === "2") && (
<ClearDebtType options={mockOptions.CLEARDEBT_TYPE} />
)}
</div>
);
}
}
These are the selectcomponents: 这些是selectcomponents:
import React from "react";
const ClearDebt = ( options) => {
console.log(options)
return (
<select>
{options.options.map(option => {
return <option>{option.label}</option>;
})}
</select>
);
};
export default ClearDebt;
import React from "react";
const DebtType = ({options, handleChangeDebtType}) => {
console.log(options);
return (
<select onChange={handleChangeDebtType}>
{options.map(option => {
return <option value={option.value}>{option.label}</option>;
})}
</select>
);
};
export default DebtType;
In my jest unit test I want to test the visibility of the second select(depending on the selected value for the 1st select): 在我的笑话单元测试中,我想测试第二个选择的可见性(取决于第一个选择的选择值):
describe('Container component', () => {
it('should show the second select component', () => {
const myComp = shallow(<MyContainer debtType={"1"}/>);
//question: how can I find the second select in the browser?
//https://airbnb.io/enzyme/docs/api/ReactWrapper/find.html
const result = myComp.find('#root > div > div > select:nth-child(2)')
console.log('result',result.length)
expect(result.length ).toEqual(1)
});
});
The issue is that I cannot find the element with this statement: 问题是我无法通过以下语句找到该元素:
const result = myComp.find('#root > div > div > select:nth-child(2)')
This is the result of the test: 这是测试的结果:
Container component › should show the second select component
expect(received).toEqual(expected)
Expected: 1
Received: 0
How to select the second listbox? 如何选择第二个列表框? Btw I am open to other test frameworks.
顺便说一句,我愿意接受其他测试框架。
Try 尝试
import React from "react";
import { shallow, configure } from "enzyme";
import { MyContainer } from "./Container";
import Adapter from "enzyme-adapter-react-16";
import ClearDebt from "./ClearDebt";
configure({ adapter: new Adapter() });
describe("Container component", () => {
it("should show the second select component", () => {
const myComp = shallow(<MyContainer debtType={"1"} />);
//question: how can I find the second select in the browser?
//https://airbnb.io/enzyme/docs/api/ReactWrapper/find.html
const result = myComp.find(ClearDebt);
console.log("result", result.props().options.length);
expect(result.props().options.length).toBe(2)
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.