0

I want to write unit test for these functions.

    enable () {
             this.setState({
              show: true
                });
                this.showValue();
            }
    
       showValue () {
            const { model } = this.props;
            model(A).onChange({ target: { value: '' } });
        }

isChecked () {
        if ((this.state.show)) {
            if (!this.state.show) {
                this.enable();
            }
            return true;
        } else {
            return false;
        }
    }

My test looks like this

it('isChecked', () => {
        const wrapper = shallow(<Component/>);
        const instance = wrapper.instance();
        const mockSetStateFn = jest.fn();
        instance.setState = mockSetStateFn;
        instance.setState({ show: false });
        instance.isChecked();
        instance.enable();
        expect(instance.isChecked).toHaveBeenCalledWith();
        const showValue= spyOn(wrapper.instance(), 'onChange');
        wrapper.find(model(A)).simulate('change', { target: { value: '' } });
        expect(instance.isChecked).equals('');
    });

I am getting error that onchange is not a function. I am new to unit tests. How do i write unit test for these three functions.

komal
  • 43
  • 6
  • You don't seem to be passing _any_ `model` prop. `A` is undefined entirely. Also that's generally a horrible way to test a component - don't mock parts of the thing you're supposed to be testing, test it through its public interface (props and rendered DOM, **not** state and internal methods). – jonrsharpe Jun 07 '22 at 10:32
  • How do i do it? I am new to unit test. Can you help me out with it – komal Jun 08 '22 at 07:59
  • Pass the component the props it needs when you render it. Interact with it by simulating e.g. clicks on elements. Assert on how the DOM should look to the user. You haven't shown enough of the thing you're supposed to be testing to be more specific than that. But also this isn't a code- (or test-) writing service, you should look for relevant learning materials (e.g. I've written some stuff on JS testing starting here: https://blog.jonrshar.pe/2020/Aug/31/js-tdd-ftw.html). – jonrsharpe Jun 08 '22 at 08:02

0 Answers0