vue-test-utils:Vue子组件方法“不是函数”

时间:2017-12-13 20:55:02

标签: vue.js vue-test-utils

在这个测试中,我只是试图断言在被测组件(gl_Position.z *= gl_Position.w; )上设置了end属性。该组件假定DateRangeEditorHotelDatepicker的一个实例,并调用其this.$refs.datepickersetCheckOut方法(如果有更好的方法,请告诉我们!)。< / p>

该组件在浏览器中正常加载时有效,但我对setCheckIn的测试失败,出现“TypeError:this。$ refs.datepicker.setCheckIn不是函数”。

我是否误解了setEndDate选项应该做什么设置?我认为这样的场景,调用代码只需调用存根函数而不需要具体结果,正好是存根的用途。我只是错了吗?

这是我的测试:

stubs

这是我的(大大简化)import { mount, shallow } from 'vue-test-utils' import DateRangeEditor from '../../assets/src/components/DateRangeEditor.vue' describe('DateRangeEditor', () => { describe('@checkOutChanged', () => { it('sets a new end date', () => { const wrapper = shallow(DateRangeEditor, { stubs: ['datepicker'] }) // simulate user changing the end date const date = new Date(2017, 11, 1) wrapper.vm.$refs.datepicker.$emit('checkOutChanged', date) expect(wrapper.vm.end).to.equal(date) }) }) describe('@checkInChanged', () => { it('sets a new start date', () => { const wrapper = shallow(DateRangeEditor, { stubs: ['datepicker'] }) // simulate user changing the start date const date = new Date(2017, 11, 1) wrapper.vm.$refs.datepicker.$emit('checkInChanged', date) expect(wrapper.vm.start).to.equal(date) }) }) }) 组件:

DateRangeEditor

最后,这是我的setup.js引导程序文件:

<template>
  <div class="date-range-editor-container">
    <datepicker
      @checkInChanged="setStartDate"
      @checkOutChanged="setEndDate"
      ref="datepicker"
    ></datepicker>
    <button type="button" @click="save" class="btn save-btn">Save</button>
    <button type="button" @click="cancel" class="btn cancel-btn">Cancel</button>
  </div>
</template>

<script>
import HotelDatePicker from 'vue-hotel-datepicker'

export default {
  methods: {
    setStartDate: function(newDate) {
      if (newDate) {
        // make sure newDate has an hour component
        newDate.setHours((new Date()).getHours())
        this.$refs.datepicker.setCheckIn(newDate)
        this.start = newDate
      }
    },
    setEndDate: function(newDate) {
      if (newDate) {
        // make sure newDate has an hour component
        newDate.setHours((new Date()).getHours())
        this.$refs.datepicker.setCheckOut(newDate)
        this.end = newDate
      }
    },
    save: function() {
      this.$emit('save', this.$data)
    },
    cancel: function() {
      this.$emit('cancel', this.$data)
    },
  },
  data: function() {
    return {
      start: '',
      end: '',
    }
  },
  components: {
    datepicker: HotelDatePicker,
  }
}
</script>

<style>
</style>

版本n东西

  • @ vue / test-utils ^ 1.0.0-beta.10
  • vue ^ 2.4.4

1 个答案:

答案 0 :(得分:3)

stubs取代了子组件方法。这就是你收到错误的原因。

如果要检查是否正在调用某个方法,可以使用这些方法创建一个组件,并将其传递给stubs选项。

const datepicker = {
    setCheckIn: () => {},
    setCheckOutDate: () => {},
    render: () => {}
}

it('sets a new end date', () => {
  const wrapper = shallow(DateRangeEditor, {
    stubs: {
      datepicker
    }
  })

  const date = new Date(2017, 11, 1)
  wrapper.vm.$refs.datepicker.$emit('checkOutChanged', date)
  expect(wrapper.vm.end).to.equal(date)
})