.instance() => ReactComponent
Возвращает экземпляр базового класса узла обертки одноузлового объекта; this в его методах.
ПРИМЕЧАНИЕ: может вызываться только для экземпляра обертки, который также является корневым экземпляром. С React 16 и выше, instance() возвращает null для компонент без состояния функционального типа.
Возвращаемые значения
ReactComponent|DOMComponent: Полученный экземпляр.
Пример
function Stateless() {
return <div>Stateless</div>;
}
class Stateful extends React.Component {
render() {
return <div>Stateful</div>;
}
}
React 16.x
test('shallow wrapper instance should be null', () => {
const wrapper = shallow(<Stateless />);
const instance = wrapper.instance();
expect(instance).to.equal(null);
});
test('shallow wrapper instance should not be null', () => {
const wrapper = shallow(<Stateful />);
const instance = wrapper.instance();
expect(instance).to.be.instanceOf(Stateful);
});
React 15.x
test('shallow wrapper instance should not be null', () => {
const wrapper = shallow(<Stateless />);
const instance = wrapper.instance();
expect(instance).to.be.instanceOf(Stateless);
});
test('shallow wrapper instance should not be null', () => {
const wrapper = shallow(<Stateful />);
const instance = wrapper.instance();
expect(instance).to.be.instanceOf(Stateful);
});
© 2015 Airbnb, Inc.
Licensed under the MIT License.
https://enzymejs.github.io/enzyme/docs/api/ShallowWrapper/instance.html