-
Notifications
You must be signed in to change notification settings - Fork 264
WIP: detect multiple root nodes #25
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
Changes from all commits
1cf1522
eb2c64c
bcfb13d
6d6e7ae
ccb460b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() { | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 - There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above! |
||
return rootElementWrapper.trigger(eventString) | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.