.prop(key) => Any
Возвращает значение свойства prop для корневого узла обёртки с предоставленным ключом. Это должна быть обёртка с единственным узлом.
ПРИМЕЧАНИЕ: При вызове на поверхностной обёртке .prop(key) вернёт значения свойств для корневого узла, который компонент отображает, а не сам компонент. Чтобы вернуть свойства для всего React-компонента, используйте wrapper.instance().props. См. .instance() => ReactComponent
Аргументы
-
key(String): Название свойства, то естьthis.props[key]илиprops[key]для корневого узла обёртки.
Пример
import PropTypes from 'prop-types';
import ValidateNumberInputComponent from './ValidateNumberInputComponent';
class MyComponent extends React.Component {
constructor(...args) {
super(...args);
this.state = {
number: 0,
};
this.onValidNumberInput = this.onValidNumberInput.bind(this);
}
onValidNumberInput(e) {
const number = e.target.value;
if (!number || typeof number === 'number') {
this.setState({ number });
}
}
render() {
const { includedProp } = this.props;
const { number } = this.state;
return (
<div className="foo bar" includedProp={includedProp}>
<ValidateNumberInputComponent onChangeHandler={onValidNumberInput} number={number} />
</div>
);
}
}
MyComponent.propTypes = {
includedProp: PropTypes.string.isRequired,
};
const wrapper = shallow(<MyComponent includedProp="Success!" excludedProp="I'm not included" />);
expect(wrapper.prop('includedProp')).to.equal('Success!');
const validInput = 1;
wrapper.find('ValidateNumberInputComponent').prop('onChangeHandler')(validInput);
expect(wrapper.state('number')).to.equal(number);
const invalidInput = 'invalid input';
wrapper.find('ValidateNumberInputComponent').prop('onChangeHandler')(invalidInput);
expect(wrapper.state('number')).to.equal(0);
// Warning: .prop(key) only returns values for props that exist in the root node.
// See the note above about wrapper.instance().props to return all props in the React component.
console.log(wrapper.prop('includedProp'));
// "Success!"
console.log(wrapper.prop('excludedProp'));
// undefined
console.log(wrapper.instance().props.excludedProp);
// "I'm not included"
Связанные методы
© 2015 Airbnb, Inc.
Licensed under the MIT License.
https://enzymejs.github.io/enzyme/docs/api/ShallowWrapper/prop.html