Skip to content

Commit 5bcf574

Browse files
authored
fix: include default props in props object (#716)
1 parent 7fa2fb3 commit 5bcf574

File tree

11 files changed

+64
-43
lines changed

11 files changed

+64
-43
lines changed

Diff for: packages/test-utils/src/wrapper.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,18 @@ export default class Wrapper implements BaseWrapper {
398398
if (!this.vm) {
399399
throwError('wrapper.props() must be called on a Vue instance')
400400
}
401-
// $props object does not exist in Vue 2.1.x, so use $options.propsData instead
402-
let _props
403-
if (this.vm && this.vm.$options && this.vm.$options.propsData) {
404-
_props = this.vm.$options.propsData
405-
} else {
406-
// $FlowIgnore
407-
_props = this.vm.$props
401+
402+
const props = {}
403+
// $FlowIgnore
404+
const keys = this.vm.$options._propKeys
405+
406+
if (keys) {
407+
keys.forEach(key => {
408+
// $FlowIgnore
409+
props[key] = this.vm[key]
410+
})
408411
}
409-
return _props || {} // Return an empty object if no props exist
412+
return props
410413
}
411414

412415
/**
@@ -518,26 +521,21 @@ export default class Wrapper implements BaseWrapper {
518521
if (this.isFunctionalComponent) {
519522
throwError('wrapper.setProps() cannot be called on a functional component')
520523
}
521-
if (!this.isVueInstance() || !this.vm) {
524+
if (!this.isVm) {
522525
throwError('wrapper.setProps() can only be called on a Vue instance')
523526
}
524-
if (this.vm && this.vm.$options && !this.vm.$options.propsData) {
525-
this.vm.$options.propsData = {}
526-
}
527+
527528
Object.keys(data).forEach((key) => {
528529
// Ignore properties that were not specified in the component options
529530
// $FlowIgnore : Problem with possibly null this.vm
530-
if (!this.vm.$options._propKeys || !this.vm.$options._propKeys.some(prop => prop === key)) {
531+
if (!this.vm.$options._propKeys ||
532+
!this.vm.$options._propKeys.some(prop => prop === key)) {
531533
throwError(`wrapper.setProps() called with ${key} property which is not defined on component`)
532534
}
533535

534536
// $FlowIgnore : Problem with possibly null this.vm
535537
if (this.vm._props) {
536538
this.vm._props[key] = data[key]
537-
// $FlowIgnore : Problem with possibly null this.vm.$props
538-
this.vm.$props[key] = data[key]
539-
// $FlowIgnore : Problem with possibly null this.vm.$options
540-
this.vm.$options.propsData[key] = data[key]
541539
} else {
542540
// $FlowIgnore : Problem with possibly null this.vm
543541
this.vm[key] = data[key]

Diff for: scripts/test-compat-all.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
scripts/test-compat.sh "2.0.8"
6+
scripts/test-compat.sh "2.1.10"
7+
scripts/test-compat.sh "2.2.6"
8+
scripts/test-compat.sh "2.3.4"
9+
scripts/test-compat.sh "2.4.2"
10+
scripts/test-compat.sh "2.5.13"

Diff for: test/specs/components/RouterLink.spec.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ describeWithShallowAndMount('RouterLinkStub', (mountingMethod) => {
88
<div>
99
<router-link
1010
to="to1"
11-
tag="tag1"
12-
exact="exact1"
13-
append="append1"
14-
replace="replace1"
11+
tag="a"
1512
activeClass="activeClass1"
1613
exactActiveClass="exactActiveClass1"
1714
event="event1"
15+
exact
16+
append
17+
replace
1818
/>
1919
</div>
2020
`
@@ -24,12 +24,13 @@ describeWithShallowAndMount('RouterLinkStub', (mountingMethod) => {
2424
RouterLink: RouterLinkStub
2525
}
2626
})
27+
2728
const routerLink = wrapper.find(RouterLinkStub)
2829
expect(routerLink.props().to).to.equal('to1')
29-
expect(routerLink.props().tag).to.equal('tag1')
30-
expect(routerLink.props().exact).to.equal('exact1')
31-
expect(routerLink.props().append).to.equal('append1')
32-
expect(routerLink.props().replace).to.equal('replace1')
30+
expect(routerLink.props().tag).to.equal('a')
31+
expect(routerLink.props().exact).to.equal(true)
32+
expect(routerLink.props().append).to.equal(true)
33+
expect(routerLink.props().replace).to.equal(true)
3334
expect(routerLink.props().activeClass).to.equal('activeClass1')
3435
expect(routerLink.props().exactActiveClass).to.equal('exactActiveClass1')
3536
expect(routerLink.props().event).to.equal('event1')
@@ -39,7 +40,7 @@ describeWithShallowAndMount('RouterLinkStub', (mountingMethod) => {
3940
const TestComponent = {
4041
template: `
4142
<div>
42-
<router-link>some text</router-link>
43+
<router-link to="/">some text</router-link>
4344
</div>
4445
`
4546
}

Diff for: test/specs/mounting-options/localVue.spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ describeWithMountingMethods('options.localVue', (mountingMethod) => {
1515
const TestComponent = {
1616
template: `
1717
<div>{{test}}</div>
18-
`
18+
`,
19+
data: { test: '' }
1920
}
2021
const localVue = Vue.extend()
2122
localVue.version = '2.3'

Diff for: test/specs/wrapper/destroy.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ import { describeWithShallowAndMount } from '~resources/utils'
33
import sinon from 'sinon'
44

55
describeWithShallowAndMount('destroy', (mountingMethod) => {
6-
it('should trigger beforeDestroy ', () => {
6+
it('triggers beforeDestroy ', () => {
77
const spy = sinon.stub()
88
mountingMethod({
9-
render: null,
9+
render: () => {},
1010
beforeDestroy () {
1111
spy()
1212
}
1313
}).destroy()
1414
expect(spy.calledOnce).to.equal(true)
1515
})
1616

17-
it('should trigger destroy ', () => {
17+
it('triggers destroy ', () => {
1818
const spy = sinon.stub()
1919
mountingMethod({
20-
render: null,
20+
render: () => {},
2121
destroyed () {
2222
spy()
2323
}
2424
}).destroy()
2525
expect(spy.calledOnce).to.equal(true)
2626
})
2727

28-
it.skip('should remove element from document.body', () => {
28+
it.skip('removes element from document.body', () => {
2929
const compiled = compileToFunctions('<div></div>')
3030
const wrapper = mountingMethod(compiled, { attachToDocument: true })
3131
expect(wrapper.vm.$el.parentNode).to.equal(document.body)

Diff for: test/specs/wrapper/emitted.spec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ describeWithShallowAndMount('emitted', (mountingMethod) => {
6363
},
6464
mounted () {
6565
this.$emit('bar', 1, 2)
66-
}
66+
},
67+
render: () => {}
6768
})
6869

6970
expect(wrapper.emitted().foo).to.eql([[]])
@@ -74,12 +75,14 @@ describeWithShallowAndMount('emitted', (mountingMethod) => {
7475
const localVue = createLocalVue()
7576

7677
const wrapper1 = mountingMethod({
78+
render: () => {},
7779
beforeCreate () {
7880
this.$emit('foo')
7981
}
8082
}, { localVue })
8183

8284
const wrapper2 = mountingMethod({
85+
render: () => {},
8386
mounted () {
8487
this.$emit('bar')
8588
}

Diff for: test/specs/wrapper/find.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ describeWithShallowAndMount('find', (mountingMethod) => {
240240
it('returns error Wrapper if Vue component is below Wrapper', () => {
241241
const AComponent = {
242242
render: () => {},
243-
name: 'a component'
243+
name: 'a-component'
244244
}
245245
const localVue = createLocalVue()
246246
localVue.component('a-component', AComponent)

Diff for: test/specs/wrapper/hasClass.spec.js

-7
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ describeWithShallowAndMount('hasClass', (mountingMethod) => {
4040
expect(wrapper.hasClass('color-red')).to.equal(true)
4141
})
4242

43-
it('returns false if wrapper does not contain element', () => {
44-
const wrapper = mountingMethod({ render: (h) => h('div.a-class.b-class') })
45-
const div = wrapper.find('div')
46-
div.element = null
47-
expect(wrapper.hasClass('a-class b-class')).to.equal(false)
48-
})
49-
5043
it('returns true when the element contains multiple classes', () => {
5144
const compiled = compileToFunctions('<div class="a-class b-class" />')
5245
const wrapper = mountingMethod(compiled)

Diff for: test/specs/wrapper/props.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ describeWithShallowAndMount('props', (mountingMethod) => {
3636
expect(wrapper.props()).to.eql({ prop1: {}, prop2: 'val2' }) // fail
3737
})
3838

39+
it('returns default props', () => {
40+
const TestComponent = {
41+
render: () => {},
42+
props: {
43+
message: {
44+
default: () => 'hello'
45+
}
46+
}
47+
}
48+
const wrapper = mountingMethod(TestComponent)
49+
expect(wrapper.props().message).to.equal('hello')
50+
})
51+
3952
itSkipIf(!functionalSFCsSupported,
4053
'works correctly a functional component', () => {
4154
const FunctionalComponent = {

Diff for: test/specs/wrapper/setComputed.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describeWithShallowAndMount('setComputed', (mountingMethod) => {
3939

4040
it('updates vm computed value', () => {
4141
const TestComponent = {
42+
render: () => {},
4243
data () {
4344
return {
4445
a: 1

Diff for: test/specs/wrapper/setData.spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
133133
expect(wrapper.text()).to.equal('There is no message yet')
134134
})
135135

136-
it('should update an existing property in a data object', () => {
136+
it('updates an existing property in a data object', () => {
137137
const TestComponent = {
138138
data: () => ({
139139
anObject: {
@@ -142,7 +142,8 @@ describeWithShallowAndMount('setData', (mountingMethod) => {
142142
},
143143
propB: 'b'
144144
}
145-
})
145+
}),
146+
render: () => {}
146147
}
147148
const wrapper = mountingMethod(TestComponent)
148149
wrapper.setData({

0 commit comments

Comments
 (0)