github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/test/unit/specs/api/data_spec.js (about) 1 var Vue = require('src') 2 var _ = require('src/util') 3 var nextTick = _.nextTick 4 5 describe('Data API', function () { 6 var vm 7 beforeEach(function () { 8 var el = document.createElement('div') 9 el.setAttribute('prop', 'foo') 10 vm = new Vue({ 11 el: el, 12 props: ['prop'], 13 data: { 14 a: 1, 15 b: { 16 c: 2 17 } 18 }, 19 filters: { 20 double: function (v) { 21 return v * 2 22 } 23 }, 24 computed: { 25 d: function () { 26 return this.a + 1 27 } 28 } 29 }) 30 }) 31 32 it('$get', function () { 33 expect(vm.$get('a')).toBe(1) 34 expect(vm.$get('b["c"]')).toBe(2) 35 expect(vm.$get('a + b.c')).toBe(3) 36 expect(vm.$get('c')).toBeUndefined() 37 // invalid, should warn 38 vm.$get('a(') 39 expect('Invalid expression').toHaveBeenWarned() 40 }) 41 42 it('$set', function () { 43 vm.$set('a', 2) 44 expect(vm.a).toBe(2) 45 vm.$set('b["c"]', 3) 46 expect(vm.b.c).toBe(3) 47 // setting unexisting 48 vm.$set('c.d', 2) 49 expect(vm.c.d).toBe(2) 50 // warn against setting unexisting 51 expect('Consider pre-initializing').toHaveBeenWarned() 52 }) 53 54 it('$set invalid', function () { 55 vm.$set('c + d', 1) 56 expect('Invalid setter expression').toHaveBeenWarned() 57 }) 58 59 it('$delete', function () { 60 vm._digest = jasmine.createSpy() 61 vm.$delete('a') 62 expect(_.hasOwn(vm, 'a')).toBe(false) 63 expect(_.hasOwn(vm._data, 'a')).toBe(false) 64 expect(vm._digest).toHaveBeenCalled() 65 // reserved key should not be deleted 66 vm.$delete('_data') 67 expect(vm._data).toBeTruthy() 68 }) 69 70 it('$watch', function (done) { 71 var spy = jasmine.createSpy() 72 // test immediate invoke 73 var unwatch = vm.$watch('a + b.c', spy, { 74 immediate: true 75 }) 76 expect(spy).toHaveBeenCalledWith(3) 77 vm.a = 2 78 nextTick(function () { 79 expect(spy).toHaveBeenCalledWith(4, 3) 80 // unwatch 81 unwatch() 82 vm.a = 3 83 nextTick(function () { 84 expect(spy.calls.count()).toBe(2) 85 done() 86 }) 87 }) 88 }) 89 90 it('function $watch', function (done) { 91 var spy = jasmine.createSpy() 92 // test immediate invoke 93 var unwatch = vm.$watch(function () { 94 return this.a + this.b.c 95 }, spy, { immediate: true }) 96 expect(spy).toHaveBeenCalledWith(3) 97 vm.a = 2 98 nextTick(function () { 99 expect(spy).toHaveBeenCalledWith(4, 3) 100 // unwatch 101 unwatch() 102 vm.a = 3 103 nextTick(function () { 104 expect(spy.calls.count()).toBe(2) 105 done() 106 }) 107 }) 108 }) 109 110 it('deep $watch', function (done) { 111 var oldB = vm.b 112 var spy = jasmine.createSpy() 113 vm.$watch('b', spy, { 114 deep: true 115 }) 116 vm.b.c = 3 117 nextTick(function () { 118 expect(spy).toHaveBeenCalledWith(oldB, oldB) 119 vm.b = { c: 4 } 120 nextTick(function () { 121 expect(spy).toHaveBeenCalledWith(vm.b, oldB) 122 done() 123 }) 124 }) 125 }) 126 127 it('$watch with filters', function (done) { 128 var spy = jasmine.createSpy() 129 vm.$watch('a | double', spy) 130 vm.a = 2 131 nextTick(function () { 132 expect(spy).toHaveBeenCalledWith(4, 2) 133 done() 134 }) 135 }) 136 137 it('$eval', function () { 138 expect(vm.$eval('a')).toBe(1) 139 expect(vm.$eval('b.c')).toBe(2) 140 expect(vm.$eval('a + b.c | double')).toBe(6) 141 }) 142 143 it('$interpolate', function () { 144 expect(vm.$interpolate('abc')).toBe('abc') 145 expect(vm.$interpolate('{{a}}')).toBe('1') 146 expect(vm.$interpolate('{{a}} and {{a + b.c | double}}')).toBe('1 and 6') 147 }) 148 149 if (typeof console !== 'undefined') { 150 it('$log', function () { 151 var oldLog = console.log 152 var spy = jasmine.createSpy() 153 console.log = function (val) { 154 expect(val.a).toBe(1) 155 expect(val.b.c).toBe(2) 156 expect(val.d).toBe(2) 157 expect(val.prop).toBe('foo') 158 spy() 159 } 160 vm.$log() 161 expect(spy.calls.count()).toBe(1) 162 console.log = function (val) { 163 expect(val.c).toBe(2) 164 spy() 165 } 166 vm.$log('b') 167 expect(spy.calls.count()).toBe(2) 168 console.log = oldLog 169 }) 170 } 171 })