From e272ba0611ed32626c8df6d293e41d515c8ce28c Mon Sep 17 00:00:00 2001 From: Victor Cazanave Date: Fri, 2 Nov 2018 16:43:15 +0800 Subject: [PATCH 1/2] docs: add emitting event from child component section --- docs/guides/common-tips.md | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/docs/guides/common-tips.md b/docs/guides/common-tips.md index a70234ecd..a9e250a19 100644 --- a/docs/guides/common-tips.md +++ b/docs/guides/common-tips.md @@ -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 + + + +``` + +**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 by child component", () => { + 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: From f38e6814dbfbfa47f95ce7d46e76ae5d710fc94a Mon Sep 17 00:00:00 2001 From: Victor Cazanave Date: Fri, 2 Nov 2018 17:30:06 +0800 Subject: [PATCH 2/2] fix lint --- docs/guides/common-tips.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guides/common-tips.md b/docs/guides/common-tips.md index a9e250a19..d7be5165a 100644 --- a/docs/guides/common-tips.md +++ b/docs/guides/common-tips.md @@ -98,7 +98,7 @@ import ParentComponent from '@/components/ParentComponent' import ChildComponent from '@/components/ChildComponent' describe('ParentComponent', () => { - it("displays 'Emitted!' when custom event is emitted by child component", () => { + it("displays 'Emitted!' when custom event is emitted", () => { const wrapper = shallowMount(ParentComponent) wrapper.find(ChildComponent).vm.$emit('custom') expect(wrapper.html()).toContain('Emitted!')