首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多个指令向组件添加数据属性

使用多个指令向组件添加数据属性
EN

Stack Overflow用户
提问于 2019-09-05 15:00:20
回答 1查看 361关注 0票数 1

我有两个指令,它们应该将数据属性添加到组件中进行测试,但是实际上只有一个指令被添加了。这两个组件是Bootstrap的BFormInput和BButton。

我试着删除除了一个按钮之外的所有内容,但指令仍未添加,即

代码语言:javascript
复制
    <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()输出是:

代码语言:javascript
复制
    <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>

但是,当我把输入表单放在适当的位置而不是按钮时,它就被添加了。

代码语言:javascript
复制
<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()输出是:

代码语言:javascript
复制
<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>

我就是这样添加指令的

代码语言:javascript
复制
<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
}
}

作业搜索输入指令是

代码语言:javascript
复制
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])
    })
  }
}

作业-搜索-按钮-指令是

代码语言:javascript
复制
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运行的测试

代码语言:javascript
复制
  it('should call jobsSearch method on search button click event', () => {
    wrapper.find('[data-jobs-search-button-id="search-button"]').trigger('click')
    expect(searchJobs).toHaveBeenCalled()
  })

回来的时候

代码语言:javascript
复制
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中。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-05 20:53:02

我相信问题发生在..。

  • ..。试图使用指令..。
  • ..。关于一个功能性的子组件..。
  • ..。用shallowMount

b-button是一个功能组件。

我已经把下面的演示放在一起来说明这个问题。它以3种不同的方式安装相同的组件,并且仅在上面概述的特定情况下失败。

代码语言:javascript
复制
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()
代码语言:javascript
复制
#markup1, #markup2, #markup3 {
  border: 1px solid #777;
  margin: 10px;
  padding: 10px;
}
代码语言:javascript
复制
<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的代码,但是在调试器中的步骤让我对这一行产生了怀疑:

https://github.com/vuejs/vue-test-utils/blob/9dc90a3fd4818ff70e270568a2294b1d8aa2c3af/packages/create-instance/create-component-stubs.js#L99

这是存根子组件的render函数。看起来,context.data.directives确实包含正确的指令,但在对h的调用中并没有传递它们。

与我的示例组件render中的MyFunctional函数相比,MyFunctional传递了所有的data。这是指令使用函数组件所必需的,但是当MyFunctional被存根替换时,新的render函数似乎会删除directives属性。

我能找到的唯一解决办法是提供您自己的存根:

代码语言:javascript
复制
VueTestUtils.shallowMount(MyComponent, {
  stubs: {
    BButton: { render: h => h('div')}
  }
})

通过使用非功能存根,指令工作得很好。但不确定这会给测试带来多大的损失。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57808229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档