batcher.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. describe('Batcher', function () {
  2. var Batcher = require('vue/src/batcher'),
  3. batcher = new Batcher(),
  4. nextTick = require('vue/src/utils').nextTick
  5. var updateCount = 0
  6. function mockJob (id, middleware) {
  7. return {
  8. id: id,
  9. execute: function () {
  10. updateCount++
  11. this.updated = true
  12. if (middleware) middleware()
  13. }
  14. }
  15. }
  16. it('should push bindings to be updated on nextTick', function (done) {
  17. updateCount = 0
  18. var b1 = mockJob(1),
  19. b2 = mockJob(2)
  20. batcher.push(b1)
  21. batcher.push(b2)
  22. assert.strictEqual(updateCount, 0)
  23. assert.notOk(b1.updated)
  24. assert.notOk(b2.updated)
  25. nextTick(function () {
  26. assert.strictEqual(updateCount, 2)
  27. assert.ok(b1.updated)
  28. assert.ok(b2.updated)
  29. done()
  30. })
  31. })
  32. it('should not push dupicate bindings', function (done) {
  33. updateCount = 0
  34. var b1 = mockJob(1),
  35. b2 = mockJob(1)
  36. batcher.push(b1)
  37. batcher.push(b2)
  38. nextTick(function () {
  39. assert.strictEqual(updateCount, 1)
  40. assert.ok(b1.updated)
  41. assert.notOk(b2.updated)
  42. done()
  43. })
  44. })
  45. it('should push dependency bidnings triggered during flush', function (done) {
  46. updateCount = 0
  47. var b1 = mockJob(1),
  48. b2 = mockJob(2, function () {
  49. batcher.push(b1)
  50. })
  51. batcher.push(b2)
  52. nextTick(function () {
  53. assert.strictEqual(updateCount, 2)
  54. assert.ok(b1.updated)
  55. assert.ok(b2.updated)
  56. done()
  57. })
  58. })
  59. it('should allow overriding jobs with same ID', function (done) {
  60. updateCount = 0
  61. var b1 = mockJob(1),
  62. b2 = mockJob(1)
  63. b2.override = true
  64. batcher.push(b1)
  65. batcher.push(b2)
  66. nextTick(function () {
  67. assert.strictEqual(updateCount, 1)
  68. assert.ok(b1.cancelled)
  69. assert.notOk(b1.updated)
  70. assert.ok(b2.updated)
  71. done()
  72. })
  73. })
  74. it('should execute the _preFlush hook', function (done) {
  75. var executed = false
  76. batcher._preFlush = function () {
  77. executed = true
  78. }
  79. batcher.push(mockJob(1))
  80. nextTick(function () {
  81. assert.ok(executed)
  82. done()
  83. })
  84. })
  85. })