В моем приложении для реагирования у меня есть компонент 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 {
handleChangeEventDebtType = event => {
console.info("handleChangeEventDebtType 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}
handleChangeEventDebtType = {this.handleChangeEventDebtType}
/>
{(debtType === "1" || debtType === "2") && (
<ClearDebtType options = {mockOptions.CLEARDEBT_TYPE} />
)}
</div>
);
}
}
Это выбранные компоненты:
import React from "react";
const ClearDebt = ( options) => {
console.info(options)
return (
<select>
{options.options.map(option => {
return <option>{option.label}</option>;
})}
</select>
);
};
export default ClearDebt;
import React from "react";
const DebtType = ({options, handleChangeEventDebtType}) => {
console.info(options);
return (
<select onChange = {handleChangeEventDebtType}>
{options.map(option => {
return <option value = {option.value}>{option.label}</option>;
})}
</select>
);
};
export default DebtType;
В моем модульном тесте шутки я хочу проверить видимость второго выбора (в зависимости от выбранного значения для 1-го выбора):
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.info('result',result.length)
expect(result.length ).toEqual(1)
});
});
Проблема в том, что я не могу найти элемент с этим оператором:
const result = myComp.find('#root > div > div > select:nth-child(2)')
Это результат теста:
Container component › should show the second select component
expect(received).toEqual(expected)
Expected: 1
Received: 0
Как выбрать второй список? Кстати, я открыт для других тестовых фреймворков.



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Пытаться
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.info("result", result.props().options.length);
expect(result.props().options.length).toBe(2)
});
});