RenderQueue.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. describe('RenderQueue', function() {
  2. var RenderQueue = $.fullCalendar.RenderQueue;
  3. it('executes atomic events in sequence', function() {
  4. var ops = [];
  5. var q = new RenderQueue();
  6. q.queue(function() {
  7. ops.push('fooinit');
  8. }, 'foo', 'init');
  9. q.queue(function() {
  10. ops.push('fooremove');
  11. }, 'foo', 'add');
  12. q.queue(function() {
  13. ops.push('fooadd');
  14. }, 'foo', 'remove');
  15. q.queue(function() {
  16. ops.push('foodestroy');
  17. }, 'foo', 'destroy');
  18. expect(ops).toEqual([ 'fooinit', 'fooremove', 'fooadd', 'foodestroy' ]);
  19. });
  20. describe('when accumulating', function() {
  21. describe('using clear action', function() {
  22. it('destroys add/remove operations in same namespace', function() {
  23. var ops = [];
  24. var q = new RenderQueue();
  25. q.pause();
  26. q.queue(function() {
  27. ops.push('fooadd');
  28. }, 'foo', 'add');
  29. q.queue(function() {
  30. ops.push('fooremove');
  31. }, 'foo', 'remove');
  32. q.queue(function() {
  33. ops.push('foodestroy');
  34. }, 'foo', 'destroy');
  35. expect(ops).toEqual([]);
  36. q.resume();
  37. expect(ops).toEqual([ 'foodestroy' ]);
  38. });
  39. });
  40. });
  41. describe('when namespace has a wait value', function() {
  42. it('unpauses when done', function(done) {
  43. var ops = [];
  44. var q = new RenderQueue({
  45. foo: 100
  46. });
  47. q.queue(function() {
  48. ops.push('fooinit');
  49. }, 'foo', 'init');
  50. q.queue(function() {
  51. ops.push('fooadd');
  52. }, 'foo', 'add');
  53. expect(ops).toEqual([]);
  54. setTimeout(function() {
  55. expect(ops).toEqual([ 'fooinit', 'fooadd' ]);
  56. done();
  57. }, 200);
  58. });
  59. it('restarts timer when new operation happens', function(done) {
  60. var ops = [];
  61. var q = new RenderQueue({
  62. foo: 100
  63. });
  64. q.queue(function() {
  65. ops.push('fooinit');
  66. }, 'foo', 'init');
  67. setTimeout(function() {
  68. q.queue(function() {
  69. ops.push('fooadd');
  70. }, 'foo', 'add');
  71. }, 50);
  72. setTimeout(function() {
  73. expect(ops).toEqual([]);
  74. }, 125);
  75. setTimeout(function() {
  76. expect(ops).toEqual([ 'fooinit', 'fooadd' ]);
  77. done();
  78. }, 175);
  79. });
  80. it('synchronously executes queue when sync non-namespace operation happens', function() {
  81. var ops = [];
  82. var q = new RenderQueue({
  83. foo: 100
  84. });
  85. q.queue(function() {
  86. ops.push('fooinit');
  87. }, 'foo', 'init');
  88. q.queue(function() {
  89. ops.push('fooadd');
  90. }, 'foo', 'add');
  91. expect(ops).toEqual([]);
  92. q.queue(function() {
  93. ops.push('barinit');
  94. }, 'bar', 'init');
  95. expect(ops).toEqual([ 'fooinit', 'fooadd', 'barinit' ]);
  96. });
  97. it('synchronously executes queue when async non-namespace operation happens', function(done) {
  98. var ops = [];
  99. var q = new RenderQueue({
  100. foo: 100,
  101. bar: 100
  102. });
  103. q.queue(function() {
  104. ops.push('fooinit');
  105. }, 'foo', 'init');
  106. q.queue(function() {
  107. ops.push('fooadd');
  108. }, 'foo', 'add');
  109. expect(ops).toEqual([]);
  110. q.queue(function() {
  111. ops.push('barinit');
  112. }, 'bar', 'init');
  113. expect(ops).toEqual([ 'fooinit', 'fooadd' ]);
  114. setTimeout(function() {
  115. expect(ops).toEqual([ 'fooinit', 'fooadd', 'barinit' ]);
  116. done();
  117. }, 200);
  118. });
  119. it('resumes non-waiting tasks when unpaused', function(done) {
  120. var ops = [];
  121. var q = new RenderQueue({
  122. foo: 100
  123. });
  124. q.pause();
  125. q.queue(function() {
  126. ops.push('barinit');
  127. }, 'bar', 'init');
  128. q.queue(function() {
  129. ops.push('fooinit');
  130. }, 'foo', 'init');
  131. q.resume();
  132. expect(ops).toEqual([ 'barinit' ]);
  133. setTimeout(function() {
  134. expect(ops).toEqual([ 'barinit', 'fooinit' ]);
  135. done();
  136. }, 200);
  137. });
  138. it('paused+queued tasks from a previous namespace wait resume immediately', function(done) {
  139. var ops = [];
  140. var q = new RenderQueue({
  141. foo: 100
  142. });
  143. q.pause();
  144. q.queue(function() {
  145. ops.push('foodestroy');
  146. }, 'foo', 'destroy');
  147. q.queue(function() {
  148. ops.push('bardestroy');
  149. }, 'bar', 'destroy');
  150. expect(ops).toEqual([]);
  151. q.queue(function() {
  152. ops.push('barinit');
  153. }, 'bar', 'init');
  154. q.queue(function() {
  155. ops.push('fooinit');
  156. }, 'foo', 'init');
  157. expect(ops).toEqual([]);
  158. q.resume();
  159. expect(ops).toEqual([ 'foodestroy', 'bardestroy', 'barinit' ]);
  160. setTimeout(function() {
  161. expect(ops).toEqual([ 'foodestroy', 'bardestroy', 'barinit', 'fooinit' ]);
  162. done();
  163. }, 200);
  164. });
  165. });
  166. });