Skip to content

Commit

Permalink
chore(types): improve types of vueWrapper (#823)
Browse files Browse the repository at this point in the history
* chore(types): improve types of vueWrapper

* chore(docs): sync doc with type changes
  • Loading branch information
xanf authored Aug 5, 2021
1 parent 97926a2 commit b916066
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
3 changes: 1 addition & 2 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,6 @@ Finds a Vue Component instance and returns a `VueWrapper` if found. Returns `Err
```ts
findComponent<T extends ComponentPublicInstance>(selector: new () => T): VueWrapper<T>
findComponent<T extends ComponentPublicInstance>(selector: FindComponentSelector): VueWrapper<T>
findComponent<T extends ComponentPublicInstance>(selector: any): VueWrapper<T>
```
**Details:**
Expand Down Expand Up @@ -1571,7 +1570,7 @@ Sets a value on DOM element. Including:
**Signature:**
```ts
setValue(value: any, prop?: string): Promise<void>
setValue(value: unknown, prop?: string): Promise<void>
```
**Details:**
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const isObject = (obj: unknown): obj is Record<string, any> =>

// https://stackoverflow.com/a/48218209
export const mergeDeep = (
target: Record<string, any>,
source: Record<string, any>
target: Record<string, unknown>,
source: Record<string, unknown>
) => {
if (!isObject(target) || !isObject(source)) {
return source
Expand Down
34 changes: 12 additions & 22 deletions src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export class VueWrapper<T extends ComponentPublicInstance>
private componentVM: T
private rootVM: ComponentPublicInstance | null
private __app: App | null
private __setProps: ((props: Record<string, any>) => void) | undefined
private __setProps: ((props: Record<string, unknown>) => void) | undefined

constructor(
app: App | null,
vm: ComponentPublicInstance,
setProps?: (props: Record<string, any>) => void
setProps?: (props: Record<string, unknown>) => void
) {
super(vm?.$el)
this.__app = app
Expand Down Expand Up @@ -149,18 +149,14 @@ export class VueWrapper<T extends ComponentPublicInstance>
}

findComponent<T extends ComponentPublicInstance>(
selector: new () => T
): VueWrapper<T>
findComponent<T extends ComponentPublicInstance>(
selector: FindComponentSelector
): VueWrapper<T>
findComponent<T extends ComponentPublicInstance>(
selector: any
selector: FindComponentSelector | (new () => T)
): VueWrapper<T> {
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')
}
}

Expand All @@ -181,13 +177,7 @@ export class VueWrapper<T extends ComponentPublicInstance>
}

getComponent<T extends ComponentPublicInstance>(
selector: new () => T
): Omit<VueWrapper<T>, 'exists'>
getComponent<T extends ComponentPublicInstance>(
selector: FindComponentSelector
): Omit<VueWrapper<T>, 'exists'>
getComponent<T extends ComponentPublicInstance>(
selector: any
selector: FindComponentSelector | (new () => T)
): Omit<VueWrapper<T>, 'exists'> {
const result = this.findComponent(selector)

Expand All @@ -198,9 +188,9 @@ export class VueWrapper<T extends ComponentPublicInstance>
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'
Expand Down Expand Up @@ -235,12 +225,12 @@ export class VueWrapper<T extends ComponentPublicInstance>
return domWrapper.isVisible()
}

setData(data: Record<string, any>): Promise<void> {
setData(data: Record<string, unknown>): Promise<void> {
mergeDeep(this.componentVM.$data, data)
return nextTick()
}

setProps(props: Record<string, any>): Promise<void> {
setProps(props: Record<string, unknown>): Promise<void> {
// 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')
Expand All @@ -249,7 +239,7 @@ export class VueWrapper<T extends ComponentPublicInstance>
return nextTick()
}

setValue(value: any, prop?: string): Promise<void> {
setValue(value: unknown, prop?: string): Promise<void> {
const propEvent = prop || 'modelValue'
this.vm.$emit(`update:${propEvent}`, value)
return this.vm.$nextTick()
Expand All @@ -270,7 +260,7 @@ export class VueWrapper<T extends ComponentPublicInstance>
export function createWrapper<T extends ComponentPublicInstance>(
app: App | null,
vm: ComponentPublicInstance,
setProps?: (props: Record<string, any>) => void
setProps?: (props: Record<string, unknown>) => void
): VueWrapper<T> {
return new VueWrapper<T>(app, vm, setProps)
}

0 comments on commit b916066

Please sign in to comment.