From e42b44e7bd74a5e3a7e7649bc3502fa37506a1c7 Mon Sep 17 00:00:00 2001 From: lmiller1990 Date: Sat, 2 Jun 2018 13:39:40 +0900 Subject: [PATCH 1/2] Add a createStub method and spec --- packages/test-utils/src/create-stub.js | 7 +++++++ test/specs/create-stub.spec.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 packages/test-utils/src/create-stub.js create mode 100644 test/specs/create-stub.spec.js diff --git a/packages/test-utils/src/create-stub.js b/packages/test-utils/src/create-stub.js new file mode 100644 index 000000000..7b6ca9cd6 --- /dev/null +++ b/packages/test-utils/src/create-stub.js @@ -0,0 +1,7 @@ +export default function createStub (name) { + return { + name, + render: h => h('div') + } +} + diff --git a/test/specs/create-stub.spec.js b/test/specs/create-stub.spec.js new file mode 100644 index 000000000..348c2d3fb --- /dev/null +++ b/test/specs/create-stub.spec.js @@ -0,0 +1,16 @@ +import ComponentWithChild from '~resources/components/component-with-child.vue' +import { describeWithShallowAndMount } from '~resources/utils' +import { shallow, createStub } from '~vue/test-utils' + +describeWithShallowAndMount('createStub', (mountingMethod) => { + it('stubs a component', () => { + const ChildComponent = createStub('TestComponent') + const wrapper = mountingMethod(ComponentWithChild, { + stubs: { + 'child-component': ChildComponent + } + }) + + expect(wrapper.findAll(ChildComponent).length).to.equal(1) + }) +}) From b500d50662f44f0326215b133b715cad2f53a011 Mon Sep 17 00:00:00 2001 From: lmiller1990 Date: Sun, 3 Jun 2018 19:46:44 +0900 Subject: [PATCH 2/2] Update docs --- docs/api/README.md | 3 ++- docs/api/createStub.md | 31 +++++++++++++++++++++++++++++++ packages/test-utils/src/index.js | 2 ++ test/specs/create-stub.spec.js | 2 +- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 docs/api/createStub.md diff --git a/docs/api/README.md b/docs/api/README.md index d081f89fb..605395bf8 100644 --- a/docs/api/README.md +++ b/docs/api/README.md @@ -5,5 +5,6 @@ !!!include(docs/api/render.md)!!! !!!include(docs/api/renderToString.md)!!! !!!include(docs/api/selectors.md)!!! +!!!include(docs/api/createStub.md)!!! !!!include(docs/api/createLocalVue.md)!!! -!!!include(docs/api/config.md)!!! \ No newline at end of file +!!!include(docs/api/config.md)!!! diff --git a/docs/api/createStub.md b/docs/api/createStub.md new file mode 100644 index 000000000..260493d0f --- /dev/null +++ b/docs/api/createStub.md @@ -0,0 +1,31 @@ +## createStub() + +- **Arguments:** + + - `{String} name` + +- **Returns:** + - `{Component}` + +- **Usage:** + +Creates a stub component. Useful when combined with [`find`](wrapper/find) and [`findAll`](wrapper/findAll). + +```js +import { shallowMount, createStub } from '@vue/test-utils' +import ComponentWithChild from './ComponentWithChild.vue' + +describe('ComponentWithChild', () => { + it('renders a child component', () => { + const Child = createStub('Child') + const wrapper = shallowMount(ComponentWithChild, { + stubs: { + Child: Child + } + }) + expect(wrapper.findAll(Child).length).toBe(1) + }) +}) +``` + +Where `Child` is a component with `name: 'Child'`. diff --git a/packages/test-utils/src/index.js b/packages/test-utils/src/index.js index 2c1982aa9..590b424b3 100644 --- a/packages/test-utils/src/index.js +++ b/packages/test-utils/src/index.js @@ -1,6 +1,7 @@ import shallowMount from './shallow-mount' import mount from './mount' import createLocalVue from './create-local-vue' +import createStub from './create-stub' import TransitionStub from './components/TransitionStub' import TransitionGroupStub from './components/TransitionGroupStub' import RouterLinkStub from './components/RouterLinkStub' @@ -15,6 +16,7 @@ function shallow (component, options) { export default { createLocalVue, config, + createStub, mount, shallow, shallowMount, diff --git a/test/specs/create-stub.spec.js b/test/specs/create-stub.spec.js index 348c2d3fb..aee846cc5 100644 --- a/test/specs/create-stub.spec.js +++ b/test/specs/create-stub.spec.js @@ -1,6 +1,6 @@ import ComponentWithChild from '~resources/components/component-with-child.vue' import { describeWithShallowAndMount } from '~resources/utils' -import { shallow, createStub } from '~vue/test-utils' +import { createStub } from '~vue/test-utils' describeWithShallowAndMount('createStub', (mountingMethod) => { it('stubs a component', () => {