使用element-ui和vue-test-utils模拟选择

nic*_*nck 7 javascript vue.js jestjs vue-test-utils element-ui

我正在对Vue中的Jest和Element-ui进行单元测试,该组件包含一个带有2个选项的select。我从下拉菜单中选择一个选项,然后检查是否已调用操作。

1)使用normal selectoptionHTML标签,可以完美地工作。

// Fruit.vue

<template lang="pug">
  select(
    v-model="fruit"
  )
    option(
      v-for="item in fruits"
      :label="item.label"
      :value="item.value"
    )
</template>
<script>
export default {
  data () {
    return {
      fruits: [
        {
          label: 'Banana',
          value: false
        },
        {
          label: 'Apple',
          value: false
        }
      ]
    }
  },
  computed: {
    fruit: {
      get () {
        return this.$store.state.fruit
      },
      set (fruit) {
        this.$store.dispatch('setSelectedFruit', { fruit })
      }
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

// DOM

<select>
  <option label="Banana" value="false"></option>
  <option label="Apple" value="false"></option>
</select>
Run Code Online (Sandbox Code Playgroud)

// Fruit.spec.js

it('calls the "setSelectedFruit" action when a value is selected', () => {
  const wrapper = mount(Fruit, { store, localVue })
  const options = wrapper.find('select').findAll('option')


  options.at(1).setSelected()
  expect(actions.setSelectedFruit).toHaveBeenCalled()
})
Run Code Online (Sandbox Code Playgroud)

但是,我使用element-ui el-selectel-option,它们与DOM有自己的交互作用。

2)利用el-selectel-option

// Fruit.vue

保持不变,除了select被替换el-selectoption通过el-option

// DOM

<div class="el-select-dropdown">
  <div class="el-select-dropdown__wrap">
    <ul class="el-select-dropdown__list">
      <li class="el-select-dropdown__item">
        <span>Banana</span>
      </li>
      <li class="el-select-dropdown__item">
        <span>Apple</span>
      </li>
    </ul>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

// Fruit.spec.js

一种)

it('calls the "setSelectedFruit" action', () => {
  const wrapper = mount(Fruit, { store, localVue })
  const options = wrapper.find('.el-select-dropdown__list').findAll('el-select-dropdown__items')

  options.at(1).setSelected()
  expect(actions.setSelectedFruit).toHaveBeenCalled()
})
Run Code Online (Sandbox Code Playgroud)

b)setSelected根据vue-test-utils文档,鉴于这实际上是别名,我以这种方式尝试了它:

it('calls the "setSelectedFruit" action', () => {
  const wrapper = mount(Fruit, { store, localVue })
  const options = wrapper.findAll('.el-select-dropdown__item')
  const select = wrapper.find('.el-select-dropdown__list')

  options.at(1).element.selected = false
  select.trigger('change')
  expect(actions.setSelectedFruit).toHaveBeenCalled()
})
Run Code Online (Sandbox Code Playgroud)

使用第二种方法,将selected option设置为selected,但是on上的触发器select不起作用,因此v-model不会更新。

因此,我想知道是否有人对此有解决方案,或者是否还有另一个可以模拟element-ui的el-select库(vue-test-utils除外)。

小智 1

我可以通过触发单击您想要模拟的下拉项来实现此功能:

wrapper.findAll('.el-select-dropdown__item').at(1).trigger('click');
await Vue.nextTick();
expect(YourExpectStatement);
Run Code Online (Sandbox Code Playgroud)

我需要 Vue.nextTick(),因为它允许 DOM 更新其周期,更多信息请参见此处。另请注意,您需要使测试函数异步,例如:

it('Async test name', async () => {
Run Code Online (Sandbox Code Playgroud)