VUE配方 - 表单按钮上的测试事件发射单击使用测试库
我有以下组件,使用 vue配方
<template>
<FormulateForm @submit="onSubmit">
<button type="submit">Submit</button>
</FormulateForm>
</template>
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
name: 'Issue',
methods: {
onSubmit(): void {
this.$emit('my-special-event')
},
},
})
</script>
<style scoped></style>
我的目标是测试当我单击“提交”按钮时, My-Event-Event
已发出。而且我需要使用 testing-library 。这是我的尝试:
import '@testing-library/jest-dom'
import { fireEvent, render } from '@testing-library/vue'
import Vue from 'vue'
import VueFormulate from '@braid/vue-formulate'
import Issue from '~/components/Issue.vue'
describe('Issue component', () => {
it('test', async () => {
Vue.use(VueFormulate)
const component = render(Issue)
const submitBtn = component.getByText('Submit')
await fireEvent.click(submitBtn)
expect(component.emitted()).not.toEqual({})
})
})
不幸的是,这无效 - 没有发出任何事件。不过,针对非形式形式的类似测试可以正常运行。
I have the following component, using Vue Formulate
<template>
<FormulateForm @submit="onSubmit">
<button type="submit">Submit</button>
</FormulateForm>
</template>
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
name: 'Issue',
methods: {
onSubmit(): void {
this.$emit('my-special-event')
},
},
})
</script>
<style scoped></style>
My goal is to test that, when I click the "Submit" button, my-special-event
event is emitted. And I need to do this using testing-library. Here's my attempt:
import '@testing-library/jest-dom'
import { fireEvent, render } from '@testing-library/vue'
import Vue from 'vue'
import VueFormulate from '@braid/vue-formulate'
import Issue from '~/components/Issue.vue'
describe('Issue component', () => {
it('test', async () => {
Vue.use(VueFormulate)
const component = render(Issue)
const submitBtn = component.getByText('Submit')
await fireEvent.click(submitBtn)
expect(component.emitted()).not.toEqual({})
})
})
Unfortunately, this doesn't work - no events are emitted. A similar test for a non-Formulate form works ok, though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论