Reputation: 4264
I've got a childComponent
that has text.
I've written a test
that is checking for that text. That test passed until I made two notable changes. I need to keep those changes, and get the test to pass.
forwardRef
parentComponent.childComponent = childComponent
The test works fine when the parentComponent does not use forwardRef
I can see the text in the childComponent
when I print the html() of the parent in the console.
HTML:
<div class="parentComponent">
<div class="childComponent">
childText
</div>
</div>
parentComponent:
class SelectRef extends PureComponent {
/// component code here
}
// Bottom of parentComponent
const Select = forwardRef((props, ref) => <SelectRef {...props} innerRef={ref} />);
Select.displayName = "Select";
Select.Option = Option;
export default Select;
I've tried:
expect(component.dive().text()).to.contain("text");
expect(component.shallow().text()).to.contain("text");
expect(component.prop('children')).to.contain("text");
What are other ways to get the childComponent text in a component using Enyzme / React?
Upvotes: 1
Views: 4945
Reputation: 4264
Here's a way to do this when using forwardRef:
I have a helper function in the enzyme test that allows you to access the class function in the forwardRef wrapper:
const getComp = (comp, className = "default-class") =>
comp
.find("ComponentRef")
.dive()
.find(className);
Example test:
it("should render the place holder text", () => {
const component = shallow(
<Component placeholderText="Select an option" selectName="test"/>
);
const comp = getComp(component)
expect(comp.text()).to.contain("inner text of component");
});
Upvotes: 2