RenderQueue.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. it('is cancelled out by an init in same namespace', function() {
  40. var ops = [];
  41. var q = new RenderQueue();
  42. q.pause();
  43. q.queue(function() {
  44. ops.push('barinit');
  45. }, 'bar', 'init');
  46. q.queue(function() {
  47. ops.push('fooinit');
  48. }, 'foo', 'init');
  49. q.queue(function() {
  50. ops.push('fooadd');
  51. }, 'foo', 'add');
  52. q.queue(function() {
  53. ops.push('fooadd');
  54. }, 'foo', 'remove');
  55. q.queue(function() {
  56. ops.push('fooadd');
  57. }, 'foo', 'destroy');
  58. expect(ops).toEqual([]);
  59. q.resume();
  60. expect(ops).toEqual([ 'barinit' ]);
  61. });
  62. });
  63. });
  64. describe('when namespace has a wait value', function() {
  65. it('unpauses when done', function(done) {
  66. var ops = [];
  67. var q = new RenderQueue({
  68. foo: 100
  69. });
  70. q.queue(function() {
  71. ops.push('fooinit');
  72. }, 'foo', 'init');
  73. q.queue(function() {
  74. ops.push('fooadd');
  75. }, 'foo', 'add');
  76. expect(ops).toEqual([]);
  77. setTimeout(function() {
  78. expect(ops).toEqual([ 'fooinit', 'fooadd' ]);
  79. done();
  80. }, 200);
  81. });
  82. it('restarts timer when new operation happens', function(done) {
  83. var ops = [];
  84. var q = new RenderQueue({
  85. foo: 100
  86. });
  87. q.queue(function() {
  88. ops.push('fooinit');
  89. }, 'foo', 'init');
  90. setTimeout(function() {
  91. q.queue(function() {
  92. ops.push('fooadd');
  93. }, 'foo', 'add');
  94. }, 50);
  95. setTimeout(function() {
  96. expect(ops).toEqual([]);
  97. }, 125);
  98. setTimeout(function() {
  99. expect(ops).toEqual([ 'fooinit', 'fooadd' ]);
  100. done();
  101. }, 175);
  102. });
  103. it('synchronously executes queue when sync non-namespace operation happens', function() {
  104. var ops = [];
  105. var q = new RenderQueue({
  106. foo: 100
  107. });
  108. q.queue(function() {
  109. ops.push('fooinit');
  110. }, 'foo', 'init');
  111. q.queue(function() {
  112. ops.push('fooadd');
  113. }, 'foo', 'add');
  114. expect(ops).toEqual([]);
  115. q.queue(function() {
  116. ops.push('barinit');
  117. }, 'bar', 'init');
  118. expect(ops).toEqual([ 'fooinit', 'fooadd', 'barinit' ]);
  119. });
  120. it('synchronously executes queue when async non-namespace operation happens', function(done) {
  121. var ops = [];
  122. var q = new RenderQueue({
  123. foo: 100,
  124. bar: 100
  125. });
  126. q.queue(function() {
  127. ops.push('fooinit');
  128. }, 'foo', 'init');
  129. q.queue(function() {
  130. ops.push('fooadd');
  131. }, 'foo', 'add');
  132. expect(ops).toEqual([]);
  133. q.queue(function() {
  134. ops.push('barinit');
  135. }, 'bar', 'init');
  136. expect(ops).toEqual([ 'fooinit', 'fooadd' ]);
  137. setTimeout(function() {
  138. expect(ops).toEqual([ 'fooinit', 'fooadd', 'barinit' ]);
  139. done();
  140. }, 200);
  141. });
  142. it('resumes non-waiting tasks when unpaused', function(done) {
  143. var ops = [];
  144. var q = new RenderQueue({
  145. foo: 100
  146. });
  147. q.pause();
  148. q.queue(function() {
  149. ops.push('barinit');
  150. }, 'bar', 'init');
  151. q.queue(function() {
  152. ops.push('fooinit');
  153. }, 'foo', 'init');
  154. q.resume();
  155. expect(ops).toEqual([ 'barinit' ]);
  156. setTimeout(function() {
  157. expect(ops).toEqual([ 'barinit', 'fooinit' ]);
  158. done();
  159. }, 200);
  160. });
  161. it('paused+queued tasks from a previous namespace wait resume immediately', function(done) {
  162. var ops = [];
  163. var q = new RenderQueue({
  164. foo: 100
  165. });
  166. q.pause();
  167. q.queue(function() {
  168. ops.push('foodestroy');
  169. }, 'foo', 'destroy');
  170. q.queue(function() {
  171. ops.push('bardestroy');
  172. }, 'bar', 'destroy');
  173. expect(ops).toEqual([]);
  174. q.queue(function() {
  175. ops.push('barinit');
  176. }, 'bar', 'init');
  177. q.queue(function() {
  178. ops.push('fooinit');
  179. }, 'foo', 'init');
  180. expect(ops).toEqual([]);
  181. q.resume();
  182. expect(ops).toEqual([ 'foodestroy', 'bardestroy', 'barinit' ]);
  183. setTimeout(function() {
  184. expect(ops).toEqual([ 'foodestroy', 'bardestroy', 'barinit', 'fooinit' ]);
  185. done();
  186. }, 200);
  187. });
  188. });
  189. });