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

ger*_*ben 4 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>
Run Code Online (Sandbox Code Playgroud)

测试设置:

import vuetify from '@/plugins/vuetify'
import { mount } from 'avoriaz'
import Form from '@/views/Form'

describe('Form', () => {
  const mountFunction = options => {
    return mount(Form, {
      vuetify,
      ...options
    })
  }
Run Code Online (Sandbox Code Playgroud)

Vue & Vuetify 设置在@/plugins/vuetify以下位置完成:

import Vue from 'vue'
import Vuetify from 'vuetify/lib'

Vue.use(Vuetify)

export default new Vuetify({
})
Run Code Online (Sandbox Code Playgroud)

以下测试成功(因此模拟工作):

  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)
  })
Run Code Online (Sandbox Code Playgroud)

但实际测试提交按钮,失败:

  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)
  })
Run Code Online (Sandbox Code Playgroud)

更新:也许一些相关的依赖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"
  }
}
Run Code Online (Sandbox Code Playgroud)

更新: 当使用 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()
    }
  })
})
Run Code Online (Sandbox Code Playgroud)

然后切换到 Vuetify 组件导致测试失败。

ger*_*ben 7

显然,在使用 Vuetify 组件(<v-form><v-btn>)时,需要一些关键要素才能使其工作:

  • Vue测试工具代替 avoriaz
  • mount 代替 shallowMount
  • include attachToDocument,这也需要事后清理 ( wrapper.destroy())
  • 正如@Husam Ibrahim 也提到的,需要async认真await Vue.nextTick()
  • 要让 setData 发挥全部作用,您可能需要额外添加await Vue.nextTick(). 在我的全部代码,我有表单验证(与:rules输入和v-model形式,以及按钮的上:disabled绑定到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()
    }
  })
})
Run Code Online (Sandbox Code Playgroud)