Giter Club home page Giter Club logo

Comments (13)

eddyerburgh avatar eddyerburgh commented on June 25, 2024 121

Yes πŸ™‚

const value = wrapper.find('input').element.value

find traverses DOM nodes and Vue instances. All Wrappers returned by find contain an element and a vnode property. Only Vue instance Wrappers contain a vm object.

from vue-test-utils.

OzzyTheGiant avatar OzzyTheGiant commented on June 25, 2024 52

To add to @eddyerburgh's comment, if using TypeScript you will need to cast the returned element as HTMLInputElement if getting type or compiler errors. This is because .element returns a generic HTMLElement object. So this code would be like the following:

const inputField: HTMLInputElement = wrapper.find('input').element as HTMLInputElement
const value = inputField.value

from vue-test-utils.

LinusBorg avatar LinusBorg commented on June 25, 2024 9

Why use find? wrapper.vm.$refs.emailField.currentValue

from vue-test-utils.

ProfessorTom avatar ProfessorTom commented on June 25, 2024 4

Yes πŸ™‚

const value = wrapper.find('input').element.value

find traverses DOM nodes and Vue instances. All Wrappers returned by find contain an element and a vnode property. Only Vue instance Wrappers contain a vm object.

@eddyerburgh Has this stopped working along the way? I'm having no luck reading my input's value in a Jest test and wonder if I've hit upon a limitation of JSDom.

P.S. Your book, Testing Vue.js Applications has been very helpful getting me up to speed. (Hopefully, I didn't read so fast that retention suffered and caused me to ask a question you already answered in your book.)

from vue-test-utils.

chlab avatar chlab commented on June 25, 2024 1

What if you're using a custom form element, such as this where the actual HTML input field is somewhere inside the component?

<el-input ref="emailField" name="email" v-model="form.email"></el-input>

I have tried giving it a ref and accessing it via vm.find({ ref: 'emailField' }).data.currentValue or via name vm.find('[name=email]').text() (and various other combinations) but I can't ever get the value.

Preferably, I'd rather go "the DOM way" as the test is about seeing if the value is rendered, not if it's set in the component's data.

from vue-test-utils.

chlab avatar chlab commented on June 25, 2024 1

Good point @LinusBorg, that works. Thanks.

from vue-test-utils.

lusarz avatar lusarz commented on June 25, 2024

Yeah, it works :) Thanks!

from vue-test-utils.

lusarz avatar lusarz commented on June 25, 2024

find method returns wrapper. Could you check for: vm.find({ ref: 'emailField' }).vm.currentValue ?

from vue-test-utils.

chlab avatar chlab commented on June 25, 2024

That doesn't work. Also tried with .vnode.currentValue and .element.value all to no avail.

from vue-test-utils.

Bogdan808 avatar Bogdan808 commented on June 25, 2024

Yes slightly_smiling_face

const value = wrapper.find('input').element.value

find traverses DOM nodes and Vue instances. All Wrappers returned by find contain an element and a vnode property. Only Vue instance Wrappers contain a vm object.

@eddyerburgh Has this stopped working along the way? I'm having no luck reading my input's value in a Jest test and wonder if I've hit upon a limitation of JSDom.

P.S. Your book, Testing Vue.js Applications has been very helpful getting me up to speed. (Hopefully, I didn't read so fast that retention suffered and caused me to ask a question you already answered in your book.)

I have the same problem

from vue-test-utils.

afontcu avatar afontcu commented on June 25, 2024

Hi! Can you share a basic example of the scenario where you cannot access input's value?

from vue-test-utils.

soumyadeep589 avatar soumyadeep589 commented on June 25, 2024

Yes slightly_smiling_face

const value = wrapper.find('input').element.value

find traverses DOM nodes and Vue instances. All Wrappers returned by find contain an element and a vnode property. Only Vue instance Wrappers contain a vm object.

@eddyerburgh Has this stopped working along the way? I'm having no luck reading my input's value in a Jest test and wonder if I've hit upon a limitation of JSDom.
P.S. Your book, Testing Vue.js Applications has been very helpful getting me up to speed. (Hopefully, I didn't read so fast that retention suffered and caused me to ask a question you already answered in your book.)

I have the same problem

I am also having the same problem, can anyone tell me the solution around it?

from vue-test-utils.

soumyadeep589 avatar soumyadeep589 commented on June 25, 2024

To add to @eddyerburgh's comment, if using TypeScript you will need to cast the returned element as HTMLInputElement if getting type or compiler errors. This is because .element returns a generic HTMLElement object. So this code would be like the following:

const inputField: HTMLInputElement = wrapper.find('input').element as HTMLInputElement
const value = inputField.value

Thanks for the solution, it is working.

from vue-test-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.