extraxt
extraxt

Reputation: 425

Test Error Comparing Props and Data

I have a component that when created() sets date: with null or with props.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
    }
  }

In DevTools with no props:

enter image description here

In DevTools with some props:

enter image description here

When I do my test:

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')
  })
})

I get this error:

enter image description here

Upvotes: 0

Views: 41

Answers (1)

ittus
ittus

Reputation: 22393

created only runs 1 time when component is created. When you use setProps, component props will be updated but created method will not be called again.

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')
  })
})

Upvotes: 1

Related Questions