removeEventSource.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. } else {
  70. expect($('.event1').length).toBe(0)
  71. expect($('.event2').length).toBe(1)
  72. done()
  73. }
  74. }
  75. $('#cal').fullCalendar(options)
  76. $('#cal').fullCalendar('removeEventSource', source1)
  77. $('#cal').fullCalendar('addEventSource', source2)
  78. })
  79. describe('when multiple sources share the same fetching function', function() {
  80. var fetchFunc = function(start, end, timezone, callback) {
  81. callback([ {
  82. title: 'event',
  83. start: '2014-08-01T02:00:00'
  84. } ])
  85. }
  86. beforeEach(function() {
  87. options.eventSources = [
  88. { events: fetchFunc, className: 'event1', id: 'source1' },
  89. { events: fetchFunc, className: 'event2', id: 'source2' }
  90. ]
  91. })
  92. describe('when called with the raw function', function() {
  93. it('removes events from all matching sources', function() {
  94. $('#cal').fullCalendar(options)
  95. expect($('.fc-event').length).toBe(2)
  96. expect($('.event1').length).toBe(1)
  97. expect($('.event2').length).toBe(1)
  98. $('#cal').fullCalendar('removeEventSource', fetchFunc)
  99. expect($('.fc-event').length).toBe(0)
  100. })
  101. })
  102. describe('when called with proper source object', function() {
  103. it('removes events only from the specific source', function() {
  104. $('#cal').fullCalendar(options)
  105. expect($('.fc-event').length).toBe(2)
  106. expect($('.event1').length).toBe(1)
  107. expect($('.event2').length).toBe(1)
  108. var source = $('#cal').fullCalendar('getEventSourceById', 'source2')
  109. $('#cal').fullCalendar('removeEventSource', source)
  110. expect($('.fc-event').length).toBe(1)
  111. expect($('.event1').length).toBe(1)
  112. expect($('.event2').length).toBe(0)
  113. })
  114. })
  115. })
  116. function testInput(eventInput) {
  117. it('correctly removes events provided via `events` at initialization', function(done) {
  118. var callCnt = 0
  119. options.eventAfterAllRender = function() {
  120. callCnt++
  121. if (callCnt === 1) {
  122. expectEventCnt(2)
  123. $('#cal').fullCalendar('removeEventSource', eventInput)
  124. } else if (callCnt === 2) {
  125. expectEventCnt(0)
  126. done()
  127. }
  128. }
  129. options.events = eventInput
  130. $('#cal').fullCalendar(options)
  131. })
  132. it('correctly removes events provided via `eventSources` at initialization', function(done) {
  133. var callCnt = 0
  134. options.eventAfterAllRender = function() {
  135. callCnt++
  136. if (callCnt === 1) {
  137. expectEventCnt(2)
  138. $('#cal').fullCalendar('removeEventSource', eventInput)
  139. } else if (callCnt === 2) {
  140. expectEventCnt(0)
  141. done()
  142. }
  143. }
  144. options.eventSources = [ eventInput ]
  145. $('#cal').fullCalendar(options)
  146. })
  147. it('correctly removes events provided via `addEventSource` method', function(done) {
  148. var callCnt = 0
  149. options.eventAfterAllRender = function() {
  150. callCnt++
  151. if (callCnt === 1) {
  152. $('#cal').fullCalendar('addEventSource', eventInput)
  153. } else if (callCnt === 2) {
  154. expectEventCnt(2)
  155. $('#cal').fullCalendar('removeEventSource', eventInput)
  156. } else if (callCnt === 3) {
  157. expectEventCnt(0)
  158. done()
  159. }
  160. }
  161. $('#cal').fullCalendar(options)
  162. })
  163. }
  164. function buildEventArray() {
  165. return [
  166. { title: 'event1', start: '2014-08-01' },
  167. { title: 'event2', start: '2014-08-02' }
  168. ]
  169. }
  170. function expectEventCnt(cnt) {
  171. expect($('.fc-event').length).toBe(cnt)
  172. expect($('#cal').fullCalendar('clientEvents').length).toBe(cnt)
  173. }
  174. })