visibleRange.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { expectActiveRange } from './ViewDateUtils'
  2. describe('visibleRange', function() {
  3. describe('when custom view with a flexible range', function() {
  4. pushOptions({
  5. defaultView: 'agenda'
  6. })
  7. describe('when given a valid date range', function() {
  8. var startInput = '2017-06-26'
  9. var endInput = '2017-06-29'
  10. describeOptions('visibleRange', {
  11. 'of moment objects': {
  12. start: $.fullCalendar.moment(startInput),
  13. end: $.fullCalendar.moment(endInput)
  14. },
  15. 'of strings': {
  16. start: startInput,
  17. end: endInput
  18. },
  19. 'of a function that returns moment objects': function() {
  20. return {
  21. start: $.fullCalendar.moment(startInput),
  22. end: $.fullCalendar.moment(endInput)
  23. }
  24. },
  25. 'of a function that returns strings': function() {
  26. return {
  27. start: startInput,
  28. end: endInput
  29. }
  30. }
  31. }, function() {
  32. it('gets set to the given range', function() {
  33. initCalendar()
  34. expectActiveRange(startInput, endInput)
  35. })
  36. })
  37. it('works as a custom view', function() {
  38. initCalendar({
  39. views: {
  40. myCustomView: {
  41. type: 'agenda',
  42. visibleRange: {
  43. start: startInput,
  44. end: endInput
  45. }
  46. }
  47. },
  48. defaultView: 'myCustomView'
  49. })
  50. expectActiveRange(startInput, endInput)
  51. })
  52. it('ignores dateAlignment', function() {
  53. initCalendar({
  54. dateAlignment: 'month',
  55. visibleRange: {
  56. start: startInput,
  57. end: endInput
  58. }
  59. })
  60. expectActiveRange(startInput, endInput)
  61. })
  62. it('works as a dynamic option', function() {
  63. initCalendar({
  64. defaultView: 'basic'
  65. })
  66. currentCalendar.option('visibleRange', {
  67. start: startInput,
  68. end: endInput
  69. })
  70. expectActiveRange(startInput, endInput)
  71. })
  72. })
  73. describe('when a function', function() {
  74. var defaultDateInput = '2017-06-08T12:30:00'
  75. it('receives the calendar\'s defaultDate, timezoneless', function() {
  76. var matched = false
  77. initCalendar({
  78. defaultDate: defaultDateInput,
  79. visibleRange: function(date) {
  80. // this function will receive the date for prev/next,
  81. // which should be ignored. make sure just one call matches.
  82. if (date.format() === defaultDateInput) {
  83. matched = true
  84. }
  85. }
  86. })
  87. expect(matched).toBe(true)
  88. })
  89. it('receives the calendar\'s defaultDate, with UTC timezone', function() {
  90. var matched = false
  91. initCalendar({
  92. timezone: 'UTC',
  93. defaultDate: defaultDateInput,
  94. visibleRange: function(date) {
  95. // this function will receive the date for prev/next,
  96. // which should be ignored. make sure just one call matches.
  97. if (date.format() === defaultDateInput + 'Z') {
  98. matched = true
  99. }
  100. }
  101. })
  102. expect(matched).toBe(true)
  103. })
  104. it('does not cause side effects when given date is mutated', function() {
  105. initCalendar({
  106. defaultDate: defaultDateInput,
  107. visibleRange: function(date) {
  108. date.add(1, 'year')
  109. }
  110. })
  111. expect(currentCalendar.getDate()).toEqualMoment(defaultDateInput)
  112. })
  113. })
  114. describe('when given an invalid range', function() {
  115. describeOptions('visibleRange', {
  116. 'with end before start': {
  117. start: '2017-06-18',
  118. end: '2017-06-15'
  119. },
  120. 'with no end': {
  121. start: '2017-06-18'
  122. },
  123. 'with no start': {
  124. end: '2017-06-15'
  125. }
  126. }, function() {
  127. it('defaults to the defaultDate', function() { // TODO: have it report an warning
  128. initCalendar({
  129. defaultDate: '2017-08-01'
  130. })
  131. expectActiveRange('2017-08-01', '2017-08-02')
  132. })
  133. })
  134. })
  135. describe('when later switching to a one-day view', function() {
  136. it('constrains the current date to the start of visibleRange', function() {
  137. initCalendar({
  138. defaultDate: '2017-06-25',
  139. visibleRange: {
  140. start: '2017-06-26',
  141. end: '2017-06-29'
  142. }
  143. })
  144. currentCalendar.changeView('agendaDay')
  145. expectActiveRange('2017-06-26', '2017-06-27')
  146. })
  147. it('constrains the current date to the end of visibleRange', function() {
  148. initCalendar({
  149. defaultDate: '2017-07-01',
  150. visibleRange: {
  151. start: '2017-06-26',
  152. end: '2017-06-29'
  153. }
  154. })
  155. currentCalendar.changeView('agendaDay')
  156. expectActiveRange('2017-06-28', '2017-06-29')
  157. })
  158. })
  159. })
  160. describe('when a list view', function() {
  161. pushOptions({
  162. defaultView: 'list',
  163. visibleRange: {
  164. start: '2017-06-07',
  165. end: '2017-06-10'
  166. },
  167. events: [
  168. { start: '2017-06-08' }
  169. ]
  170. })
  171. it('respects the given range', function() {
  172. initCalendar()
  173. expectActiveRange('2017-06-07', '2017-06-10')
  174. })
  175. })
  176. describe('when custom view with fixed duration', function() {
  177. pushOptions({
  178. defaultDate: '2015-06-08',
  179. defaultView: 'agenda',
  180. duration: { days: 3 }
  181. })
  182. it('ignores the given visibleRange', function() {
  183. initCalendar({
  184. visibleRange: {
  185. start: '2017-06-29',
  186. end: '2017-07-04'
  187. }
  188. })
  189. expectActiveRange('2015-06-08', '2015-06-11')
  190. })
  191. })
  192. describe('when standard view', function() {
  193. pushOptions({
  194. defaultDate: '2015-06-08',
  195. defaultView: 'agendaWeek'
  196. })
  197. it('ignores the given visibleRange', function() {
  198. initCalendar({
  199. visibleRange: {
  200. start: '2017-06-29',
  201. end: '2017-07-04'
  202. }
  203. })
  204. expectActiveRange('2015-06-07', '2015-06-14')
  205. })
  206. })
  207. })