github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/vue-1.0.24/test/unit/specs/instance/state_spec.js (about)

     1  var Vue = require('src')
     2  var _ = require('src/util')
     3  
     4  describe('Instance state initialization', function () {
     5    it('should warn data functions that do not return an object', function () {
     6      new Vue({
     7        data: function () {}
     8      })
     9      expect('should return an object').toHaveBeenWarned()
    10    })
    11  
    12    it('should initialize data once per strat', function () {
    13      var spyOncePerStrat = jasmine.createSpy('called once per strat')
    14      var Comp = Vue.extend({
    15        data: function () {
    16          spyOncePerStrat()
    17          return {
    18            result: 'false'
    19          }
    20        }
    21      })
    22      new Comp({
    23        data: function () {
    24          spyOncePerStrat()
    25          return {
    26            result: 'true'
    27          }
    28        }
    29      })
    30      expect(spyOncePerStrat.calls.count()).toBe(2)
    31    })
    32  
    33    describe('data proxy', function () {
    34      var data = {
    35        a: 0,
    36        b: 0
    37      }
    38      var vm = new Vue({
    39        data: data
    40      })
    41  
    42      it('initial', function () {
    43        expect(vm.a).toBe(data.a)
    44        expect(vm.b).toBe(data.b)
    45      })
    46  
    47      it('vm => data', function () {
    48        vm.a = 1
    49        expect(data.a).toBe(1)
    50        expect(vm.a).toBe(data.a)
    51      })
    52  
    53      it('data => vm', function () {
    54        data.b = 2
    55        expect(vm.b).toBe(2)
    56        expect(vm.b).toBe(data.b)
    57      })
    58    })
    59  
    60    describe('$data', function () {
    61      it('should initialize props', function () {
    62        var vm = new Vue({
    63          el: document.createElement('div'),
    64          props: ['c']
    65        })
    66        expect(_.hasOwn(vm, 'c')).toBe(true)
    67      })
    68  
    69      it('external prop should overwrite default value', function () {
    70        var el = document.createElement('div')
    71        el.setAttribute('v-bind:c', '2')
    72        el.textContent = '{{c}}'
    73        var vm = new Vue({
    74          el: el,
    75          props: ['c'],
    76          data: {
    77            c: 1
    78          }
    79        })
    80        expect(vm.c).toBe(2)
    81        expect(el.textContent).toBe('2')
    82      })
    83  
    84      it('props should be available in data() and create()', function () {
    85        var el = document.createElement('div')
    86        el.setAttribute(':c', '2')
    87        var vm = new Vue({
    88          el: el,
    89          props: ['c'],
    90          data: function () {
    91            expect(this.c).toBe(2)
    92            return {
    93              d: this.c + 1
    94            }
    95          },
    96          created: function () {
    97            expect(this.c).toBe(2)
    98          }
    99        })
   100        expect(vm.d).toBe(3)
   101      })
   102  
   103      it('replace $data', function () {
   104        var vm = new Vue({
   105          data: {
   106            a: 1
   107          }
   108        })
   109        vm.$data = { b: 2 }
   110        // proxy new key
   111        expect(vm.b).toBe(2)
   112        // unproxy old key that's no longer present
   113        expect(_.hasOwn(vm, 'a')).toBe(false)
   114      })
   115    })
   116  
   117    describe('computed', function () {
   118      var spyE = jasmine.createSpy('computed e')
   119      var spyF = jasmine.createSpy('cached computed f')
   120      var spyCachedWatcher = jasmine.createSpy('cached computed watcher')
   121  
   122      var Test = Vue.extend({
   123        computed: {
   124          // uncached
   125          c: {
   126            cache: false,
   127            get: function () {
   128              return this.a + this.b
   129            }
   130          },
   131          // with setter
   132          d: {
   133            get: function () {
   134              return this.a + this.b
   135            },
   136            set: function (newVal) {
   137              var vals = newVal.split(' ')
   138              this.a = vals[0]
   139              this.b = vals[1]
   140            }
   141          },
   142          // chained computed
   143          e: function () {
   144            return this.c + 'e'
   145          },
   146          // cached
   147          f: {
   148            get: function () {
   149              spyF()
   150              return this.ff
   151            }
   152          },
   153          // chained cached
   154          g: function () {
   155            return this.f + 1
   156          },
   157          // another cached, for watcher test
   158          h: {
   159            get: function () {
   160              return this.hh
   161            }
   162          }
   163        }
   164      })
   165  
   166      var vm = new Test({
   167        data: {
   168          a: 'a',
   169          b: 'b',
   170          ff: 0,
   171          hh: 0
   172        },
   173        watch: {
   174          e: spyE,
   175          h: spyCachedWatcher
   176        }
   177      })
   178  
   179      it('get', function () {
   180        expect(vm.c).toBe('ab')
   181        expect(vm.d).toBe('ab')
   182        expect(vm.e).toBe('abe')
   183      })
   184  
   185      it('set', function (done) {
   186        vm.c = 123 // should do nothing
   187        vm.d = 'c d'
   188        expect(vm.a).toBe('c')
   189        expect(vm.b).toBe('d')
   190        expect(vm.c).toBe('cd')
   191        expect(vm.d).toBe('cd')
   192        expect(vm.e).toBe('cde')
   193        Vue.nextTick(function () {
   194          expect(spyE).toHaveBeenCalledWith('cde', 'abe')
   195          done()
   196        })
   197      })
   198  
   199      it('cached computed', function () {
   200        expect(spyF).not.toHaveBeenCalled()
   201        var f = vm.f
   202        var g = vm.g
   203        expect(spyF.calls.count()).toBe(1)
   204        expect(f).toBe(0)
   205        expect(g).toBe(1)
   206        // get again
   207        f = vm.f
   208        g = vm.g
   209        // should not be evaluated again
   210        expect(spyF.calls.count()).toBe(1)
   211        expect(f).toBe(0)
   212        expect(g).toBe(1)
   213        // update dep
   214        vm.ff = 1
   215        f = vm.f
   216        g = vm.g
   217        expect(spyF.calls.count()).toBe(2)
   218        expect(f).toBe(1)
   219        expect(g).toBe(2)
   220      })
   221  
   222      it('watching cached computed', function (done) {
   223        expect(spyCachedWatcher).not.toHaveBeenCalled()
   224        vm.hh = 2
   225        Vue.nextTick(function () {
   226          expect(spyCachedWatcher).toHaveBeenCalledWith(2, 0)
   227          done()
   228        })
   229      })
   230  
   231      it('same definition object bound to different instance', function () {
   232        var vm = new Test({
   233          data: {
   234            a: 'A',
   235            b: 'B'
   236          }
   237        })
   238        expect(vm.c).toBe('AB')
   239        expect(vm.d).toBe('AB')
   240        vm.d = 'C D'
   241        expect(vm.a).toBe('C')
   242        expect(vm.b).toBe('D')
   243        expect(vm.c).toBe('CD')
   244        expect(vm.d).toBe('CD')
   245        expect(vm.e).toBe('CDe')
   246      })
   247    })
   248  
   249    describe('methods', function () {
   250      it('should work and have correct context', function () {
   251        var vm = new Vue({
   252          data: {
   253            a: 1
   254          },
   255          methods: {
   256            test: function () {
   257              expect(this instanceof Vue).toBe(true)
   258              return this.a
   259            }
   260          }
   261        })
   262        expect(vm.test()).toBe(1)
   263      })
   264    })
   265  
   266    describe('meta', function () {
   267      var vm = new Vue({
   268        _meta: {
   269          $index: 0,
   270          $value: 'test'
   271        }
   272      })
   273  
   274      it('should define metas only on vm', function () {
   275        expect(vm.$index).toBe(0)
   276        expect(vm.$value).toBe('test')
   277        expect('$index' in vm.$data).toBe(false)
   278        expect('$value' in vm.$data).toBe(false)
   279      })
   280    })
   281  })