David
David

Reputation: 345

Using Vue test-utils and jest to test the value of a specific input element

Using Vue test-utils, I'm needing to test the value of a specific input element which is populated via a prop. I've figured out how to find the element using it's id:

  it("Address should render Street 1", () => {
     const street1 = wrapper.find('#street1')              // this works
     expect(street1).toEqual('223 Some Street Rd')         // nope
     expect(street1.text()).toEqual('223 Some Street Rd')  // seemed good at the time
     expect(street1.value()).toEqual('223 Some Street Rd') // grasping at straws - no love 
  });

but I've not been able to figure out how to access the "value" of the input.

Any help is appreciated in advance

Upvotes: 4

Views: 11547

Answers (2)

Tony
Tony

Reputation: 1432

According to the vue-test-utils documentation, you can add props to a component using propsData or setProps

import { mount } from '@vue/test-utils'
import Foo from './Foo.vue'

const wrapper = mount(Foo, {
  propsData: {
    myProp: '223 Some Street Rd' // I am assuming the prop's value is '223 Some Street Rd'
  }
})
const street1 = wrapper.find('#street1') 
expect(street1.text()).toBe('223 Some Street Rd'); //.toEqual should also work

Or Alternatively

import { mount } from '@vue/test-utils';
import Foo from './Foo.vue';

const wrapper = mount(Foo);
wrapper.setProps({ myProp: '223 Some Street Rd' }) // I am assuming the prop's value is '223 Some Street Rd'
const street1 = wrapper.find('#street1') 
expect(street1.text()).toBe('223 Some Street Rd'); //.toEqual should also work

Upvotes: 0

logee
logee

Reputation: 5077

I am assuming your component looks something like this:

 <input id="street1" type="text" v-model="street1" >

then you can try

   expect(street1.element.value).toEqual('223 Some Street Rd');

E.g.

import { mount } from "@vue/test-utils";
import Input from "./Input";

describe("Input", () => {
  const mountInput = (propsData = {}) => {
    return mount(Input, {
      propsData
    });
  };

  it("Address should render Street 1", () => {
    const wrapper = mountInput();
    const street1 = wrapper.find('#street1') 
    expect(street1.element.value).toEqual('223 Some Street Rd');
  });
});

Upvotes: 9

Related Questions