Replies: 9 comments 16 replies
-
I like the first one - I also think the default would be I am personally not a fan of adding another mounting options (like Not entirely clear on the merits of (2), so (1) is my favorite. |
Beta Was this translation helpful? Give feedback.
-
I read the proposals and I preferred the first one too. Calling setProps in the wrapper is exactly like the vue functionality, so we can be more confident about our tests without any extra code. |
Beta Was this translation helpful? Give feedback.
-
Seems this is solved here? https://github.com/vuejs/vue-test-utils-next/pull/393/files |
Beta Was this translation helpful? Give feedback.
-
const wrapper = mount({
components: { Increment },
template: `<increment v-model="value" />`,
data: () => ({ value: 11 }),
})
expect(wrapper.text()).toBe('Value: 11Increment')
wrapper.find('button').element.click()
expect(wrapper.vm.value).toBe(12)
await nextTick()
expect(wrapper.text()).toMatch('Value: 12') |
Beta Was this translation helpful? Give feedback.
-
Another suggestion: #440 (comment). Similar to #3 but not using a Thoughts? If we can agree on this, I think we can implement it. Maybe @nekosaur @pikax @KaelWD? |
Beta Was this translation helpful? Give feedback.
-
This discussion is over couple of PRs and I'm not sure where it was suggested that currently we could use: However there is an issue with that when an immediate watcher updates the modelValue on mount. When the mount looks like: const wrapper = mount(MyComp, {
props: {
options,
modelValue: null,
'onUpdate:modelValue': async (modelValue: any) => await wrapper.setProps({ modelValue })
multi: true
}
}); (the watcher updates the modelValue to Here the |
Beta Was this translation helpful? Give feedback.
-
How about this approach? const wrapper = mount(Increment, {
propsData: { value: 11 },
})
wrapper.vm.$on('input', (value) => wrapper.setProps({ value }))
expect(wrapper.text()).toBe('Value: 11Increment')
wrapper.find('button').element.click()
expect(wrapper.vm.value).toBe(12)
await nextTick()
expect(wrapper.text()).toMatch('Value: 12') |
Beta Was this translation helpful? Give feedback.
-
I was trying to understand how to properly test Quasar UI components which rely on v-model with Cypress Component Testing and I found this thread export function vModelAdapter (modelRef, modelName = 'modelValue') {
watch(modelRef, (value) => Cypress.vueWrapper.setProps({ [ modelName ]: value }))
return {
[ modelName ]: modelRef.value,
[ `onUpdate:${ modelName }` ]: (emittedValue) => {
modelRef.value = emittedValue
}
}
} const model = ref(null)
mount(QSelect, {
props: {
...vModelAdapter(model),
options
}
}) |
Beta Was this translation helpful? Give feedback.
-
I also needed this functionality, but I am using Vitest and don't have access to mounted component inside const render = (cmp: any, options: MountOptions = {}) => {
const vModelProps: [string, Ref][] = []
const vModelOptions = Object.entries(options?.vModel || {})
for (const [prop, propDefault] of vModelOptions) {
const reactiveValue = ref(propDefault)
const props = (options.props ?? {}) as Record<string, unknown>
props[prop] = propDefault
props[`onUpdate:${prop}`] = (value: unknown) => {
reactiveValue.value = value
}
vModelProps.push([prop, reactiveValue])
options.props = props
}
const mounted = mount(cmp, options)
if (vModelProps.length) {
watchEffect(async () => {
const propsValues = vModelProps.reduce((acc, [prop, reactiveValue]) => {
acc[prop] = reactiveValue.value
return acc
}, {} as Record<string, unknown>)
await mounted.setProps(propsValues)
})
}
return mounted
}
const view = render(Component, {
vModel: {
modelValue: ''
}
})
await view.find('input').input('test') It can also be easily rewritten to pass |
Beta Was this translation helpful? Give feedback.
-
The other day I came across a situation that I wanted to check if my input was updating the vmodel, couldn't find a way to do it on vtu, so I created this method:
It would be nice to have a way to do this automatically on vtu, I've a few proposes that I can think of.
Propose 1
We can handle the vmodel updates automatically, this would
setProps
automatically on the component and it would need to be disabled if not needed.Propose 2
If the user passes a
ref
as a prop we could start listening forvmodel
Propose 3
Adding a new mounting prop
vmodel
Beta Was this translation helpful? Give feedback.
All reactions