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

fix: allow mounting functional components #118

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
VNodeNormalizedChildren,
transformVNodeArgs,
reactive,
FunctionalComponent,
ComponentPublicInstance,
ComponentOptionsWithObjectProps,
ComponentOptionsWithArrayProps,
Expand Down Expand Up @@ -53,6 +54,11 @@ type ExtractComponent<T> = T extends { new (): infer PublicInstance }
? PublicInstance
: any

// Functional component
export function mount<TestedComponent extends FunctionalComponent>(
originalComponent: TestedComponent,
options?: MountingOptions<any>
): VueWrapper<ComponentPublicInstance>
dobromir-hristov marked this conversation as resolved.
Show resolved Hide resolved
// Component declared with defineComponent
export function mount<TestedComponent extends ComponentPublicInstance>(
originalComponent: { new (): TestedComponent } & Component,
Expand Down Expand Up @@ -80,7 +86,14 @@ export function mount(
originalComponent: any,
options?: MountingOptions<any>
): VueWrapper<any> {
const component = { ...originalComponent }
// normalise the incoming component
const component =
typeof originalComponent === 'function'
? {
setup: (_, { attrs, slots }) => () =>
h(originalComponent, attrs, slots)
}
: { ...originalComponent }

const el = document.createElement('div')
el.id = MOUNT_ELEMENT_ID
Expand Down
57 changes: 57 additions & 0 deletions tests/functionalComponents.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { mount } from '../src'
import { h } from 'vue'
import Hello from './components/Hello.vue'

describe('functionalComponents', () => {
it('mounts a functional component', () => {
const Foo = (props) => h('div', { class: 'foo' }, props.msg)

const wrapper = mount(Foo, {
props: {
// @ts-ignore
msg: 'foo'
}
})
dobromir-hristov marked this conversation as resolved.
Show resolved Hide resolved

expect(wrapper.html()).toEqual('<div class="foo">foo</div>')
})

it('renders the slots of a functional component', () => {
const Foo = (props, { slots }) => h('div', { class: 'foo' }, slots)

const wrapper = mount(Foo, {
slots: {
default: 'just text'
}
})

expect(wrapper.html()).toEqual('<div class="foo">just text</div>')
})

it('asserts classes', () => {
const Foo = (props, { slots }) => h('div', { class: 'foo' }, slots)

const wrapper = mount(Foo, {
attrs: {
class: 'extra_classes'
}
})

expect(wrapper.classes()).toContain('extra_classes')
expect(wrapper.classes()).toContain('foo')
})

it('uses `find`', () => {
const Foo = () => h('div', { class: 'foo' }, h(Hello))
const wrapper = mount(Foo)

expect(wrapper.find('#root').exists()).toBe(true)
})

it('uses `findComponent`', () => {
const Foo = () => h('div', { class: 'foo' }, h(Hello))
const wrapper = mount(Foo)

expect(wrapper.findComponent(Hello).exists()).toBe(true)
})
})