Skip to content

Commit 79e89e6

Browse files
committed
more changes
1 parent 2e0e1a6 commit 79e89e6

File tree

8 files changed

+119
-31
lines changed

8 files changed

+119
-31
lines changed

src/baseWrapper.ts

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { textContent } from './utils'
22
import type { TriggerOptions } from './createDomEvent'
33
import {
4+
ComponentDefineOptions,
5+
ComponentInjectOptions,
6+
ComponentInstance,
47
ComponentInternalInstance,
58
ComponentOptions,
9+
ComponentOptionsMixin,
610
ComponentPublicInstance,
711
ComputedOptions,
812
CreateComponentPublicInstance,
13+
EmitsOptions,
914
FunctionalComponent,
1015
MethodOptions,
16+
SlotsType,
1117
nextTick
1218
} from 'vue'
1319
import { createDOMEvent } from './createDomEvent'
@@ -108,16 +114,58 @@ export default abstract class BaseWrapper<ElementType extends Node>
108114

109115
// searching by string without specifying component results in WrapperLike object
110116
findComponent<T extends never>(selector: string): WrapperLike
117+
111118
// Find Component Options aka plain object
112119
findComponent<
113-
Props,
114-
RawBindings = any,
115-
D = any,
116-
C extends ComputedOptions = ComputedOptions,
117-
M extends MethodOptions = MethodOptions
120+
Props = {},
121+
RawBindings = {},
122+
D = {},
123+
C extends ComputedOptions = {},
124+
M extends MethodOptions = {},
125+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
126+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
127+
E extends EmitsOptions = {},
128+
EE extends string = string,
129+
I extends ComponentInjectOptions = {},
130+
II extends string = string,
131+
S extends SlotsType = {},
132+
Options = {}
118133
>(
119-
selector: ComponentOptions<Props, RawBindings, D, C, M>
120-
): VueWrapper<CreateComponentPublicInstance<Props, RawBindings, D, C, M>>
134+
selector: ComponentDefineOptions<
135+
Props,
136+
RawBindings,
137+
D,
138+
C,
139+
M,
140+
Mixin,
141+
Extends,
142+
E,
143+
EE,
144+
I,
145+
II,
146+
S,
147+
Options
148+
>
149+
): VueWrapper<
150+
Props extends DefinedComponent
151+
? ComponentInstance<Props>
152+
: CreateComponentPublicInstance<
153+
Props,
154+
RawBindings,
155+
D,
156+
C,
157+
M,
158+
Mixin,
159+
Extends,
160+
E,
161+
Props,
162+
{},
163+
true,
164+
I,
165+
S,
166+
Options
167+
>
168+
>
121169
findComponent<T extends ComponentOptions>(
122170
selector: string
123171
): VueWrapper<
@@ -129,17 +177,19 @@ export default abstract class BaseWrapper<ElementType extends Node>
129177
infer M
130178
>
131179
? CreateComponentPublicInstance<Props, RawBindings, D, C, M>
132-
: VueWrapper<CreateComponentPublicInstance>
180+
: CreateComponentPublicInstance
133181
>
134-
// searching for component created via defineComponent results in VueWrapper of proper type
135-
findComponent<T extends DefinedComponent>(
136-
selector: T | Exclude<FindComponentSelector, FunctionalComponent>
137-
): VueWrapper<InstanceType<T>>
138182
// searching for functional component results in DOMWrapper
139183
findComponent<T extends FunctionalComponent>(selector: T): DOMWrapper<Node>
140184
findComponent<T extends FunctionalComponent>(
141185
selector: string
142186
): DOMWrapper<Element>
187+
188+
// searching for component created via defineComponent results in VueWrapper of proper type
189+
findComponent<T extends DefinedComponent>(
190+
selector: T | Exclude<FindComponentSelector, FunctionalComponent>
191+
): VueWrapper<ComponentInstance<T>>
192+
143193
// searching by name or ref always results in VueWrapper
144194
findComponent<T extends never>(
145195
selector: NameSelector | RefSelector
@@ -185,7 +235,7 @@ export default abstract class BaseWrapper<ElementType extends Node>
185235
findAllComponents<T extends never>(selector: string): WrapperLike[]
186236
findAllComponents<T extends DefinedComponent>(
187237
selector: T | Exclude<FindAllComponentsSelector, FunctionalComponent>
188-
): VueWrapper<InstanceType<T>>[]
238+
): VueWrapper<ComponentInstance<T>>[]
189239
findAllComponents<T extends FunctionalComponent>(
190240
selector: T
191241
): DOMWrapper<Node>[]
@@ -292,7 +342,7 @@ export default abstract class BaseWrapper<ElementType extends Node>
292342
getComponent<T extends never>(selector: string): Omit<WrapperLike, 'exists'>
293343
getComponent<T extends DefinedComponent>(
294344
selector: T | Exclude<FindComponentSelector, FunctionalComponent>
295-
): Omit<VueWrapper<InstanceType<T>>, 'exists'>
345+
): Omit<VueWrapper<ComponentInstance<T>>, 'exists'>
296346
// searching for functional component results in DOMWrapper
297347
getComponent<T extends FunctionalComponent>(
298348
selector: T | string

src/interfaces/wrapperLike.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
RefSelector
99
} from '../types'
1010
import { VueWrapper } from '../vueWrapper'
11-
import { ComponentPublicInstance, FunctionalComponent } from 'vue'
11+
import {
12+
ComponentInstance,
13+
ComponentPublicInstance,
14+
FunctionalComponent
15+
} from 'vue'
1216
import type { DOMWrapper } from '../domWrapper'
1317

1418
export default interface WrapperLike {
@@ -35,7 +39,7 @@ export default interface WrapperLike {
3539
findComponent<T extends never>(selector: string): WrapperLike
3640
findComponent<T extends DefinedComponent>(
3741
selector: T | Exclude<FindComponentSelector, FunctionalComponent>
38-
): VueWrapper<InstanceType<T>>
42+
): VueWrapper<ComponentInstance<T>>
3943
findComponent<T extends FunctionalComponent>(
4044
selector: T | string
4145
): DOMWrapper<Element>
@@ -50,7 +54,7 @@ export default interface WrapperLike {
5054
findAllComponents<T extends never>(selector: string): WrapperLike[]
5155
findAllComponents<T extends DefinedComponent>(
5256
selector: T | Exclude<FindAllComponentsSelector, FunctionalComponent>
53-
): VueWrapper<InstanceType<T>>[]
57+
): VueWrapper<ComponentInstance<T>>[]
5458
findAllComponents<T extends FunctionalComponent>(
5559
selector: string
5660
): DOMWrapper<Element>[]
@@ -79,7 +83,7 @@ export default interface WrapperLike {
7983
getComponent<T extends never>(selector: string): Omit<WrapperLike, 'exists'>
8084
getComponent<T extends DefinedComponent>(
8185
selector: T | Exclude<FindComponentSelector, FunctionalComponent>
82-
): Omit<VueWrapper<InstanceType<T>>, 'exists'>
86+
): Omit<VueWrapper<ComponentInstance<T>>, 'exists'>
8387
// searching for functional component results in DOMWrapper
8488
getComponent<T extends FunctionalComponent>(
8589
selector: T | string

src/mount.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ export type ComponentMountingOptions<T, P> = Omit<
3535
}
3636
} & Record<string, unknown>
3737

38+
// export function mount<T extends { new (): { $props: any } }>(
39+
// originalComponent: T,
40+
// options?: ComponentMountingOptions<T, ComponentProps<T>>
41+
// ): //VueWrapper<ComponentInstance<T>>
42+
// {
43+
// props: ComponentProps<T>
44+
// }
45+
3846
// defineComponent
3947
export function mount<
4048
T extends DefineComponent<
@@ -54,7 +62,6 @@ export function mount<
5462
originalComponent: T,
5563
options?: ComponentMountingOptions<T, ComponentProps<T>>
5664
): VueWrapper<ComponentInstance<T>>
57-
5865
// implementation
5966
export function mount(
6067
inputComponent: any,

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,4 @@ export type VueNode<T extends Node = Node> = T & {
166166

167167
export type VueElement = VueNode<Element>
168168

169-
export type DefinedComponent = new (...args: any[]) => any
169+
export type DefinedComponent = Component

src/vueWrapper.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
App,
44
ComponentPublicInstance,
55
VNode,
6-
ExtractComponentEmits
6+
ExtractComponentEmits,
7+
ComponentCustomProperties
78
} from 'vue'
89

910
import { config } from './config'
@@ -94,8 +95,23 @@ type ResolveEmitRecord<T> = ExtractComponentEmits<T> extends infer E
9495
}
9596
: never
9697

98+
declare const aaa: keyof Omit<
99+
ComponentPublicInstance,
100+
keyof ComponentCustomProperties
101+
>
102+
103+
// type BetterKeys = keyof Omit<
104+
// ComponentPublicInstance,
105+
// keyof ComponentCustomProperties
106+
// >
107+
// export type ComponentInstance = {
108+
// [K in keyof ComponentPublicInstance]?: any
109+
// } & Record<PropertyKey, any>
110+
97111
export class VueWrapper<
98-
T extends ComponentPublicInstance = ComponentPublicInstance
112+
T extends Omit<ComponentPublicInstance, '$emit'> & {
113+
$emit: any
114+
} = ComponentPublicInstance
99115
> extends BaseWrapper<Node> {
100116
private readonly componentVM: T
101117
private readonly rootVM: ComponentPublicInstance | undefined | null

test-dts/findComponent.d-test.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expectType } from './index'
2-
import { defineComponent } from 'vue'
2+
import { ComponentInstance, defineComponent } from 'vue'
33
import { DOMWrapper, mount, VueWrapper } from '../src'
44
import WrapperLike from '../src/interfaces/wrapperLike'
55

@@ -31,11 +31,13 @@ const wrapper = mount(AppWithDefine)
3131

3232
// find by type - component definition
3333
const componentByType = wrapper.findComponent(ComponentToFind)
34-
expectType<VueWrapper<InstanceType<typeof ComponentToFind>>>(componentByType)
34+
expectType<VueWrapper<ComponentInstance<typeof ComponentToFind>>>(
35+
componentByType
36+
)
3537

3638
// find by type - component definition with emits
3739
const componentWithEmitsByType = wrapper.findComponent(ComponentWithEmits)
38-
expectType<VueWrapper<InstanceType<typeof ComponentWithEmits>>>(
40+
expectType<VueWrapper<ComponentInstance<typeof ComponentWithEmits>>>(
3941
componentWithEmitsByType
4042
)
4143

@@ -50,7 +52,7 @@ expectType<WrapperLike>(componentByString)
5052
// findi by string with specifying component
5153
const componentByStringWithParam =
5254
wrapper.findComponent<typeof ComponentToFind>('.foo')
53-
expectType<VueWrapper<InstanceType<typeof ComponentToFind>>>(
55+
expectType<VueWrapper<ComponentInstance<typeof ComponentToFind>>>(
5456
componentByStringWithParam
5557
)
5658

@@ -66,7 +68,7 @@ expectType<VueWrapper>(componentByRef)
6668
const componentByRefWithType = wrapper.findComponent<typeof ComponentToFind>({
6769
ref: 'foo'
6870
})
69-
expectType<VueWrapper<InstanceType<typeof ComponentToFind>>>(
71+
expectType<VueWrapper<ComponentInstance<typeof ComponentToFind>>>(
7072
componentByRefWithType
7173
)
7274

@@ -78,6 +80,12 @@ expectType<VueWrapper>(componentByName)
7880
const componentByNameWithType = wrapper.findComponent<typeof ComponentToFind>({
7981
name: 'foo'
8082
})
81-
expectType<VueWrapper<InstanceType<typeof ComponentToFind>>>(
83+
84+
85+
declare const aaa : ComponentInstance<typeof ComponentToFind>
86+
aaa.$props
87+
componentByNameWithType.vm.$props
88+
89+
expectType<VueWrapper<ComponentInstance<typeof ComponentToFind>>>(
8290
componentByNameWithType
8391
)

test-dts/mount.d-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ mount(AppWithProps, {
8585

8686
expectError(
8787
mount(AppWithProps, {
88-
// @ts-expect-error wrong prop type should not compile
88+
// @ts-expect-error wrong prop type should not compile
8989
props: { a: 2 }
9090
})
9191
)

test-dts/wrapper.d-test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { defineComponent } from 'vue'
33
import { mount } from '../src'
44

55
const AppWithDefine = defineComponent({
6+
emits: {
7+
increment: (arg: { count: number }) => true
8+
},
69
template: ''
710
})
811

@@ -65,10 +68,10 @@ expectType<Element | undefined>(byClassArray[0].element)
6568

6669
// event name without specific type
6770
let incrementEventWithoutType = wrapper.emitted('increment')
68-
expectType<unknown[][] | undefined>(incrementEventWithoutType)
71+
expectType<{ count: number }[] | undefined>(incrementEventWithoutType)
6972

7073
// event name
71-
let incrementEvent = wrapper.emitted<{ count: number }>('increment')
74+
let incrementEvent = wrapper.emitted('increment')
7275
expectType<{ count: number }[] | undefined>(incrementEvent)
7376
expectType<{ count: number }>(incrementEvent![0])
7477

0 commit comments

Comments
 (0)