Skip to content

Commit 1bb0884

Browse files
Merge branch 'master' into feat/find-by-component
# Conflicts: # src/mount.ts
2 parents 07a203b + 72bd1cd commit 1bb0884

File tree

10 files changed

+991
-78
lines changed

10 files changed

+991
-78
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ jobs:
3030
- run: yarn build
3131
env:
3232
CI: true
33+
- run: yarn tsd

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mixins | ✅ | (new!) nested in [`global`](https://vuejs.github.io/vue-test-util
5959
plugins | ✅ | (new!) nested in [`global`](https://vuejs.github.io/vue-test-utils-next-docs/api/#global)
6060
component | ✅ | (new!) nested in [`global`](https://vuejs.github.io/vue-test-utils-next-docs/api/#global)
6161
directives | ✅ | (new!) nested in [`global`](https://vuejs.github.io/vue-test-utils-next-docs/api/#global)
62-
stubs |
62+
stubs |
6363
attachToDocument | ❌| will rename to `attachTo`. See [here](https://github.com/vuejs/vue-test-utils/pull/1492)
6464
attrs | ❌ |
6565
scopedSlots | ⚰️ | scopedSlots are merged with slots in Vue 3
@@ -79,15 +79,15 @@ exists | ✅
7979
find | ✅ | only `querySelector` syntax is supported. `find(Comp)` under discussion [here](https://github.com/vuejs/vue-test-utils/issues/1498)
8080
emitted | ✅
8181
findAll | ✅ | see above. `.vm` is different to Vue 2. We are exploring options.
82+
get | ✅
8283
html | ✅
8384
setValue | ✅ | works for select, checkbox, radio button, input, textarea. Returns `nextTick`.
8485
text | ✅ |
8586
trigger | ✅ | returns `nextTick`. You can do `await wrapper.find('button').trigger('click')`
8687
setProps | ✅ |
88+
props | ✅
8789
setData | ❌ | has PR
8890
destroy | ❌
89-
get | ❌
90-
props | ❌
9191
contains | ⚰️| use `find`
9292
emittedByOrder | ⚰️ | use `emitted`
9393
setSelected | ⚰️ | now part of `setValue`

package.json

+14-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"module": "dist/vue-test-utils.esm-bundler.js",
1010
"files": [
1111
"dist",
12-
"README.md"
12+
"README.md",
13+
"dist/index.d.ts"
1314
],
1415
"dependencies": {
1516
"lodash": "^4.17.15"
@@ -21,8 +22,9 @@
2122
"@rollup/plugin-node-resolve": "^7.1.3",
2223
"@types/estree": "^0.0.42",
2324
"@types/jest": "^24.9.1",
25+
"@types/node": "12.12.35",
2426
"@types/lodash": "^4.14.149",
25-
"@vue/compiler-sfc": "^3.0.0-alpha.13",
27+
"@vue/compiler-sfc": "^3.0.0-beta.2",
2628
"babel-jest": "^25.2.3",
2729
"babel-preset-jest": "^25.2.1",
2830
"flush-promises": "^1.0.2",
@@ -33,8 +35,9 @@
3335
"rollup": "^1.31.1",
3436
"rollup-plugin-typescript2": "^0.26.0",
3537
"ts-jest": "^25.0.0",
38+
"tsd": "0.11.0",
3639
"typescript": "^3.7.5",
37-
"vue": "^3.0.0-alpha.13",
40+
"vue": "^3.0.0-beta.2",
3841
"vue-jest": "vuejs/vue-jest#next",
3942
"vuex": "^4.0.0-alpha.1"
4043
},
@@ -48,6 +51,7 @@
4851
},
4952
"scripts": {
5053
"test": "yarn jest --runInBand tests",
54+
"tsd": "tsd",
5155
"build": "yarn rollup -c rollup.config.js",
5256
"lint": "prettier -c --parser typescript \"(src|tests)/**/*.ts?(x)\"",
5357
"lint:fix": "yarn lint --write"
@@ -61,5 +65,12 @@
6165
"*.ts": [
6266
"prettier --parser=typescript --write"
6367
]
68+
},
69+
"tsd": {
70+
"directory": "test-dts",
71+
"compilerOptions": {
72+
"strict": false,
73+
"lib": ["esnext", "dom"]
74+
}
6475
}
6576
}

src/mount.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ interface MountingOptions {
4545
stubs?: Record<string, any>
4646
}
4747

48-
export function mount<T extends any>(
49-
originalComponent: any,
48+
export function mount<TestedComponent extends ComponentPublicInstance>(
49+
originalComponent: new () => TestedComponent,
50+
options?: MountingOptions
51+
): VueWrapper<TestedComponent>
52+
export function mount(
53+
originalComponent: Component,
5054
options?: MountingOptions
5155
): VueWrapper<any>
52-
export function mount<T extends ComponentPublicInstance>(
53-
originalComponent: new () => T,
56+
export function mount(
57+
originalComponent: any,
5458
options?: MountingOptions
55-
): VueWrapper<T> {
59+
): VueWrapper<any> {
5660
const component = { ...originalComponent }
5761

5862
// Reset the document.body
@@ -161,5 +165,5 @@ export function mount<T extends ComponentPublicInstance>(
161165
// mount the app!
162166
const app = vm.mount(el)
163167
const App = app.$refs[MOUNT_COMPONENT_REF] as T
164-
return createWrapper<T>(App, setProps)
168+
return createWrapper(App, setProps)
165169
}

src/stubs.ts

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ const resolveComponentStubByName = (
2323
const componentPascalName = pascalCase(componentName)
2424
const componentKebabName = kebabCase(componentName)
2525

26+
if (Array.isArray(stubs) && stubs.length) {
27+
// ['Foo', 'Bar'] => { Foo: true, Bar: true }
28+
stubs = stubs.reduce((acc, current) => {
29+
acc[current] = true
30+
return acc
31+
}, {})
32+
}
33+
2634
for (const [stubKey, value] of Object.entries(stubs)) {
2735
if (
2836
stubKey === componentPascalName ||

src/vue-wrapper.ts

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ export class VueWrapper<T extends ComponentPublicInstance>
4343
return this.componentVM
4444
}
4545

46+
props(selector?: string) {
47+
return selector
48+
? this.componentVM.$props[selector]
49+
: this.componentVM.$props
50+
}
51+
4652
classes(className?: string) {
4753
return new DOMWrapper(this.element).classes(className)
4854
}

test-dts/index.d-test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expectType } from 'tsd'
2+
import { defineComponent } from 'vue'
3+
import { mount } from '../src'
4+
5+
const App = defineComponent({
6+
props: {
7+
a: String
8+
},
9+
template: ''
10+
})
11+
12+
let wrapper = mount(App)
13+
expectType<string>(wrapper.vm.a)
14+
15+
const AppWithoutDefine = {
16+
props: {
17+
a: String
18+
},
19+
template: ''
20+
}
21+
22+
wrapper = mount(AppWithoutDefine)
23+
expectType<string>(wrapper.vm.a)

tests/mountingOptions/stubs.global.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ import { mount } from '../../src'
44
import Hello from '../components/Hello.vue'
55

66
describe('mounting options: stubs', () => {
7+
it('handles Array syntax', () => {
8+
const Foo = {
9+
name: 'Foo',
10+
render() {
11+
return h('p')
12+
}
13+
}
14+
const Component: ComponentOptions = {
15+
render() {
16+
return h(() => [h('div'), h(Foo)])
17+
}
18+
}
19+
20+
const wrapper = mount(Component, {
21+
global: {
22+
stubs: ['foo']
23+
}
24+
})
25+
26+
expect(wrapper.html()).toBe('<div></div><foo-stub></foo-stub>')
27+
})
728
it('stubs in a fragment', () => {
829
const Foo = {
930
name: 'Foo',

tests/props.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { mount } from '../src'
2+
import WithProps from './components/WithProps.vue'
3+
import Hello from './components/Hello.vue'
4+
5+
describe('props', () => {
6+
it('returns a single prop applied to a component', () => {
7+
const wrapper = mount(WithProps, { props: { msg: 'ABC' } })
8+
expect(wrapper.props('msg')).toEqual('ABC')
9+
})
10+
11+
it('returns all props applied to a component', () => {
12+
const wrapper = mount(WithProps, { props: { msg: 'ABC' } })
13+
expect(wrapper.props()).toEqual({ msg: 'ABC' })
14+
})
15+
16+
it('returns undefined if props does not exist', () => {
17+
const wrapper = mount(WithProps, { props: { msg: 'ABC' } })
18+
expect(wrapper.props('foo')).toEqual(undefined)
19+
})
20+
21+
it('returns empty object for components without props', () => {
22+
const wrapper = mount(Hello)
23+
expect(wrapper.props()).toEqual({})
24+
})
25+
})

0 commit comments

Comments
 (0)