我有两个指令,它们应该将数据属性添加到组件中进行测试,但是实际上只有一个指令被添加了。这两个组件是Bootstrap的BFormInput和BButton。
我试着删除除了一个按钮之外的所有内容,但指令仍未添加,即
<b-input-group class="sm-2 mb-2 mt-2">
<b-button
variant="primary"
@click="searchJobs"
class="rounded-0"
v-jobs-search-button-directive="{ id: 'search-button' }"
>
Search
</b-button>
</b-input-group>wrapper.html()输出是:
<b-input-group-stub tag="div" class="sm-2 mb-2 mt-2"><b-button-stub target="_self" event="click" routertag="a" variant="secondary" type="button" tag="button" class="rounded-0">
Search
</b-button-stub></b-input-group-stub>但是,当我把输入表单放在适当的位置而不是按钮时,它就被添加了。
<b-input-group class="sm-2 mb-2 mt-2">
<b-form-input
v-jobs-search-input-directive="{ id: 'input-keyword' }"
class="mr-2 rounded-0"
placeholder="Enter Search term..."
:value="this.searchConfig.Keyword"
@input="this.updateJobsSearchConfig"
/>
</b-input-group>wrapper.html()输出是:
<b-input-group-stub tag="div" class="sm-2 mb-2 mt-2"><b-form-input-stub value="" placeholder="Enter Search term..." type="text" class="mr-2 rounded-0" data-jobs-search-input-id="input-keyword"></b-form-input>我就是这样添加指令的
<template>
<b-input-group class="sm-2 mb-2 mt-2">
<b-form-input
v-jobs-search-input-directive="{ id: 'input-keyword' }"
class="mr-2 rounded-0"
placeholder="Enter Search term..."
:value="this.searchConfig.Keyword"
@input="this.updateJobsSearchConfig"
/>
<b-button
variant="primary"
@click="searchJobs"
class="rounded-0"
v-jobs-search-button-directive="{ id: 'search-button' }"
>
Search
</b-button>
</b-input-group>
</template>
<script>
import { mapActions, mapState } from 'vuex'
import JobService from '@/api-services/job.service'
import JobsSearchInputDirective from '@/directives/components/jobs/JobsSearchInputDirective'
import JobsSearchButtonDirective from '@/directives/components/jobs/JobsSearchButtonDirective'
export default {
name: 'jobs-search',
directives: { JobsSearchInputDirective, JobsSearchButtonDirective },
data () {
return {
jobs: [],
pages: 0
}
},
computed: {
...mapState({
pagedConfig: state => state.jobs.paged,
searchConfig: state => state.jobs.search
})
},
methods: {
// Methods go here
}
}作业搜索输入指令是
export default (el, binding) => {
if (process.env.NODE_ENV === 'test') {
Object.keys(binding.value).forEach(value => {
el.setAttribute(`data-jobs-search-input-${value}`, binding.value[value])
})
}
}作业-搜索-按钮-指令是
export default (el, binding) => {
if (process.env.NODE_ENV === 'test') {
Object.keys(binding.value).forEach(value => {
el.setAttribute(`data-jobs-search-button-${value}`, binding.value[value])
})
}
}这是我用shallowMount运行的测试
it('should call jobsSearch method on search button click event', () => {
wrapper.find('[data-jobs-search-button-id="search-button"]').trigger('click')
expect(searchJobs).toHaveBeenCalled()
})回来的时候
Error: [vue-test-utils]: find did not return [data-jobs-search-button-id="search-button"], cannot call trigger() on empty Wrapper然而,wrapper.find('[data-jobs-search-input-id="input-keyword"]')确实找到了输入表单。
这两个指令在JobsSearch.vue组件中注册,如果我移除process.env部分,它们肯定会被呈现。
我希望将该属性添加到两个组件中,但只有在测试时才会将其添加到BFormInput中。任何帮助都将不胜感激。
发布于 2019-09-05 20:53:02
我相信问题发生在..。
shallowMount。b-button是一个功能组件。
我已经把下面的演示放在一起来说明这个问题。它以3种不同的方式安装相同的组件,并且仅在上面概述的特定情况下失败。
MyComponent = {
template: `
<div>
<my-normal v-my-directive></my-normal>
<my-functional v-my-directive></my-functional>
</div>
`,
components: {
MyNormal: {
render: h => h('span', 'Normal')
},
MyFunctional: {
functional: true,
render: (h, context) => h('span', context.data, 'Functional')
}
},
directives: {
myDirective (el) {
el.setAttribute('name', 'Lisa')
}
}
}
const v = new Vue({
el: '#app',
components: {
MyComponent
}
})
document.getElementById('markup1').innerText = v.$el.innerHTML
const cmp1 = VueTestUtils.mount(MyComponent)
document.getElementById('markup2').innerText = cmp1.html()
const cmp2 = VueTestUtils.shallowMount(MyComponent)
document.getElementById('markup3').innerText = cmp2.html()#markup1, #markup2, #markup3 {
border: 1px solid #777;
margin: 10px;
padding: 10px;
}<script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script>
<script src="https://unpkg.com/vue-template-compiler@2.6.10/browser.js"></script>
<script src="https://unpkg.com/@vue/test-utils@1.0.0-beta.29/dist/vue-test-utils.iife.js"></script>
<div id="app">
<my-component></my-component>
</div>
<div id="markup1"></div>
<div id="markup2"></div>
<div id="markup3"></div>
我以前还没有真正看过vue-test-utils的代码,但是在调试器中的步骤让我对这一行产生了怀疑:
这是存根子组件的render函数。看起来,context.data.directives确实包含正确的指令,但在对h的调用中并没有传递它们。
与我的示例组件render中的MyFunctional函数相比,MyFunctional传递了所有的data。这是指令使用函数组件所必需的,但是当MyFunctional被存根替换时,新的render函数似乎会删除directives属性。
我能找到的唯一解决办法是提供您自己的存根:
VueTestUtils.shallowMount(MyComponent, {
stubs: {
BButton: { render: h => h('div')}
}
})通过使用非功能存根,指令工作得很好。但不确定这会给测试带来多大的损失。
https://stackoverflow.com/questions/57808229
复制相似问题