removeEvents.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. describe('removeEvents', function() {
  2. var options;
  3. beforeEach(function() {
  4. affix('#cal');
  5. options = {
  6. defaultDate: '2014-06-24',
  7. defaultView: 'month'
  8. };
  9. });
  10. function buildEventsWithoutIds() {
  11. return [
  12. { title: 'event zero', start: '2014-06-24', className: 'event-zero' },
  13. { title: 'event one', start: '2014-06-24', className: 'event-non-zero event-one' },
  14. { title: 'event two', start: '2014-06-24', className: 'event-non-zero event-two' }
  15. ];
  16. }
  17. function buildEventsWithIds() {
  18. var events = buildEventsWithoutIds();
  19. var i;
  20. for (i = 0; i < events.length; i++) {
  21. events[i].id = i;
  22. }
  23. return events;
  24. }
  25. $.each({
  26. 'when events without IDs': buildEventsWithoutIds,
  27. 'when events with IDs': buildEventsWithIds
  28. }, function(desc, eventGenerator) {
  29. describe(desc, function() {
  30. it('can remove all events if no args specified', function(done) {
  31. go(
  32. eventGenerator(),
  33. function() {
  34. $('#cal').fullCalendar('removeEvents');
  35. },
  36. function() {
  37. expect($('#cal').fullCalendar('clientEvents').length).toEqual(0);
  38. expect($('.fc-event').length).toEqual(0);
  39. },
  40. done
  41. );
  42. });
  43. it('can remove events with a filter function', function(done) {
  44. go(
  45. eventGenerator(),
  46. function() {
  47. $('#cal').fullCalendar('removeEvents', function(event) {
  48. return $.inArray('event-one', event.className) !== -1;
  49. });
  50. },
  51. function() {
  52. expect($('#cal').fullCalendar('clientEvents').length).toEqual(2);
  53. expect($('.fc-event').length).toEqual(2);
  54. expect($('.event-zero').length).toEqual(1);
  55. expect($('.event-two').length).toEqual(1);
  56. },
  57. done
  58. );
  59. });
  60. });
  61. });
  62. it('can remove events with a numeric ID', function(done) {
  63. go(
  64. buildEventsWithIds(),
  65. function() {
  66. $('#cal').fullCalendar('removeEvents', 1);
  67. },
  68. function() {
  69. expect($('#cal').fullCalendar('clientEvents').length).toEqual(2);
  70. expect($('.fc-event').length).toEqual(2);
  71. expect($('.event-zero').length).toEqual(1);
  72. expect($('.event-two').length).toEqual(1);
  73. },
  74. done
  75. );
  76. });
  77. it('can remove events with a string ID', function(done) {
  78. go(
  79. buildEventsWithIds(),
  80. function() {
  81. $('#cal').fullCalendar('removeEvents', '1');
  82. },
  83. function() {
  84. expect($('#cal').fullCalendar('clientEvents').length).toEqual(2);
  85. expect($('.fc-event').length).toEqual(2);
  86. expect($('.event-zero').length).toEqual(1);
  87. expect($('.event-two').length).toEqual(1);
  88. },
  89. done
  90. );
  91. });
  92. it('can remove an event with ID 0', function(done) { // for issue 2082
  93. go(
  94. buildEventsWithIds(),
  95. function() {
  96. $('#cal').fullCalendar('removeEvents', 0);
  97. },
  98. function() {
  99. expect($('#cal').fullCalendar('clientEvents').length).toEqual(2);
  100. expect($('.fc-event').length).toEqual(2);
  101. expect($('.event-zero').length).toEqual(0);
  102. expect($('.event-non-zero').length).toEqual(2);
  103. },
  104. done
  105. );
  106. });
  107. it('can remove an event with an internal _id', function() {
  108. var event;
  109. initCalendar({
  110. defaultDate: '2014-06-24',
  111. events: [ { title: 'event0', start: '2014-06-24' } ]
  112. });
  113. event = currentCalendar.clientEvents()[0];
  114. expect(typeof event).toBe('object');
  115. currentCalendar.removeEvents(event._id);
  116. expect(
  117. currentCalendar.clientEvents().length
  118. ).toBe(0);
  119. });
  120. // Verifies the actions in removeFunc executed correctly by calling checkFunc.
  121. function go(events, removeFunc, checkFunc, doneFunc) {
  122. var called = false;
  123. options.events = events;
  124. options.eventAfterAllRender = function() {
  125. if (!called) { // don't execute on subsequent removeEvents/next/prev
  126. called = true;
  127. checkAllEvents(); // make sure all events initially rendered correctly
  128. removeFunc(); // remove the events
  129. setTimeout(function() { // because the event rerender will be queued because we're a level deep
  130. checkFunc(); // check correctness
  131. // move the calendar back out of view, then back in
  132. $('#cal').fullCalendar('next');
  133. $('#cal').fullCalendar('prev');
  134. // array event sources should maintain the same state
  135. // whereas "dynamic" event sources should refetch and reset the state
  136. if ($.isArray(events)) {
  137. checkFunc(); // for issue 2187
  138. }
  139. else {
  140. checkAllEvents();
  141. }
  142. doneFunc();
  143. }, 0);
  144. }
  145. };
  146. $('#cal').fullCalendar(options);
  147. }
  148. // Checks to make sure all events have been rendered and that the calendar
  149. // has internal info on all the events.
  150. function checkAllEvents() {
  151. expect($('#cal').fullCalendar('clientEvents').length).toEqual(3);
  152. expect($('.fc-event').length).toEqual(3);
  153. }
  154. });