refetchEventSources.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. describe('refetchEventSources', function() {
  2. var calendarEl;
  3. var options;
  4. // used by createEventGenerator
  5. var eventCount;
  6. var fetchId;
  7. var fetchDelay;
  8. beforeEach(function() {
  9. affix('#cal');
  10. calendarEl = $('#cal');
  11. eventCount = 1;
  12. fetchId = 7;
  13. options = {
  14. now: '2015-08-07',
  15. defaultView: 'agendaDay',
  16. scrollTime: '00:00',
  17. eventSources: [
  18. {
  19. id: 1,
  20. events: createEventGenerator('source1-'),
  21. color: 'green'
  22. },
  23. {
  24. id: 2,
  25. events: createEventGenerator('source2-'),
  26. color: 'blue'
  27. },
  28. {
  29. id: 3,
  30. events: createEventGenerator('source3-'),
  31. color: 'green'
  32. }
  33. ]
  34. };
  35. });
  36. describe('with a single event source passed in', function() {
  37. it('only refetches events for the specified event source', function(done) {
  38. calendarEl.fullCalendar(options);
  39. expect($('.source1-7').length).toEqual(1);
  40. expect($('.source2-7').length).toEqual(1);
  41. expect($('.source3-7').length).toEqual(1);
  42. var allEventSources = calendarEl.fullCalendar('getEventSources');
  43. var blueEventSource = $.grep(allEventSources, function(eventSource) {
  44. return eventSource.color === 'blue';
  45. })[0];
  46. // increase the number of events for the refetched source
  47. eventCount = 2;
  48. fetchId = 8;
  49. calendarEl.fullCalendar('refetchEventSources', blueEventSource);
  50. // events from unaffected sources remain
  51. expect($('.source1-7').length).toEqual(1);
  52. expect($('.source3-7').length).toEqual(1);
  53. // events from old fetch were cleared
  54. expect($('.source2-7').length).toEqual(0);
  55. // events from new fetch were rendered
  56. expect($('.source2-8').length).toEqual(2);
  57. done();
  58. });
  59. });
  60. describe('with a single event source ID passed in', function() {
  61. it('only refetches events for the specified event source', function(done) {
  62. calendarEl.fullCalendar(options);
  63. expect($('.source1-7').length).toEqual(1);
  64. expect($('.source2-7').length).toEqual(1);
  65. expect($('.source3-7').length).toEqual(1);
  66. // increase the number of events for the refetched source
  67. eventCount = 2;
  68. fetchId = 8;
  69. calendarEl.fullCalendar('refetchEventSources', 2);
  70. // events from unaffected sources remain
  71. expect($('.source1-7').length).toEqual(1);
  72. expect($('.source3-7').length).toEqual(1);
  73. // events from old fetch were cleared
  74. expect($('.source2-7').length).toEqual(0);
  75. // events from new fetch were rendered
  76. expect($('.source2-8').length).toEqual(2);
  77. done();
  78. });
  79. });
  80. describe('with an array of multiple event sources passed in', function() {
  81. it('only refetches events for the specified event sources', function(done) {
  82. calendarEl.fullCalendar(options);
  83. expect($('.source1-7').length).toEqual(1);
  84. expect($('.source2-7').length).toEqual(1);
  85. expect($('.source3-7').length).toEqual(1);
  86. var allEventSources = calendarEl.fullCalendar('getEventSources');
  87. var greenEventSources = $.grep(allEventSources, function(eventSource) {
  88. return eventSource.color === 'green';
  89. });
  90. // increase the number of events for the refetched sources
  91. eventCount = 2;
  92. fetchId = 8;
  93. calendarEl.fullCalendar('refetchEventSources', greenEventSources);
  94. // events from unaffected sources remain
  95. expect($('.source2-7').length).toEqual(1);
  96. // events from old fetch were cleared
  97. expect($('.source1-7').length).toEqual(0);
  98. expect($('.source3-7').length).toEqual(0);
  99. // events from new fetch were rendered
  100. expect($('.source1-8').length).toEqual(2);
  101. expect($('.source3-8').length).toEqual(2);
  102. done();
  103. });
  104. });
  105. describe('with an array of multiple event source IDs passed in', function() {
  106. it('only refetches events for the specified event sources', function(done) {
  107. calendarEl.fullCalendar(options);
  108. expect($('.source1-7').length).toEqual(1);
  109. expect($('.source2-7').length).toEqual(1);
  110. expect($('.source3-7').length).toEqual(1);
  111. // increase the number of events for the refetched sources
  112. eventCount = 2;
  113. fetchId = 8;
  114. calendarEl.fullCalendar('refetchEventSources', [ 1, 3 ]);
  115. // events from unaffected sources remain
  116. expect($('.source2-7').length).toEqual(1);
  117. // events from old fetch were cleared
  118. expect($('.source1-7').length).toEqual(0);
  119. expect($('.source3-7').length).toEqual(0);
  120. // events from new fetch were rendered
  121. expect($('.source1-8').length).toEqual(2);
  122. expect($('.source3-8').length).toEqual(2);
  123. done();
  124. });
  125. });
  126. describe('when called while initial fetch is still pending', function() {
  127. it('keeps old events and rerenders new', function(done) {
  128. options.eventAfterAllRender = function() {
  129. // events from unaffected sources remain
  130. expect($('.source2-7').length).toEqual(1);
  131. // events from old fetch were cleared
  132. expect($('.source1-7').length).toEqual(0);
  133. expect($('.source3-7').length).toEqual(0);
  134. // events from new fetch were rendered
  135. expect($('.source1-8').length).toEqual(2);
  136. expect($('.source3-8').length).toEqual(2);
  137. done();
  138. };
  139. fetchDelay = 100;
  140. calendarEl.fullCalendar(options);
  141. var allEventSources = calendarEl.fullCalendar('getEventSources');
  142. var greenEventSources = $.grep(allEventSources, function(eventSource) { // source 1 and 3
  143. return eventSource.color === 'green';
  144. });
  145. // increase the number of events for the refetched sources
  146. eventCount = 2;
  147. fetchId = 8;
  148. calendarEl.fullCalendar('refetchEventSources', greenEventSources);
  149. });
  150. });
  151. function createEventGenerator(classNamePrefix) {
  152. return function(start, end, timezone, callback) {
  153. var events = [];
  154. for (var i = 0; i < eventCount; i++) {
  155. events.push({
  156. start: '2015-08-07T02:00:00',
  157. end: '2015-08-07T03:00:00',
  158. className: classNamePrefix + fetchId,
  159. title: classNamePrefix + fetchId // also make it the title
  160. });
  161. }
  162. if (fetchDelay) {
  163. setTimeout(function() {
  164. callback(events);
  165. }, fetchDelay);
  166. }
  167. else {
  168. callback(events);
  169. }
  170. };
  171. }
  172. });