batcher_spec.js 2.4 KB

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