batcher_spec.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var _ = require('../../../src/util')
  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. spyOn(_, 'warn')
  9. })
  10. it('push', function (done) {
  11. batcher.push({
  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.push({
  21. id: 1,
  22. run: spy
  23. })
  24. batcher.push({
  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. batcher.push({
  35. id: 1,
  36. run: function () {
  37. spy()
  38. batcher.push({
  39. id: 1,
  40. run: spy
  41. })
  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.push({
  55. id: 2,
  56. user: true,
  57. run: function () {
  58. run.call(this)
  59. // user watcher triggering another directive update!
  60. batcher.push({
  61. id: 3,
  62. run: run
  63. })
  64. }
  65. })
  66. batcher.push({
  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.push(job)
  84. }
  85. }
  86. batcher.push(job)
  87. nextTick(function () {
  88. expect(count).not.toBe(0)
  89. expect(_.warn).toHaveBeenCalled()
  90. done()
  91. })
  92. })
  93. })