8000 add test for prop availablity in data() and created() · TonyXY/vue@ac7af06 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac7af06

Browse files
committed
add test for prop availablity in data() and created()
1 parent 8f6dfce commit ac7af06

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/compiler/compile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ function makePropsLinkFn (props) {
527527
// one time binding
528528
value = vm.$parent.$get(prop.parentPath)
529529
if (_.assertProp(prop, value)) {
530-
vm._data[path] = value
530+
vm[path] = vm._data[path] = value
531531
}
532532
} else {
533533
// dynamic binding
@@ -544,7 +544,7 @@ function makePropsLinkFn (props) {
544544
// literal, cast it and just set once
545545
value = _.toBoolean(_.toNumber(prop.raw))
546546
if (_.assertProp(prop, value)) {
547-
vm._data[path] = value
547+
vm[path] = vm._data[path] = value
548548
}
549549
}
550550
}

test/unit/specs/compiler/compile_spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,13 @@ if (_.inBrowser) {
205205
// literal and one time should've been set on the _data
206206
// and numbers should be casted
207207
expect(Object.keys(vm._data).length).toBe(5)
208+
expect(vm.a).toBe(1)
208209
expect(vm._data.a).toBe(1)
210+
expect(vm.someOtherAttr).toBe(2)
209211
expect(vm._data.someOtherAttr).toBe(2)
212+
expect(vm.onetime).toBe('from parent: a')
210213
expect(vm._data.onetime).toBe('from parent: a')
214+
expect(vm.booleanLiteral).toBe('from parent: true')
211215
expect(vm._data.booleanLiteral).toBe('from parent: true')
212216
expect(vm._data.camelCase).toBeUndefined()
213217
// camelCase should've warn

test/unit/specs/instance/scope_spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ describe('Instance Scope', function () {
6565
expect(vm.c).toBe(2)
6666
})
6767

68+
it('props should be available in data() and create()', function () {
69+
var el = document.createElement('div')
70+
el.setAttribute('c', '2')
71+
var vm = new Vue({
72+
el: el,
73+
props: ['c'],
74+
data: function () {
75+
expect(this.c).toBe(2)
76+
expect(this._data.c).toBe(2)
77+
},
78+
created: function () {
79+
expect(this.c).toBe(2)
80+
expect(this._data.c).toBe(2)
81+
}
82+
})
83+
})
84+
6885
it('replace $data', function () {
6986
var vm = new Vue({
7087
data: {

0 commit comments

Comments
 (0)
0