I have a HOC, adding some props to the component, for handling network requests and passing down data as prop. The following is a very simplified version of the HOC:
export const withTags = (Component) => {
class WithTags extends PureComponent {
getItems() {
return getTags({
search: this.state.searchTerm
})
.then((items) => this.setState({
items
}));
}
.
.
.
render() {
return (
<Component
{...this.props}
items={this.state.items}
getItems={this.getItems}
/>
);
}
}
return withTags;
}
Using enzyme I can easily do something like:
it('should return tags', async () => {
const mockComponent = jest.fn(() => null);
const WithTagsComponent = withTags(mockComponent);
const wrapper = shallow(<WithTagsComponent />);
const res = await wrapper.props().getItems();
expect(res).toEqual(getTagsResponseMock);
expect(tagsApi.getTags).toHaveBeenCalledTimes(1);
expect(tagsApi.getTags).toHaveBeenCalledWith({
limit: PAGE_SIZE,
});
expect(wrapper.props().items).toEqual([tagFlowMock]);
});
But this approach won't work in React Testing Library, as we should test from the end-user's perspective, and not access props. So, how should I test such HOCs using React Testing Library?
itemsandgetItemsprops and does something testable, i.e. some interaction like displaying the items and clicking a button to get new items. Can you provide a more complete HOC code example?