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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: detect multiple root nodes #25

Merged
merged 5 commits into from
Mar 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
createApp,
VNode,
defineComponent,
Plugin,
ComponentOptions
VNodeNormalizedChildren,
VNodeProps,
ComponentOptions,
Plugin
} from 'vue'

import { VueWrapper, createWrapper } from './vue-wrapper'
Expand Down Expand Up @@ -41,26 +43,23 @@ export function mount<P>(
document.body.appendChild(el)

// handle any slots passed via mounting options
const slots =
const slots: VNodeNormalizedChildren =
options?.slots &&
Object.entries(options.slots).reduce<Record<string, () => VNode | string>>(
(acc, [name, fn]) => {
acc[name] = () => fn
return acc
},
{}
)
Object.entries(options.slots).reduce((acc, [name, fn]) => {
acc[name] = () => fn
return acc
}, {})
// override component data with mounting options data
if (options?.data) {
const dataMixin = createDataMixin(options.data())
component.mixins = [...(component.mixins || []), dataMixin]
}

// create the wrapper component
const Parent = (props?: P) =>
const Parent = (props?: VNodeProps) =>
lmiller1990 marked this conversation as resolved.
Show resolved Hide resolved
defineComponent({
render() {
return h(component, props, slots)
return h(component, { ...props, ref: 'VTU_COMPONENT' }, slots)
}
})

Expand Down Expand Up @@ -90,7 +89,7 @@ export function mount<P>(
vm.mixin(emitMixin)

// mount the app!
const app = vm.mount('#app')
const app = vm.mount(el)

return createWrapper(app, events)
}
39 changes: 29 additions & 10 deletions src/vue-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
import { ComponentPublicInstance } from 'vue'
import { ShapeFlags } from '@vue/shared'

import { DOMWrapper } from './dom-wrapper'
import { WrapperAPI } from './types'
import { ErrorWrapper } from './error-wrapper'

export class VueWrapper implements WrapperAPI {
vm: ComponentPublicInstance
rootVM: ComponentPublicInstance
componentVM: ComponentPublicInstance
__emitted: Record<string, unknown[]> = {}

constructor(vm: ComponentPublicInstance, events: Record<string, unknown[]>) {
this.vm = vm
this.rootVM = vm
this.componentVM = this.rootVM.$refs[
'VTU_COMPONENT'
] as ComponentPublicInstance
this.__emitted = events
}

private get hasMultipleRoots(): boolean {
// if the subtree is an array of children, we have multiple root nodes
return this.componentVM.$.subTree.shapeFlag === ShapeFlags.ARRAY_CHILDREN
afontcu marked this conversation as resolved.
Show resolved Hide resolved
}

get element() {
return this.hasMultipleRoots
? // get the parent element of the current component
this.componentVM.$el.parentElement
: this.componentVM.$el
}

classes(className?) {
return new DOMWrapper(this.vm.$el).classes(className)
return new DOMWrapper(this.element).classes(className)
}

attributes(key?: string) {
return new DOMWrapper(this.vm.$el).attributes(key)
return new DOMWrapper(this.element).attributes(key)
}

exists() {
Expand All @@ -30,15 +47,17 @@ export class VueWrapper implements WrapperAPI {
}

html() {
return this.vm.$el.outerHTML
return this.hasMultipleRoots
? this.element.innerHTML
: this.element.outerHTML
}

text() {
return this.vm.$el.textContent?.trim()
return this.element.textContent?.trim()
}

find<T extends Element>(selector: string): DOMWrapper<T> | ErrorWrapper {
const result = this.vm.$el.querySelector(selector) as T
const result = this.element.querySelector(selector) as T
if (result) {
return new DOMWrapper(result)
}
Expand All @@ -47,16 +66,16 @@ export class VueWrapper implements WrapperAPI {
}

findAll<T extends Element>(selector: string): DOMWrapper<T>[] {
const results = (this.vm.$el as Element).querySelectorAll<T>(selector)
const results = (this.element as Element).querySelectorAll<T>(selector)
return Array.from(results).map((x) => new DOMWrapper(x))
}

async setChecked(checked: boolean = true) {
return new DOMWrapper(this.vm.$el).setChecked(checked)
return new DOMWrapper(this.element).setChecked(checked)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here it would probably be best to return an error, if the component has multiple roots?

Copy link
Member

Choose a reason for hiding this comment

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

as discussed I think we should omit this code entirely for now and revisit it later - setChecked on a VueWrapper may not make sense after all, I am in favor of only supporting it on DOMWrapper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. We can add them back later I guess.

}

trigger(eventString: string) {
const rootElementWrapper = new DOMWrapper(this.vm.$el)
const rootElementWrapper = new DOMWrapper(this.element)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here it would probably be best to return an error, if the component has multiple roots?

Copy link
Member

Choose a reason for hiding this comment

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

See above!

return rootElementWrapper.trigger(eventString)
}
}
Expand Down
65 changes: 47 additions & 18 deletions tests/find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,56 @@ import { defineComponent, h } from 'vue'

import { mount } from '../src'

test('find', () => {
const Component = defineComponent({
render() {
return h('div', {}, [h('span', { id: 'my-span' })])
}
describe('find', () => {
test('find using single root node', () => {
const Component = defineComponent({
render() {
return h('div', {}, [h('span', { id: 'my-span' })])
}
})

const wrapper = mount(Component)
expect(wrapper.find('#my-span')).toBeTruthy()
})

const wrapper = mount(Component)
expect(wrapper.find('#my-span')).toBeTruthy()
it('find using multiple root nodes', () => {
const Component = defineComponent({
render() {
return [h('div', 'text'), h('span', { id: 'my-span' })]
}
})

const wrapper = mount(Component)
expect(wrapper.find('#my-span')).toBeTruthy()
})
})

test('findAll', () => {
const Component = defineComponent({
render() {
return h('div', {}, [
h('span', { className: 'span' }),
h('span', { className: 'span' })
])
}
describe('findAll', () => {
test('findAll using single root node', () => {
const Component = defineComponent({
render() {
return h('div', {}, [
h('span', { className: 'span' }),
h('span', { className: 'span' })
])
}
})

const wrapper = mount(Component)
expect(wrapper.findAll('.span')).toHaveLength(2)
})

const wrapper = mount(Component)
expect(wrapper.findAll('.span')).toHaveLength(2)
})
test('findAll using multiple root nodes', () => {
const Component = defineComponent({
render() {
return [
h('span', { className: 'span' }),
h('span', { className: 'span' })
]
}
})

const wrapper = mount(Component)
expect(wrapper.findAll('.span')).toHaveLength(2)
})
})
31 changes: 22 additions & 9 deletions tests/html-text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@ import { defineComponent, h } from 'vue'

import { mount } from '../src'

test('html, text', () => {
const Component = defineComponent({
render() {
return h('div', {}, 'Text content')
}
describe('html', () => {
it('returns html when mounting single root node', () => {
const Component = defineComponent({
render() {
return h('div', {}, 'Text content')
}
})

const wrapper = mount(Component)

expect(wrapper.html()).toBe('<div>Text content</div>')
})

const wrapper = mount(Component)
it('returns the html when mounting multiple root nodes', () => {
const Component = defineComponent({
render() {
return [h('div', {}, 'foo'), h('div', {}, 'bar'), h('div', {}, 'baz')]
}
})

expect(wrapper.html()).toBe('<div>Text content</div>')
expect(wrapper.text()).toBe('Text content')
})
const wrapper = mount(Component)

expect(wrapper.html()).toBe('<div>foo</div><div>bar</div><div>baz</div>')
})
})
13 changes: 7 additions & 6 deletions tests/setChecked.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('setChecked', () => {
const wrapper = mount(Comp)
await wrapper.setChecked()

expect(wrapper.vm.$el.checked).toBe(true)
expect(wrapper.componentVM.$el.checked).toBe(true)
})

it('sets element checked true with no option passed', async () => {
Expand Down Expand Up @@ -64,11 +64,12 @@ describe('setChecked', () => {
const listener = jest.fn()
const Comp = defineComponent({
setup() {
return () => h('input', {
onChange: listener,
type: 'checkbox',
checked: true
})
return () =>
h('input', {
onChange: listener,
type: 'checkbox',
checked: true
})
}
})

Expand Down