0
votes

I am implementing unit test cases for my vue application with jest

I have a situation where i need to test a input field in a child component.

<parent-component>
    <child-component>
       <input type="text" v-model="inputValue" />
    </child-component>
</parent-component>

My test goes like below

it('check empty validation', () => {
   const wrapper = mount(parentComponent, {
            propsData: {
             test:test
            }
   });
   wrapper.find(childComponent).vm.inputValue = "";
   expect(wrapper.vm.errorMessage).toBe("cannot be empty");
});

But setting model doesnt seems to be working.

How to set value to text box and test the same is my question

Thanks

2

2 Answers

1
votes

You can use the Vue Test Utils setValue method on the element:

it('check empty validation', () => {
  const wrapper = mount(parentComponent, {
    propsData: {
      test: test
    }
  })

  wrapper.find('input').setValue('')

  expect(wrapper.vm.errorMessage).toBe('cannot be empty')
})

This sets the element value property and forces the model to update.