Skip to content

Commit

Permalink
Merge pull request #49 from vuejs/feature/mocks
Browse files Browse the repository at this point in the history
feat: mocks mounting option
  • Loading branch information
lmiller1990 committed Apr 8, 2020
2 parents 9cf79b1 + a2dd554 commit ed65fd4
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/components/RouterLinkStub.ts
@@ -0,0 +1,17 @@
import { defineComponent, h } from 'vue'

// TODO: Borrow typings from vue-router-next
export const RouterLinkStub = defineComponent({
name: 'RouterLinkStub',

props: {
to: {
type: [String, Object],
required: true
}
},

render() {
return h('a', undefined, this.$slots.default())
}
})
3 changes: 2 additions & 1 deletion src/index.ts
@@ -1,3 +1,4 @@
import { mount } from './mount'
import { RouterLinkStub } from './components/RouterLinkStub'

export { mount }
export { mount, RouterLinkStub }
20 changes: 18 additions & 2 deletions src/mount.ts
Expand Up @@ -8,7 +8,8 @@ import {
ComponentOptions,
Plugin,
Directive,
Component
Component,
getCurrentInstance
} from 'vue'

import { VueWrapper, createWrapper } from './vue-wrapper'
Expand All @@ -28,8 +29,10 @@ interface MountingOptions<Props> {
global?: {
plugins?: Plugin[]
mixins?: ComponentOptions[]
mocks?: Record<string, any>
provide?: Record<any, any>
components?: Record<string, Component>
// TODO how to type `defineComponent`? Using `any` for now.
components?: Record<string, Component | object>
directives?: Record<string, Directive>
}
stubs?: Record<string, any>
Expand Down Expand Up @@ -78,6 +81,19 @@ export function mount<P>(
// create the vm
const vm = createApp(Parent(options && options.props))

// global mocks mixin
if (options?.global?.mocks) {
const mixin = {
beforeCreate() {
for (const [k, v] of Object.entries(options.global?.mocks)) {
this[k] = v
}
}
}

vm.mixin(mixin)
}

// use and plugins from mounting options
if (options?.global?.plugins) {
for (const use of options?.global?.plugins) vm.use(use)
Expand Down
76 changes: 76 additions & 0 deletions tests/mountingOptions/mocks.spec.ts
@@ -0,0 +1,76 @@
import { mount, RouterLinkStub } from '../../src'

describe('mocks', () => {
it('mocks a vuex store', async () => {
const Foo = {
template: `
<div>
count: {{ $store.state.count }}
<button @click="$store.dispatch('inc')" />
</div>
`
}
const $store = {
state: {
count: 1
},
dispatch: jest.fn()
}

const wrapper = mount(Foo, {
global: {
mocks: { $store }
}
})

expect(wrapper.html()).toContain('count: 1')
await wrapper.find('button').trigger('click')
expect($store.dispatch).toHaveBeenCalledWith('inc')
})

it('mocks vue-router', async () => {
const Foo = {
template: `
<div>
<RouterLink :to="url">Go to post: {{ id }}</RouterLink>
<button @click="submit">Go</button>
</div>
`,
computed: {
url() {
return `/posts/${this.$route.params.id}`
},
id() {
return this.$route.params.id
}
},
methods: {
submit() {
this.$router.push(`/posts/${this.id}`)
}
}
}

const $router = {
push: jest.fn()
}
const $route = {
params: {
id: 1
}
}

const wrapper = mount(Foo, {
global: {
components: {
RouterLink: RouterLinkStub
},
mocks: { $route, $router }
}
})

expect(wrapper.html()).toContain('Go to post: 1')
await wrapper.find('button').trigger('click')
expect($router.push).toHaveBeenCalledWith('/posts/1')
})
})

0 comments on commit ed65fd4

Please sign in to comment.