batcher.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. describe('Batcher', function () {
  2. var batcher = require('vue/src/batcher'),
  3. nextTick = require('vue/src/utils').nextTick
  4. var updateCount = 0
  5. function mockBinding (id, middleware) {
  6. return {
  7. id: id,
  8. _update: function () {
  9. updateCount++
  10. this.updated = true
  11. if (middleware) middleware()
  12. }
  13. }
  14. }
  15. it('should queue bindings to be updated on nextTick', function (done) {
  16. updateCount = 0
  17. var b1 = mockBinding(1),
  18. b2 = mockBinding(2)
  19. batcher.queue(b1)
  20. batcher.queue(b2)
  21. assert.strictEqual(updateCount, 0)
  22. assert.notOk(b1.updated)
  23. assert.notOk(b2.updated)
  24. nextTick(function () {
  25. assert.strictEqual(updateCount, 2)
  26. assert.ok(b1.updated)
  27. assert.ok(b2.updated)
  28. done()
  29. })
  30. })
  31. it('should not queue dupicate bindings', function (done) {
  32. updateCount = 0
  33. var b1 = mockBinding(1),
  34. b2 = mockBinding(1)
  35. batcher.queue(b1)
  36. batcher.queue(b2)
  37. nextTick(function () {
  38. assert.strictEqual(updateCount, 1)
  39. assert.ok(b1.updated)
  40. assert.notOk(b2.updated)
  41. done()
  42. })
  43. })
  44. it('should queue dependency bidnings triggered during flush', function (done) {
  45. updateCount = 0
  46. var b1 = mockBinding(1),
  47. b2 = mockBinding(2, function () {
  48. batcher.queue(b1)
  49. })
  50. batcher.queue(b2)
  51. nextTick(function () {
  52. assert.strictEqual(updateCount, 2)
  53. assert.ok(b1.updated)
  54. assert.ok(b2.updated)
  55. done()
  56. })
  57. })
  58. })