Comments (13)
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.
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.
Why use find? wrapper.vm.$refs.emailField.currentValue
from vue-test-utils.
Yes π
const value = wrapper.find('input').element.value
find
traverses DOM nodes and Vue instances. All Wrappers returned byfind
contain anelement
and avnode
property. Only Vue instance Wrappers contain avm
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.
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.
Good point @LinusBorg, that works. Thanks.
from vue-test-utils.
Yeah, it works :) Thanks!
from vue-test-utils.
find
method returns wrapper. Could you check for: vm.find({ ref: 'emailField' }).vm.currentValue
?
from vue-test-utils.
That doesn't work. Also tried with .vnode.currentValue
and .element.value
all to no avail.
from vue-test-utils.
Yes slightly_smiling_face
const value = wrapper.find('input').element.value
find
traverses DOM nodes and Vue instances. All Wrappers returned byfind
contain anelement
and avnode
property. Only Vue instance Wrappers contain avm
object.@eddyerburgh Has this stopped working along the way? I'm having no luck reading my
input
'svalue
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.
Hi! Can you share a basic example of the scenario where you cannot access input's value
?
from vue-test-utils.
Yes slightly_smiling_face
const value = wrapper.find('input').element.value
find
traverses DOM nodes and Vue instances. All Wrappers returned byfind
contain anelement
and avnode
property. Only Vue instance Wrappers contain avm
object.@eddyerburgh Has this stopped working along the way? I'm having no luck reading my
input
'svalue
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.
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)
- When setProps() is called on a wrapper which renders a slot, the slot's content element gets destroyed
- `RouterLink.useLink` is missing when using `RouterLinkStub`. HOT 3
- Test child component, which is the root element HOT 3
- Computed value does not update when data updates
- isVisible() not seeing display: none using vitest with jsdom HOT 4
- Testing component with onBeforeRouteLeave hook raising "No active route record was found" warning HOT 1
- vue2 εΌε ₯ element2. ι‘΅ι’εε¨table θ‘¨ζ Ό jest ζ΅θ―ζ₯ι HOT 1
- *.svg?inline issues
- Errors in `setup()` are silent if `render()` throws an error HOT 6
- Check style changes on hover HOT 1
- setData on writable computed property does not seem to work as it does in VTU.1
- classes documentation
- Support Vue 2.7.16 HOT 2
- Vue test utils not updating template when testing composables
- TypeError: $setup.mergeProps is not a function
- Data property is already defined in Props. HOT 1
- Unable to use vitest spy within vue/nuxt 3 component methods. HOT 3
- `findComponent` doesn't find anything when using `@vue/compat` HOT 1
- Components with TS generic HOT 1
- event.keyCode is deprecated
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-test-utils.