简体   繁体   English

在 vue 测试工具中 $refs 设置为空 object

[英]In vue test utils $refs is set to an empty object

I'm mounting a component EditUser in tests using the mount function from vue-test-utils .我正在使用来自vue-test-utilsmount function 在测试中安装组件EditUser EditUser has one child component ChangePassword . EditUser有一个子组件ChangePassword mount should mount all child components as is and not stub them. mount应该按原样安装所有子组件,而不是存根它们。 I'm accessing the ChangePassword components submit method using a ref like this this.$refs.changePassword.submit() .我正在使用this.$refs.changePassword.submit()ref访问ChangePassword组件submit方法。 It works fine in browser but in test $refs is getting set to an empty object and can not access the submit method.它在浏览器中运行良好,但在测试中$refs设置为空 object 并且无法访问提交方法。 I'm using jest to test my vue components.我正在使用 jest 来测试我的vue组件。

In EditUSer.vue componentEditUSer.vue组件中

updatePassword() {
  this.$refs.changePassword.submit()
    .then(data => {
       this.successMessage = data.message
       this.$bvModal.hide('change-password-modal')
     })
     .catch(data => {
       console.error(data.message)
      })
}

In template在模板中

<b-modal id="change-password-modal" title="Change Password" size="lg" @ok="updatePassword">
    <change-password ref="changePassword" :id="id"></change-password>
</b-modal>

And in ChangePassword.vue component I have a submit methodChangePassword.vue组件中我有一个提交方法

submit() {
  return this.form.patch('/api/users/' + this.id + '/password')
}

In test测试中

it ('updates password', async () => {
    moxios.stubRequest('/api/users/1/password', {
      status: 200,
      response: {
        message: 'Password updated successfully!'
      }
    })

    const wrapper = mount(EditUser, { propsData: { id: 1 } })
    const testUtils = new TestUtils(wrapper)

    wrapper.vm.updatePassword()

    await flushPromises()

    testUtils.see('Password updated successfully!')
  })

It works fine in browser but in test (jest) I'm getting this error它在浏览器中运行良好,但在测试中(开玩笑)我收到了这个错误

TypeError: Cannot read property 'submit' of undefined

      262 |       },
      263 |       updatePassword() {
    > 264 |         this.$refs.changePassword.submit()
          | ^
      265 |             .then(data => {
      266 |               this.successMessage = data.message
      267 |               this.$bvModal.hide('change-password-modal')

      at VueComponent.updatePassword (src/Users/EditUser.vue:264:1)
      at Object.<anonymous> (tests/unit/users/editUser.spec.js:64:16)

I had a similar problem using Vuetify's v-dialog.我在使用 Vuetify 的 v-dialog 时遇到了类似的问题。 I'm not familiar with BootstrapVue, but I think the issue stems from how Vue itself sets up the $refs.我不熟悉 BootstrapVue,但我认为问题源于 Vue 本身如何设置 $refs。

$refs won't be updated until the bound component is created, and Vue will wait to create the component until it needs to be rendered. $refs 在绑定组件创建之前不会更新,Vue 将等待创建组件,直到它需要渲染。

To get around this, just make sure your change-password component has been rendered at least once in your test before trying to access $refs.要解决这个问题,只需确保在尝试访问 $refs 之前,您的更改密码组件已在测试中至少呈现一次。

In your test, try something like在您的测试中,尝试类似

wrapper.vm.$bvModal.show('change-password-modal'); // should render the content, binding $refs.
await wrapper.vm.$nextTick(); // make sure Vue has time to update
wrapper.vm.updatePassword();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM