【问题标题】:Jest: wrapper.find().simulate('keypress', {key: 'Enter', keycode: 13}) is not function开玩笑: wrapper.find().simulate('keypress', {key: 'Enter', keycode: 13}) 不起作用
【发布时间】:2020-07-03 13:52:21
【问题描述】:

从这个问题继续

vue-btn doesn't submit on enter key

我有这个登录页面,可以使用键盘中的“Enter”登录 我想在模拟键盘上的回车键时进行单元测试,它可以自动登录

这是我的测试代码

describe('Login.vue', () => {
    const vuetify = new Vuetify()
    const store = new Vuex.Store({
        actions: {
            login: jest.fn()
        }
    })
    const wrapper = mount(Login, { stubs: ['router-link', 'router-view'], store, vuetify })

    it('login using enter key on keyboard', async () => {
        wrapper.setData({ email: 'user@example.com' })
        wrapper.setData({ password: 'Passw0rd' })

        await wrapper.vm.$nextTick()
        wrapper.find('[data-cy="input-password"]').simulate('keypress', {key: 'Enter', keycode: 13})
    })
})

我已经使用 Cypress 进行了集成测试,但我想先进行单元测试,然后再将其提交到存储库。

非常感谢

【问题讨论】:

    标签: javascript vue.js jestjs


    【解决方案1】:

    这对我有用:

    await wrapper.find('#myFieldID').vm.$emit('keypress', {type: 'enter'});
    

    因此,在这种情况下,该行应该是:

    wrapper.find('[data-cy="input-password"]').vm.$emit('keypress', {type: 'Enter'})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-11
      • 2018-10-13
      • 1970-01-01
      • 2018-08-25
      • 2023-01-07
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多