Skip to content
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: 10 additions & 7 deletions src/dom-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ export class DOMWrapper<ElementType extends Element> {
this.element = element
}

classes(className?: string) {
classes(): string[]
classes(className: string): boolean
classes(className?: string): string[] | boolean {
const classes = this.element.classList

if (className) return classes.contains(className)

return Array.from(classes)
}

attributes(key?: string) {
const attributes = this.element.attributes
const attributeMap = {}
for (let i = 0; i < attributes.length; i++) {
const att = attributes.item(i)
attributeMap[att.localName] = att.value
attributes(): { [key: string]: string }
attributes(key: string): string
attributes(key?: string): { [key: string]: string } | string {
const attributes = Array.from(this.element.attributes)
const attributeMap: Record<string, string> = {}
for (const attribute of attributes) {
attributeMap[attribute.localName] = attribute.value
}

return key ? attributeMap[key] : attributeMap
Expand Down
12 changes: 9 additions & 3 deletions src/vue-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,22 @@ export class VueWrapper<T extends ComponentPublicInstance> {
return this.componentVM
}

props(selector?: string) {
props(): { [key: string]: any }
props(selector: string): any
props(selector?: string): { [key: string]: any } | any {
const props = this.componentVM.$props as { [key: string]: any }
return selector ? props[selector] : props
}

classes(className?: string) {
classes(): string[]
classes(className: string): boolean
classes(className?: string): string[] | boolean {
return new DOMWrapper(this.element).classes(className)
}

attributes(key?: string) {
attributes(): { [key: string]: string }
attributes(key: string): string
attributes(key?: string): { [key: string]: string } | string {
return new DOMWrapper(this.element).attributes(key)
}

Expand Down
16 changes: 16 additions & 0 deletions test-dts/wrapper.d-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,19 @@ expectType<SVGLineElement>(line.element)
// string selector
byClass = domWrapper.get('.todo')
expectType<Element>(byClass.element)

// attributes
expectType<{ [key: string]: string }>(wrapper.attributes())
expectType<string>(wrapper.attributes('key'))
expectType<{ [key: string]: string }>(domWrapper.attributes())
expectType<string>(domWrapper.attributes('key'))

// classes
expectType<Array<string>>(wrapper.classes())
expectType<boolean>(wrapper.classes('class'))
expectType<Array<string>>(domWrapper.classes())
expectType<boolean>(domWrapper.classes('class'))

// props
expectType<{ [key: string]: any }>(wrapper.props())
expectType<any>(wrapper.props('prop'))