lundi 16 mars 2020

enzyme simulate change event doesnt work over checkbox

I am trying to write a unit test over one of my react components. But my checkbox doesn't change after simulating change event over it.

My component:

const Checkbox = ({ label, onChange, id, isChecked }) => {
    return (
      <>
        <input type="checkbox" 
               name="node" 
               id={"checkbox-" + id}
               onChange={onChange} 
               checked={isChecked}
               />
        <label className="treeview__level" htmlFor={"checkbox-" + id}>{label}</label>
      </>
    )

my test:

it('check if checkbox checked ', () => {
    const   testState = { isChecked: false },
            checkboxWrapper = shallow(
                <Checkbox   label= ""
                            id= "0"
                            onChange={(e) => {
                                testState[e.target.name] = e.target.value;
                            }}
                            isChecked= {testState.isChecked}
                />
            ),
            input = checkboxWrapper.find('input').at(0);

        input.simulate('change', { target: { name: 'checked', value: true } });
        expect(input.props().checked).toBe(true);

    })

Aucun commentaire:

Enregistrer un commentaire