Skip to content

docs: add emitting event from child component section #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions docs/guides/common-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,54 @@ expect(wrapper.emitted().foo[1]).toEqual([123])

You can also get an Array of the events in their emit order by calling [`wrapper.emittedByOrder()`](../api/wrapper/emittedByOrder.md).

### Emitting Event from Child Component

You can emit a custom event from a child component by accessing the instance.

**Component under test**
```html
<template>
<div>
<child-component @custom="onCustom"/>
<p v-if="emitted">Emitted!</p>
</div>
</template>

<script>
import ChildComponent from './ChildComponent'

export default {
name: 'ParentComponent',
components: { ChildComponent },
data() {
return {
emitted: false
}
},
methods: {
onCustom () {
this.emitted = true
}
}
}
</script>
```

**Test**
```js
import { shallowMount } from '@vue/test-utils'
import ParentComponent from '@/components/ParentComponent'
import ChildComponent from '@/components/ChildComponent'

describe('ParentComponent', () => {
it("displays 'Emitted!' when custom event is emitted", () => {
const wrapper = shallowMount(ParentComponent)
wrapper.find(ChildComponent).vm.$emit('custom')
expect(wrapper.html()).toContain('Emitted!')
})
})
```

### Manipulating Component State

You can directly manipulate the state of the component using the `setData` or `setProps` method on the wrapper:
Expand Down