Testing onChange function in Jest

前端 未结 5 2056
悲哀的现实
悲哀的现实 2021-02-01 04:07

I\'m relatively new to Jest and testing in general. I have a component with an input element:

import * as React from \"react\";

export interface inputProps{
            


        
5条回答
  •  星月不相逢
    2021-02-01 04:35

    I figured out the solution.

    So, instead of passing in the value inside InputBox, we have to pass it inside the second param of simulate as shown below. Then we simply check for equality against the first arg of the first call to the mockFn. Also, we can get rid of the event.preventDefault();

    it("onChange param is the same value as the input element's value property", () => {
        const mockFn = jest.fn();
        const input = enzyme.shallow();
    
        input.find('input').simulate('change', {target: {value: 'matched'} });
        expect(mockFn.mock.calls[0][0]).toBe('matched');
    });
    

提交回复
热议问题