diff --git a/docs/api/index.md b/docs/api/index.md index 5655b1969..6bb940f06 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -1075,7 +1075,6 @@ Finds a Vue Component instance and returns a `VueWrapper` if found. Returns `Err ```ts findComponent(selector: new () => T): VueWrapper findComponent(selector: FindComponentSelector): VueWrapper -findComponent(selector: any): VueWrapper ``` **Details:** @@ -1571,7 +1570,7 @@ Sets a value on DOM element. Including: **Signature:** ```ts -setValue(value: any, prop?: string): Promise +setValue(value: unknown, prop?: string): Promise ``` **Details:** diff --git a/src/utils.ts b/src/utils.ts index 6087405a5..01740e459 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -45,8 +45,8 @@ export const isObject = (obj: unknown): obj is Record => // https://stackoverflow.com/a/48218209 export const mergeDeep = ( - target: Record, - source: Record + target: Record, + source: Record ) => { if (!isObject(target) || !isObject(source)) { return source diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index 4e7a17679..28a728781 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -25,12 +25,12 @@ export class VueWrapper private componentVM: T private rootVM: ComponentPublicInstance | null private __app: App | null - private __setProps: ((props: Record) => void) | undefined + private __setProps: ((props: Record) => void) | undefined constructor( app: App | null, vm: ComponentPublicInstance, - setProps?: (props: Record) => void + setProps?: (props: Record) => void ) { super(vm?.$el) this.__app = app @@ -149,18 +149,14 @@ export class VueWrapper } findComponent( - selector: new () => T - ): VueWrapper - findComponent( - selector: FindComponentSelector - ): VueWrapper - findComponent( - selector: any + selector: FindComponentSelector | (new () => T) ): VueWrapper { if (typeof selector === 'object' && 'ref' in selector) { const result = this.vm.$refs[selector.ref] if (result) { return createWrapper(null, result as T) + } else { + return createWrapperError('VueWrapper') } } @@ -181,13 +177,7 @@ export class VueWrapper } getComponent( - selector: new () => T - ): Omit, 'exists'> - getComponent( - selector: FindComponentSelector - ): Omit, 'exists'> - getComponent( - selector: any + selector: FindComponentSelector | (new () => T) ): Omit, 'exists'> { const result = this.findComponent(selector) @@ -198,9 +188,9 @@ export class VueWrapper let message = 'Unable to get ' if (typeof selector === 'string') { message += `component with selector ${selector}` - } else if (selector.name) { + } else if ('name' in selector) { message += `component with name ${selector.name}` - } else if (selector.ref) { + } else if ('ref' in selector) { message += `component with ref ${selector.ref}` } else { message += 'specified component' @@ -235,12 +225,12 @@ export class VueWrapper return domWrapper.isVisible() } - setData(data: Record): Promise { + setData(data: Record): Promise { mergeDeep(this.componentVM.$data, data) return nextTick() } - setProps(props: Record): Promise { + setProps(props: Record): Promise { // if this VM's parent is not the root or if setProps does not exist, error out if (this.vm.$parent !== this.rootVM || !this.__setProps) { throw Error('You can only use setProps on your mounted component') @@ -249,7 +239,7 @@ export class VueWrapper return nextTick() } - setValue(value: any, prop?: string): Promise { + setValue(value: unknown, prop?: string): Promise { const propEvent = prop || 'modelValue' this.vm.$emit(`update:${propEvent}`, value) return this.vm.$nextTick() @@ -270,7 +260,7 @@ export class VueWrapper export function createWrapper( app: App | null, vm: ComponentPublicInstance, - setProps?: (props: Record) => void + setProps?: (props: Record) => void ): VueWrapper { return new VueWrapper(app, vm, setProps) }