Skip to content

add createStub function #678

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)!!!
!!!include(docs/api/config.md)!!!
31 changes: 31 additions & 0 deletions docs/api/createStub.md
Original file line number Diff line number Diff line change
@@ -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'`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the component needs to have the name child. It should use the constructor. Can you check if the example works for a component without a name?

7 changes: 7 additions & 0 deletions packages/test-utils/src/create-stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function createStub (name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it is better to add flow type as below.

export default function createStub (name: string): Component {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and add // @flow to the top of the file

return {
name,
render: h => h('div')
}
}

2 changes: 2 additions & 0 deletions packages/test-utils/src/index.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -15,6 +16,7 @@ function shallow (component, options) {
export default {
createLocalVue,
config,
createStub,
mount,
shallow,
shallowMount,
Expand Down
16 changes: 16 additions & 0 deletions test/specs/create-stub.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ComponentWithChild from '~resources/components/component-with-child.vue'
import { describeWithShallowAndMount } from '~resources/utils'
import { 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)
})
})