Skip to content

Commit 9f97e85

Browse files
committed
chore: shallowMount overloads
`shallowMount` now has the same signatures than `mount`, to which it delegates the heavy work.
1 parent 3630dd7 commit 9f97e85

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

src/mount.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,40 @@ export function mount(
245245
return createWrapper(app, App, setProps)
246246
}
247247

248+
// Functional component
249+
export function shallowMount<TestedComponent extends FunctionalComponent>(
250+
originalComponent: TestedComponent,
251+
options?: MountingOptions<any>
252+
): VueWrapper<ComponentPublicInstance>
253+
// Component declared with defineComponent
254+
export function shallowMount<TestedComponent extends ComponentPublicInstance>(
255+
originalComponent: { new (): TestedComponent } & Component,
256+
options?: MountingOptions<TestedComponent['$props']>
257+
): VueWrapper<TestedComponent>
258+
// Component declared with { props: { ... } }
259+
export function shallowMount<
260+
TestedComponent extends ComponentOptionsWithObjectProps
261+
>(
262+
originalComponent: TestedComponent,
263+
options?: MountingOptions<ExtractPropTypes<TestedComponent['props'], false>>
264+
): VueWrapper<ExtractComponent<TestedComponent>>
265+
// Component declared with { props: [] }
266+
export function shallowMount<
267+
TestedComponent extends ComponentOptionsWithArrayProps
268+
>(
269+
originalComponent: TestedComponent,
270+
options?: MountingOptions<Record<string, any>>
271+
): VueWrapper<ExtractComponent<TestedComponent>>
272+
// Component declared with no props
273+
export function shallowMount<
274+
TestedComponent extends ComponentOptionsWithoutProps,
275+
ComponentT extends ComponentOptionsWithoutProps & {}
276+
>(
277+
originalComponent: ComponentT extends { new (): any } ? never : ComponentT,
278+
options?: MountingOptions<never>
279+
): VueWrapper<ExtractComponent<TestedComponent>>
248280
export function shallowMount(
249-
originalComponent,
281+
originalComponent: any,
250282
options?: MountingOptions<any>
251283
) {
252284
return mount(originalComponent, { ...options, shallow: true })

test-dts/shallowMount.d-test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { expectError, expectType } from 'tsd'
2+
import { defineComponent } from 'vue'
3+
import { shallowMount } from '../src'
4+
5+
const AppWithDefine = defineComponent({
6+
props: {
7+
a: {
8+
type: String,
9+
required: true
10+
},
11+
b: Number
12+
},
13+
template: ''
14+
})
15+
16+
// accept props
17+
let wrapper = shallowMount(AppWithDefine, {
18+
props: { a: 'Hello', b: 2 }
19+
})
20+
// vm is properly typed
21+
expectType<string>(wrapper.vm.a)
22+
23+
// can receive extra props
24+
// ideally, it should not
25+
// but the props have type { a: string } & VNodeProps
26+
// which allows any property
27+
shallowMount(AppWithDefine, {
28+
props: { a: 'Hello', c: 2 }
29+
})
30+
31+
// wrong prop type should not compile
32+
expectError(
33+
shallowMount(AppWithDefine, {
34+
props: { a: 2 }
35+
})
36+
)
37+
38+
const AppWithProps = {
39+
props: {
40+
a: {
41+
type: String,
42+
required: true
43+
}
44+
},
45+
template: ''
46+
}
47+
48+
// accept props
49+
wrapper = shallowMount(AppWithProps, {
50+
props: { a: 'Hello' }
51+
})
52+
// vm is properly typed
53+
expectType<string>(wrapper.vm.a)
54+
55+
// can't receive extra props
56+
expectError(
57+
shallowMount(AppWithProps, {
58+
props: { a: 'Hello', b: 2 }
59+
})
60+
)
61+
62+
// wrong prop type should not compile
63+
expectError(
64+
shallowMount(AppWithProps, {
65+
props: { a: 2 }
66+
})
67+
)
68+
69+
const AppWithArrayProps = {
70+
props: ['a'],
71+
template: ''
72+
}
73+
74+
// accept props
75+
wrapper = shallowMount(AppWithArrayProps, {
76+
props: { a: 'Hello' }
77+
})
78+
// vm is properly typed
79+
expectType<string>(wrapper.vm.a)
80+
81+
// can receive extra props
82+
// as they are declared as `string[]`
83+
shallowMount(AppWithArrayProps, {
84+
props: { a: 'Hello', b: 2 }
85+
})
86+
87+
const AppWithoutProps = {
88+
template: ''
89+
}
90+
91+
// can't receive extra props
92+
expectError(
93+
(wrapper = shallowMount(AppWithoutProps, {
94+
props: { b: 'Hello' }
95+
}))
96+
)
97+
98+
// except if explicitly cast
99+
shallowMount(AppWithoutProps, {
100+
props: { b: 'Hello' } as never
101+
})

0 commit comments

Comments
 (0)