removeEvents.js 4.8 KB

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