scheduler.spec.js 2.3 KB

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