How to test if a component emits an event in Vue?
Asked Answered
M

3

9

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.

Merciful answered 6/2, 2020 at 7:11 Comment(0)
W
6

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)
    }
  }
Wreak answered 26/1, 2021 at 13:9 Comment(1)
I tested it and it works fine. Actually, on my machine it even works without $nextTick(), you can read emitted() immediately.Lightly
G
2
const wrapper = mount(Component)

wrapper.vm.$emit('foo')
wrapper.vm.$emit('foo', 123)

await wrapper.vm.$nextTick() // Wait until $emits have been handled

expect(wrapper.emitted('foo')).toBeTruthy()

Source: https://v1.test-utils.vuejs.org/api/wrapper/emitted.html

Gauze answered 4/4, 2023 at 17:35 Comment(1)
This test is testing that the VM emitted an event, which you are telling the test to emit. This seems like a false positive.Isidore
N
-3

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>
Nicolina answered 6/2, 2020 at 8:0 Comment(1)
How can we write a test to check child component really emits this event?Wergild

© 2022 - 2024 — McMap. All rights reserved.