batcher_spec.js 2.0 KB

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