Skip to content
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

feat: mocks mounting option #49

Merged
merged 4 commits into from
Apr 8, 2020
Merged
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
17 changes: 17 additions & 0 deletions src/components/RouterLinkStub.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Copy link
Member Author

Choose a reason for hiding this comment

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

surprised this worked so easily

Copy link
Member

Choose a reason for hiding this comment

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

oh wow this is quite smart

}
}
}

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
Original file line number Diff line number Diff line change
@@ -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')
})
})