batcher_spec.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. spyWarns()
  9. })
  10. it('pushWatcher', function (done) {
  11. batcher.pushWatcher({
  12. run: spy
  13. })
  14. nextTick(function () {
  15. expect(spy.calls.count()).toBe(1)
  16. done()
  17. })
  18. })
  19. it('dedup', function (done) {
  20. batcher.pushWatcher({
  21. id: 1,
  22. run: spy
  23. })
  24. batcher.pushWatcher({
  25. id: 1,
  26. run: spy
  27. })
  28. nextTick(function () {
  29. expect(spy.calls.count()).toBe(1)
  30. done()
  31. })
  32. })
  33. it('allow diplicate when flushing', function (done) {
  34. var job = {
  35. id: 1,
  36. run: spy
  37. }
  38. batcher.pushWatcher(job)
  39. batcher.pushWatcher({
  40. id: 2,
  41. run: function () {
  42. batcher.pushWatcher(job)
  43. }
  44. })
  45. nextTick(function () {
  46. expect(spy.calls.count()).toBe(2)
  47. done()
  48. })
  49. })
  50. it('calls user watchers after directive updates', function (done) {
  51. var vals = []
  52. function run () {
  53. vals.push(this.id)
  54. }
  55. batcher.pushWatcher({
  56. id: 2,
  57. user: true,
  58. run: function () {
  59. run.call(this)
  60. // user watcher triggering another directive update!
  61. batcher.pushWatcher({
  62. id: 3,
  63. run: run
  64. })
  65. }
  66. })
  67. batcher.pushWatcher({
  68. id: 1,
  69. run: run
  70. })
  71. nextTick(function () {
  72. expect(vals[0]).toBe(1)
  73. expect(vals[1]).toBe(2)
  74. expect(vals[2]).toBe(3)
  75. done()
  76. })
  77. })
  78. it('warn against infinite update loops', function (done) {
  79. var count = 0
  80. var job = {
  81. id: 1,
  82. run: function () {
  83. count++
  84. batcher.pushWatcher(job)
  85. }
  86. }
  87. batcher.pushWatcher(job)
  88. nextTick(function () {
  89. expect(count).toBe(config._maxUpdateCount + 1)
  90. expect(hasWarned('infinite update loop')).toBe(true)
  91. done()
  92. })
  93. })
  94. })