removeEventSource.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. describe('removeEventSource', function() {
  2. var options;
  3. beforeEach(function() {
  4. affix('#cal');
  5. options = {
  6. defaultDate: '2014-08-01'
  7. };
  8. $.mockjax({
  9. url: '*',
  10. contentType: 'text/json',
  11. responseText: buildEventArray()
  12. });
  13. $.mockjaxSettings.log = function() { }; // don't console.log
  14. });
  15. afterEach(function() {
  16. $.mockjax.clear();
  17. });
  18. describe('with a URL', function() {
  19. testInput('/myscript.php'); // will go to mockjax
  20. });
  21. describe('with an array', function() {
  22. testInput(buildEventArray());
  23. });
  24. describe('with a function', function() {
  25. testInput(function(start, end, timezone, callback) {
  26. callback(buildEventArray());
  27. });
  28. });
  29. describe('with an object+url', function() {
  30. testInput({
  31. url: '/myscript.php' // will go to mockjax
  32. });
  33. });
  34. describe('with an object+array', function() {
  35. testInput({
  36. events: buildEventArray()
  37. });
  38. });
  39. describe('with an object+function', function() {
  40. testInput({
  41. events: function(start, end, timezone, callback) {
  42. callback(buildEventArray());
  43. }
  44. });
  45. });
  46. it('won\'t render removed events when subsequent addEventSource', function(done) {
  47. var source1 = function(start, end, timezone, callback) {
  48. setTimeout(function() {
  49. callback([ {
  50. title: 'event1',
  51. className: 'event1',
  52. start: '2014-08-01T02:00:00'
  53. } ]);
  54. }, 100);
  55. };
  56. var source2 = function(start, end, timezone, callback) {
  57. setTimeout(function() {
  58. callback([ {
  59. title: 'event2',
  60. className: 'event2',
  61. start: '2014-08-01T02:00:00'
  62. } ]);
  63. }, 100);
  64. };
  65. options.eventSources = [ source1 ];
  66. options.eventAfterAllRender = function() {
  67. if (!$('.fc-event').length) {
  68. ; // might have rendered no events after removeEventSource call
  69. }
  70. else {
  71. expect($('.event1').length).toBe(0);
  72. expect($('.event2').length).toBe(1);
  73. done();
  74. }
  75. };
  76. $('#cal').fullCalendar(options);
  77. $('#cal').fullCalendar('removeEventSource', source1);
  78. $('#cal').fullCalendar('addEventSource', source2);
  79. });
  80. describe('when multiple sources share the same fetching function', function() {
  81. var fetchFunc = function(start, end, timezone, callback) {
  82. callback([ {
  83. title: 'event',
  84. start: '2014-08-01T02:00:00'
  85. } ]);
  86. };
  87. beforeEach(function() {
  88. options.eventSources = [
  89. { events: fetchFunc, className: 'event1', id: 'source1' },
  90. { events: fetchFunc, className: 'event2', id: 'source2' }
  91. ];
  92. });
  93. describe('when called with the raw function', function() {
  94. it('removes events from all matching sources', function() {
  95. $('#cal').fullCalendar(options);
  96. expect($('.fc-event').length).toBe(2);
  97. expect($('.event1').length).toBe(1);
  98. expect($('.event2').length).toBe(1);
  99. $('#cal').fullCalendar('removeEventSource', fetchFunc);
  100. expect($('.fc-event').length).toBe(0);
  101. });
  102. });
  103. describe('when called with proper source object', function() {
  104. it('removes events only from the specific source', function() {
  105. $('#cal').fullCalendar(options);
  106. expect($('.fc-event').length).toBe(2);
  107. expect($('.event1').length).toBe(1);
  108. expect($('.event2').length).toBe(1);
  109. var source = $('#cal').fullCalendar('getEventSourceById', 'source2');
  110. $('#cal').fullCalendar('removeEventSource', source);
  111. expect($('.fc-event').length).toBe(1);
  112. expect($('.event1').length).toBe(1);
  113. expect($('.event2').length).toBe(0);
  114. });
  115. });
  116. });
  117. function testInput(eventInput) {
  118. it('correctly removes events provided via `events` at initialization', function(done) {
  119. var callCnt = 0;
  120. options.eventAfterAllRender = function() {
  121. callCnt++;
  122. if (callCnt === 1) {
  123. expectEventCnt(2);
  124. $('#cal').fullCalendar('removeEventSource', eventInput);
  125. }
  126. else if (callCnt === 2) {
  127. expectEventCnt(0);
  128. done();
  129. }
  130. };
  131. options.events = eventInput;
  132. $('#cal').fullCalendar(options);
  133. });
  134. it('correctly removes events provided via `eventSources` at initialization', function(done) {
  135. var callCnt = 0;
  136. options.eventAfterAllRender = function() {
  137. callCnt++;
  138. if (callCnt === 1) {
  139. expectEventCnt(2);
  140. $('#cal').fullCalendar('removeEventSource', eventInput);
  141. }
  142. else if (callCnt === 2) {
  143. expectEventCnt(0);
  144. done();
  145. }
  146. };
  147. options.eventSources = [ eventInput ];
  148. $('#cal').fullCalendar(options);
  149. });
  150. it('correctly removes events provided via `addEventSource` method', function(done) {
  151. var callCnt = 0;
  152. options.eventAfterAllRender = function() {
  153. callCnt++;
  154. if (callCnt === 1) {
  155. $('#cal').fullCalendar('addEventSource', eventInput);
  156. }
  157. else if (callCnt === 2) {
  158. expectEventCnt(2);
  159. $('#cal').fullCalendar('removeEventSource', eventInput);
  160. }
  161. else if (callCnt === 3) {
  162. expectEventCnt(0);
  163. done();
  164. }
  165. };
  166. $('#cal').fullCalendar(options);
  167. });
  168. }
  169. function buildEventArray() {
  170. return [
  171. { title: 'event1', start: '2014-08-01' },
  172. { title: 'event2', start: '2014-08-02' }
  173. ];
  174. }
  175. function expectEventCnt(cnt) {
  176. expect($('.fc-event').length).toBe(cnt);
  177. expect($('#cal').fullCalendar('clientEvents').length).toBe(cnt);
  178. }
  179. });