javascript - 无法使用 Jest 中的提交按钮触发 Vuetify 表单提交

标签 javascript vue.js jestjs vuetify.js

我正在尝试使用 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/

相关文章:

javascript - Handlebars 表单提交不起作用

javascript - 网站和移动应用程序的登录 API 应该相同还是不同?

javascript - 在 VueJS 中将父函数传递给子组件

javascript - ReactShallowRenderer render() : Shallow rendering works only with custom components, 但提供的元素类型是 `object` 。错误

javascript - slim 3 : Could not import modules when unit testing

javascript - 串联执行原生js promise

javascript - 将变量传递给 JavaScript 函数

javascript - 如何修改我的自定义输入组件以使用 vee-validate?

vue.js - @ 符号在 Vue.js 中有什么作用?

javascript - 如何用 react-testing-library 模拟内容溢出?