Skip to content

Commit 64cbd16

Browse files
authored
Merge pull request #90 from vuejs/feat/document-attrs-fallback
feat: Explain how to replace attrs with props
2 parents a0f87d6 + e02fcea commit 64cbd16

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ component | ✅ | (new!) nested in [`global`](https://vuejs.github.io/vue-test-u
6262
directives | ✅ | (new!) nested in [`global`](https://vuejs.github.io/vue-test-utils-next-docs/api/#global)
6363
stubs | ✅
6464
attachToDocument |✅| renamed `attachTo`. See [here](https://github.com/vuejs/vue-test-utils/pull/1492)
65-
attrs | ❌ |
65+
attrs | ⚰️ | use `props` instead, it assigns both attributes and props.
6666
scopedSlots | ⚰️ | scopedSlots are merged with slots in Vue 3
6767
context | ⚰️ | different from Vue 2, does not make sense anymore.
6868
localVue | ⚰️ | may not make sense anymore since we do not mutate the global Vue instance in Vue 3.
Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,62 @@
11
import { defineComponent, h } from 'vue'
2-
2+
import WithProps from '../components/WithProps.vue'
33
import { mount } from '../../src'
44

5-
test('mounting options - passes props', () => {
6-
const Component = defineComponent({
7-
props: {
8-
message: {
9-
type: String,
10-
required: true
5+
describe('mountingOptions.props', () => {
6+
test('passes props', () => {
7+
const Component = defineComponent({
8+
props: {
9+
message: {
10+
type: String,
11+
required: true
12+
}
13+
},
14+
15+
render() {
16+
return h('div', {}, `Message is ${this.message}`)
1117
}
12-
},
18+
})
1319

14-
render() {
15-
return h('div', {}, `Message is ${this.message}`)
16-
}
20+
const wrapper = mount(Component, {
21+
props: {
22+
message: 'Hello'
23+
}
24+
})
25+
expect(wrapper.text()).toBe('Message is Hello')
26+
})
27+
28+
test('assigns extra attributes on components', () => {
29+
const wrapper = mount(WithProps, {
30+
props: {
31+
class: 'HelloFromTheOtherSide',
32+
id: 'hello',
33+
disabled: true,
34+
msg: 'Hello World'
35+
}
36+
})
37+
38+
expect(wrapper.attributes()).toEqual({
39+
class: 'HelloFromTheOtherSide',
40+
disabled: 'true',
41+
id: 'hello'
42+
})
43+
44+
expect(wrapper.props()).toEqual({
45+
msg: 'Hello World'
46+
})
1747
})
1848

19-
const wrapper = mount(Component, {
20-
props: {
21-
message: 'Hello'
49+
test('assigns event listeners', async () => {
50+
const Component = {
51+
template: '<button @click="$emit(\'customEvent\', true)">Click</button>'
2252
}
53+
const onCustomEvent = jest.fn()
54+
const wrapper = mount(Component, { props: { onCustomEvent } })
55+
const button = wrapper.find('button')
56+
await button.trigger('click')
57+
await button.trigger('click')
58+
await button.trigger('click')
59+
60+
expect(onCustomEvent).toHaveBeenCalledTimes(3)
2361
})
24-
expect(wrapper.text()).toBe('Message is Hello')
2562
})

0 commit comments

Comments
 (0)