github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/test/unit/specs/batcher_spec.js (about)

     1  var config = require('src/config')
     2  var batcher = require('src/batcher')
     3  var nextTick = require('src/util').nextTick
     4  
     5  describe('Batcher', function () {
     6    var spy
     7    beforeEach(function () {
     8      spy = jasmine.createSpy('batcher')
     9    })
    10  
    11    it('pushWatcher', function (done) {
    12      batcher.pushWatcher({
    13        run: spy
    14      })
    15      nextTick(function () {
    16        expect(spy.calls.count()).toBe(1)
    17        done()
    18      })
    19    })
    20  
    21    it('dedup', function (done) {
    22      batcher.pushWatcher({
    23        id: 1,
    24        run: spy
    25      })
    26      batcher.pushWatcher({
    27        id: 1,
    28        run: spy
    29      })
    30      nextTick(function () {
    31        expect(spy.calls.count()).toBe(1)
    32        done()
    33      })
    34    })
    35  
    36    it('allow diplicate when flushing', function (done) {
    37      var job = {
    38        id: 1,
    39        run: spy
    40      }
    41      batcher.pushWatcher(job)
    42      batcher.pushWatcher({
    43        id: 2,
    44        run: function () {
    45          batcher.pushWatcher(job)
    46        }
    47      })
    48      nextTick(function () {
    49        expect(spy.calls.count()).toBe(2)
    50        done()
    51      })
    52    })
    53  
    54    it('calls user watchers after directive updates', function (done) {
    55      var vals = []
    56      function run () {
    57        vals.push(this.id)
    58      }
    59      batcher.pushWatcher({
    60        id: 2,
    61        user: true,
    62        run: function () {
    63          run.call(this)
    64          // user watcher triggering another directive update!
    65          batcher.pushWatcher({
    66            id: 3,
    67            run: run
    68          })
    69        }
    70      })
    71      batcher.pushWatcher({
    72        id: 1,
    73        run: run
    74      })
    75      nextTick(function () {
    76        expect(vals[0]).toBe(1)
    77        expect(vals[1]).toBe(2)
    78        expect(vals[2]).toBe(3)
    79        done()
    80      })
    81    })
    82  
    83    it('warn against infinite update loops', function (done) {
    84      var count = 0
    85      var job = {
    86        id: 1,
    87        run: function () {
    88          count++
    89          batcher.pushWatcher(job)
    90        }
    91      }
    92      batcher.pushWatcher(job)
    93      nextTick(function () {
    94        expect(count).toBe(config._maxUpdateCount + 1)
    95        expect('infinite update loop').toHaveBeenWarned()
    96        done()
    97      })
    98    })
    99  
   100    it('should call newly pushed watcher after current watcher is done', function (done) {
   101      var callOrder = []
   102      batcher.pushWatcher({
   103        id: 1,
   104        user: true,
   105        run: function () {
   106          callOrder.push(1)
   107          batcher.pushWatcher({
   108            id: 2,
   109            run: function () {
   110              callOrder.push(3)
   111            }
   112          })
   113          callOrder.push(2)
   114        }
   115      })
   116      nextTick(function () {
   117        expect(callOrder.join()).toBe('1,2,3')
   118        done()
   119      })
   120    })
   121  })