我正在尝试使用 Jest 和 Avoriaz 验证基于 Vuetify 组件的 Vue 表单的行为。
我可以触发submit.prevent
在表单上,产生预期的行为。
但触发click
提交按钮不起作用。
组件:
<template>
<v-form
ref="form"
data-cy="form"
@submit.prevent="login"
>
<v-text-field
id="email"
v-model="email"
label="Email"
name="email"
prepend-icon="mdi-at"
type="text"
required
autofocus
data-cy="email-text"
/>
<v-btn
color="primary"
type="submit"
data-cy="login-btn"
>
Login
</v-btn>
</v-form>
</template>
<script>
export default {
data () {
return {
email: 'test@test.com',
}
},
computed: {},
methods: {
login: function () {
console.log('Logging in')
}
}
}
</script>
测试设置:
import vuetify from '@/plugins/vuetify'
import { mount } from 'avoriaz'
import Form from '@/views/Form'
describe('Form', () => {
const mountFunction = options => {
return mount(Form, {
vuetify,
...options
})
}
Vue 和 Vuetify 设置在 @/plugins/vuetify
中完成:
import Vue from 'vue'
import Vuetify from 'vuetify/lib'
Vue.use(Vuetify)
export default new Vuetify({
})
以下测试成功(因此模拟有效):
it('can trigger form directly', () => {
const login = jest.fn()
const wrapper = mountFunction()
wrapper.setData({ 'email': 'test@com' })
wrapper.setMethods({ login })
let element = wrapper.first('[data-cy=form]')
element.trigger('submit.prevent')
expect(login).toHaveBeenCalledTimes(1)
})
但是实际测试提交按钮失败了:
it('can trigger form through button', () => {
const login = jest.fn()
const wrapper = mountFunction()
wrapper.setData({ 'email': 'test@test.com' })
wrapper.setMethods({ login })
const button = wrapper.first('[type=submit]')
button.trigger('click')
expect(login).toHaveBeenCalledTimes(1)
})
更新:
也许 package.json
中有一些相关的依赖项:
{
..
"dependencies": {
"axios": "^0.19.1",
"core-js": "^3.4.4",
"vue": "^2.6.11",
"vue-router": "^3.1.3",
"vuetify": "^2.1.0",
"vuex": "^3.1.2"
},
"devDependencies": {
..
"avoriaz": "^6.3.0",
"vue-jest": "^3.0.5",
"vuetify-loader": "^1.3.0"
}
}
更新:
使用 test-utils、非 Vuetify 组件( <form>
和 <btn
)时,以下测试成功:
const localVue = createLocalVue()
localVue.use(Vuetify)
describe('Form', () => {
const mountFunction = options => {
return shallowMount(Form, {
localVue,
vuetify,
...options
})
}
it('can trigger form through button alternative', async () => {
const login = jest.fn()
const wrapper = mountFunction({ attachToDocument: true })
try {
wrapper.setData({ 'email': 'test@test.com' })
wrapper.setMethods({ login })
const button = wrapper.find('[type=submit]')
expect(button).toBeDefined()
button.trigger('click')
await Vue.nextTick()
expect(login).toHaveBeenCalledTimes(1)
} finally {
wrapper.destroy()
}
})
})
然后切换到 Vuetify 组件会导致测试失败。
最佳答案
显然,在使用 Vuetify 组件( <v-form>
和 <v-btn>
)时,需要一些关键要素才能使其正常工作:
- Vue test-utils 而不是 avoriaz
-
mount
而不是shallowMount
- 包括
attachToDocument
,这也需要事后清理(wrapper.destroy()
) - 正如 @Husam Ibrahim 也提到的,需要
async
错误地await Vue.nextTick()
- 要让 setData 充分发挥作用,您可能需要额外
await Vue.nextTick()
。在我的完整代码中,我进行了表单验证(输入上有:rules
,表单上有v-model
,按钮的:disabled
绑定(bind)到v-model
数据元素。这需要两个nextTick()
以下作品('@/plugins/vuetify'
与问题中的相同:
import vuetify from '@/plugins/vuetify'
import Vue from 'vue'
import { createLocalVue, mount } from '@vue/test-utils'
import Form from '@/views/Form'
import Vuetify from 'vuetify/lib'
const localVue = createLocalVue()
localVue.use(Vuetify)
describe('Form', () => {
const mountFunction = options => {
return mount(Form, {
localVue,
vuetify,
...options
})
}
it('can trigger form through button alternative', async () => {
const login = jest.fn()
const wrapper = mountFunction({ attachToDocument: true })
try {
wrapper.setData({ 'email': 'test@test.com' })
await Vue.nextTick() # might require multiple
wrapper.setMethods({ login })
const button = wrapper.find('[type=submit]')
button.trigger('click')
await Vue.nextTick()
expect(login).toHaveBeenCalledTimes(1)
} finally {
wrapper.destroy()
}
})
})
关于javascript - 无法使用 Jest 中的提交按钮触发 Vuetify 表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60685233/