0

我有一个组件,当使用或created()设置时。date:nullprops.datainicial

export default {
  props: ['numeroDaParcela', 'datainicial'],
  data () {
    return {
      date: null,
      dateBR: '',
      modal: false
    }
  },
  created () {
    if (this.datainicial === '' ||
      this.datainicial === undefined) {
        this.date = null
    } else {
      this.date = this.datainicial
    }
  }

在没有的 DevTools 中props

在此处输入图像描述

在 DevTools 中有一些props

在此处输入图像描述

当我做我的测试时:

import { mount } from 'vue-test-utils'
import SelecionadorData from '@/components/Shared/SelecionadorData.vue'

describe('SelecionadorData.vue', () => {

  it('should receive the props datainicial', () => {
    const wrapper = mount(SelecionadorData)
    wrapper.setProps({
      datainicial: '2018-01-01'
    })
    expect(wrapper.vm.date).toBe('2018-01-01')
  })
})

我收到此错误:

在此处输入图像描述

4

1 回答 1

1

created仅在创建组件时运行 1 次。使用 时setProps,组件 props 会更新,但created不会再次调用方法。

import { mount } from 'vue-test-utils'
import SelecionadorData from '@/components/Shared/SelecionadorData.vue'

describe('SelecionadorData.vue', () => {

  it('should receive the props datainicial', () => {
    const wrapper = mount(SelecionadorData,
    {
        propsData: {
          datainicial: '2018-01-01'
        }
    })
    expect(wrapper.vm.date).toBe('2018-01-01')
  })
})
于 2018-05-18T03:40:24.627 回答