添加道具为找到的组件扔掉了安装的包装器
我有一个包含选择器可重复使用的组件的表格,
<template>
<div class="channelDetail" data-test="channelDetail">
<div class="row">
<BaseTypography class="label">{{ t('channel.detail.service') }}</BaseTypography>
<BaseSelector
v-model="serviceId"
data-test="serviceInput"
class="content"
:option="servicePicker.data?.data"
:class="serviceIdErrorMessage && 'input-error'"
/>
</div>
<div class="row">
<BaseTypography class="label">{{ t('channel.detail.title') }}</BaseTypography>
<BaseInput v-model="title" data-test="titleInput" class="content" :class="titleErrorMessage && 'input-error'" />
</div>
</div>
</template>
我将使用 test 使用 vue-test-utils 和 vitest 。 我需要将选项道具从脚本设置为选择器。 在我的想法中,这应该是可行的,但没有
it('test', async () => {
const wrapper=mount(MyForm,{})
wrapper.findComponent(BaseSelector).setProps({option:[...some options]})
---or
wrapper.find('[data-test="serviceInput"]').setProps({option:[...some options]})
---or ???
});
人可以帮助我将道具设置为已安装的包装器组件中的组件?
I have a form that contains a selector reusable component like this
<template>
<div class="channelDetail" data-test="channelDetail">
<div class="row">
<BaseTypography class="label">{{ t('channel.detail.service') }}</BaseTypography>
<BaseSelector
v-model="serviceId"
data-test="serviceInput"
class="content"
:option="servicePicker.data?.data"
:class="serviceIdErrorMessage && 'input-error'"
/>
</div>
<div class="row">
<BaseTypography class="label">{{ t('channel.detail.title') }}</BaseTypography>
<BaseInput v-model="title" data-test="titleInput" class="content" :class="titleErrorMessage && 'input-error'" />
</div>
</div>
</template>
I'm going to test this form by using vue-test-utils and vitest.
I need to set option props from the script to the selector.
In my thought, this should be worked but not
it('test', async () => {
const wrapper=mount(MyForm,{})
wrapper.findComponent(BaseSelector).setProps({option:[...some options]})
---or
wrapper.find('[data-test="serviceInput"]').setProps({option:[...some options]})
---or ???
});
Could anyone help me to set the props into components in the mounted wrapper component?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是您不应该这样做。因为
baseSelector
应该具有自己的测试,您应该在其中通过setProps
来测试行为更改。但是,如果由于某种原因无法执行此操作,则可以在这里做什么:
baseSelector
的道具。它们始终取决于一些反应性数据(props
,数据
或计算
)myform
而更改这些数据。例如
,我建议您通过更改其道具或数据来介绍
baseSelector
的行为。然后,在myform
的测试中,您应该只检查传递的道具baseSelector
The answer is that you should not do that. Because
BaseSelector
should have it's own tests in which you should test behavior changes through thesetProps
.But if you can't do this for some reason, here what you can do:
BaseSelector
. They always depend on some reactive data (props
,data
, orcomputed
)MyForm
instead.For example
But I suggest you to cover the behavior of
BaseSelector
in separate test by changing it's props or data. And then in theMyForm
's test you should just check the passed props toBaseSelector