Reputation: 120
I have the following onClick function
onClickFunction= index => () => {
this.setState({ activeIndex: index });
}
Which is used in the following function
getPersons= (persons) => persons
.map((person, index) => (
<StyledComponentHeader
key...
...
onClick={this.onClickFunction(index)}
>
<Styled2
...
>
{person.name}
</Styled2>
</StyledComponentHeader>
))
in my unit test I need to test the onClickFunction
. currently I have something like this
const component= (
<StyledComponent
persons={persons}
/>);
describe('...', () => {
beforeEach(() => {
wrapper = shallow(component);
});
it('testing name', () => {
expect(wrapper).toMatchSnapshot();
expect(wrapper.state().activeIndex).toEqual(0);
wrapper.instance().onClickFunction(1);
expect(wrapper.state().activeIndex).toEqual(1);
});
it returns saying that the above last except
should be 0. What is the correct way to test this function?
Upvotes: 0
Views: 70
Reputation: 196217
Since onClickFunction
does not change the state, but returns a function which if called then changes the state, the logical way would be to test it with
wrapper.instance().onClickFunction(1)();
(notice the added ()
at the end to invoke the returned function)
Upvotes: 1