소스 검색

add test for prop availablity in data() and created()

Evan You 11 년 전
부모
커밋
ac7af063d1
3개의 변경된 파일23개의 추가작업 그리고 2개의 파일을 삭제
  1. 2 2
      src/compiler/compile.js
  2. 4 0
      test/unit/specs/compiler/compile_spec.js
  3. 17 0
      test/unit/specs/instance/scope_spec.js

+ 2 - 2
src/compiler/compile.js

@@ -527,7 +527,7 @@ function makePropsLinkFn (props) {
             // one time binding
             value = vm.$parent.$get(prop.parentPath)
             if (_.assertProp(prop, value)) {
-              vm._data[path] = value
+              vm[path] = vm._data[path] = value
             }
           } else {
             // dynamic binding
@@ -544,7 +544,7 @@ function makePropsLinkFn (props) {
         // literal, cast it and just set once
         value = _.toBoolean(_.toNumber(prop.raw))
         if (_.assertProp(prop, value)) {
-          vm._data[path] = value
+          vm[path] = vm._data[path] = value
         }
       }
     }

+ 4 - 0
test/unit/specs/compiler/compile_spec.js

@@ -205,9 +205,13 @@ if (_.inBrowser) {
       // literal and one time should've been set on the _data
       // and numbers should be casted
       expect(Object.keys(vm._data).length).toBe(5)
+      expect(vm.a).toBe(1)
       expect(vm._data.a).toBe(1)
+      expect(vm.someOtherAttr).toBe(2)
       expect(vm._data.someOtherAttr).toBe(2)
+      expect(vm.onetime).toBe('from parent: a')
       expect(vm._data.onetime).toBe('from parent: a')
+      expect(vm.booleanLiteral).toBe('from parent: true')
       expect(vm._data.booleanLiteral).toBe('from parent: true')
       expect(vm._data.camelCase).toBeUndefined()
       // camelCase should've warn

+ 17 - 0
test/unit/specs/instance/scope_spec.js

@@ -65,6 +65,23 @@ describe('Instance Scope', function () {
       expect(vm.c).toBe(2)
     })
 
+    it('props should be available in data() and create()', function () {
+      var el = document.createElement('div')
+      el.setAttribute('c', '2')
+      var vm = new Vue({
+        el: el,
+        props: ['c'],
+        data: function () {
+          expect(this.c).toBe(2)
+          expect(this._data.c).toBe(2)
+        },
+        created: function () {
+          expect(this.c).toBe(2)
+          expect(this._data.c).toBe(2)
+        }
+      })
+    })
+
     it('replace $data', function () {
       var vm = new Vue({
         data: {