scheduler.spec.js 2.2 KB

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