Skip to content

Commit

Permalink
chore: add overload on findComponent for component Options
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed May 19, 2022
1 parent 2500bc2 commit cc35833
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/baseWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { textContent } from './utils'
import type { TriggerOptions } from './createDomEvent'
import {
Component,
ComponentInternalInstance,
ComponentOptions,
ComponentPublicInstance,
ComputedOptions,
CreateComponentPublicInstance,
FunctionalComponent,
MethodOptions,
nextTick
} from 'vue'
import { createDOMEvent } from './createDomEvent'
Expand Down Expand Up @@ -100,6 +103,29 @@ export default abstract class BaseWrapper<ElementType extends Node>

// searching by string without specifying component results in WrapperLike object
findComponent<T extends never>(selector: string): WrapperLike
// Find Component Options aka plain object
findComponent<
Props,
RawBindings = any,
D = any,
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions
>(
selector: ComponentOptions<Props, RawBindings, D, C, M>
): VueWrapper<CreateComponentPublicInstance<Props, RawBindings, D, C, M>>
findComponent<T extends ComponentOptions>(
selector: string
): VueWrapper<
T extends ComponentOptions<
infer Props,
infer RawBindings,
infer D,
infer C,
infer M
>
? CreateComponentPublicInstance<Props, RawBindings, D, C, M>
: VueWrapper<CreateComponentPublicInstance>
>
// searching for component created via defineComponent results in VueWrapper of proper type
findComponent<T extends DefinedComponent>(
selector: T | Exclude<FindComponentSelector, FunctionalComponent>
Expand All @@ -116,9 +142,6 @@ export default abstract class BaseWrapper<ElementType extends Node>
findComponent<T extends ComponentPublicInstance>(
selector: T | FindComponentSelector
): VueWrapper<T>
// Find my CatchAll component
findComponent<T extends Component>(selector: T): DOMWrapper<Node>
findComponent<T extends Component>(selector: string): DOMWrapper<Element>
// catch all declaration
findComponent<T extends never>(selector: FindComponentSelector): WrapperLike

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface NameSelector {
export type FindAllComponentsSelector =
| DefinedComponent
| FunctionalComponent
| ComponentOptions
| NameSelector
| string
export type FindComponentSelector = RefSelector | FindAllComponentsSelector
Expand Down

0 comments on commit cc35833

Please sign in to comment.