vendredi 1 novembre 2019

Jest mock function not being called

I have the following component:

`const shallowComponent = (props = {}) => shallow(<myComponent {...defaultProps} {...props} />);

Which renders this markup when using instance.debug():

<div className="myDiv">
    <ICWButtonGroup id="font-alignment-options" className="paragraph-style-alignment-options" labels=>
     <ICWButtonStateful active={true} onClick={[Function: onClick]} />
        <ICWButtonStateful onClick={[Function: onClick]} />
        <ICWButtonStateful onClick={[Function: onClick]} />
    </ICWButtonGroup>
    <ICWInput label="LINE_HEIGHT" value="" onBlur={[Function: onBlur]} onChange={[Function: onChange]} type="text" />
    <ICWInput label="LETTER_SPACING" value="" onBlur={[Function: onBlur]} onChange={[Function: onChange]} type="text" />
</div>

My test:

it('Should execute onChange when called', () => {
    const props = {
     onChange: jest.fn()
    };
    const instance = shallowComponent(props);
    const lineHeightInput = instance.find('ICWInput[label="LINE_HEIGHT"]');
    lineHeightInput.prop('onChange')(undefined, { value: '123' });
    console.log(instance.debug());
    expect(props.onChange).toBeCalled();
});

But when I run this test it says onChange was not called. This is frustrating because I have an identical component elsewhere and this exact test, almost the same line by line, works flawlessly. Howver in the other component I can see the onChange={[Function: mockConstructor]} throughout the markup, but in this failing test my mock function does not seem to be registered anywhere. Why?

Aucun commentaire:

Enregistrer un commentaire