1
votes

I have two components. Child component emits an 'input' event when it's value changed and parent component takes this value with v-model. I'm testing ChildComponent. I need to write a test with Vue-test-utils to verify it works.

ParentComponent.vue:

<template>
 <div>
  <child-component v-model="search"></child-component>
  <other-component></other-component>
  ...
 </div>
</template>

ChildComponent.vue:

<template>
  <input :value="value" @change="notifyChange($event.target.value)"></input>
</template>

<script lang="ts">
  import { Component, Prop, Vue } from 'vue-property-decorator'

  @Component
  export default class ChildComponent extends Vue {

    @Prop({ default: '' }) readonly value!: string

    notifyChange(value: string) {
      this.$emit('input', value)
    }

  }
</script>

child-component.spec.ts:

describe('ChildComponent', () => {
   let wrapper: any
   before(() => {
   wrapper = VueTestUtils.shallowMount(ChildComponent, {})
  })

   it(`Should emit 'input' event when value change`, () => {
    const rootWrapper = VueTestUtils.shallowMount(ParentComponent)
    wrapper.vm.value = 'Value'
    wrapper.findAll('input').at(0).trigger('change')
    assert.isTrue(!!rootWrapper.vm.search)
  })
})

I didn't write the exact same code but the logic is like this. My components work properly. 'child-component.spec.ts' doesn't work. I need to write a test for it.

3

3 Answers

1
votes

TESTED. This is a simple way of testing an emit, if someone is looking for this. in your test describe write this.

describe('Close Button method', () => {
  it('emits return false if button is clicked', (done) => {
    wrapper.find('button').trigger('click')
    wrapper.vm.$nextTick(() => {
      wrapper.vm.closeModal() //closeModal is my method
      expect(wrapper.emitted().input[0]).toEqual([false]) //test if it changes
      done()
    })
  })
})

my vue comp

<div v-if="closeButton == true">
      <button
        @click="closeModal()"
      >
      ...
      </button>
</div>

my props in vue comp

 props: {
    value: {
      type: Boolean,
      default: false
    },

my methods in vue comp

methods: {
    closeModal() {
      this.$emit('input', !this.value)
    }
  }
0
votes

Here's an example that will help you: Child Component.vue

<template>
  <div>
    <h2>{{ numbers }}</h2>
    <input v-model="number" type="number" />
    <button @click="$emit('number-added', Number(number))">
     Add new number
    </button>
  </div>
</template>

<script>
export default {
  name: "ChildComponent",
  props: {
    numbers: Array
  },
  data() {
    return {
      number: 0
    };
  }
};
</script>

Parent Component.vue

<template>
  <div>
    <ChildComponent
      :numbers="numbers"
      @number-added="numbers.push($event)"
    />
  </div>
</template>

<script>
import ChildComponent from "./ChildComponent";
export default {
  name: "ParentComponent",
  data() {
    return {
      numbers: [1, 2, 3]
    };
  },
  components: {
    ChildComponent
  }
};
</script>

Follow this article: https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87

I hope this will help you.

0
votes

in your parent component listen to the emitted event "input"

<template>
 <div>
  <child-component @input="get_input_value" v-model="search"></child-component>
  <other-component></other-component>
  ...
 </div>
</template>

and in your script add method get_input_value()

<script>
...
methods:
get_input_value(value){
console.log(value)
}
</script>