globals.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Jasmine Enhancements
  2. // ---------------------------------------------------------------------------------------------------------------------
  3. // like `it`, but with the ability to return a promise
  4. window.pit = function(description, runFunc) {
  5. it(description, function(done) {
  6. runFunc().then(done)
  7. })
  8. }
  9. // Setup / Teardown
  10. // ---------------------------------------------------------------------------------------------------------------------
  11. window.optionsStack = null
  12. window.currentCalendar = null
  13. beforeEach(function() {
  14. window.optionsStack = []
  15. })
  16. afterEach(function() {
  17. window.optionsStack = null
  18. if (window.currentCalendar) {
  19. window.currentCalendar.destroy()
  20. window.currentCalendar = null
  21. }
  22. $('#calendar').remove()
  23. })
  24. // Calendar Options and Initialization
  25. // ---------------------------------------------------------------------------------------------------------------------
  26. window.pushOptions = function(options) {
  27. beforeEach(function() {
  28. return window.optionsStack.push(options)
  29. })
  30. }
  31. // called within an `it`
  32. window.spyOnCalendarCallback = function(name, func) {
  33. var options = {}
  34. options[name] = func
  35. spyOn(options, name).and.callThrough()
  36. window.optionsStack.push(options)
  37. return options[name]
  38. }
  39. window.initCalendar = function(options, el) {
  40. var Calendar = $.fullCalendar.Calendar
  41. var $el
  42. if (options) {
  43. window.optionsStack.push(options)
  44. }
  45. if (el) {
  46. $el = $(el)
  47. } else {
  48. $el = $('<div id="calendar">').appendTo('body')
  49. }
  50. window.currentCalendar = new Calendar($el, getCurrentOptions()) // set the global
  51. return window.currentCalendar.render()
  52. }
  53. window.getCurrentOptions = function() {
  54. return $.extend.apply($, [ {} ].concat(window.optionsStack))
  55. }
  56. // Categorizing Tests
  57. // ---------------------------------------------------------------------------------------------------------------------
  58. /*
  59. describeOptions(optionName, descriptionAndValueHash, callback)
  60. describeOptions(descriptionAndOptionsHash, callback)
  61. */
  62. window.describeOptions = function(optName, hash, callback) {
  63. if ($.type(optName) === 'object') {
  64. callback = hash
  65. hash = optName
  66. optName = null
  67. }
  68. $.each(hash, function(desc, val) {
  69. var opts
  70. if (optName) {
  71. opts = {}
  72. opts[optName] = val
  73. } else {
  74. opts = val
  75. }
  76. opts = $.extend(true, {}, opts)
  77. describe(desc, function() {
  78. pushOptions(opts)
  79. callback(val)
  80. })
  81. })
  82. }
  83. window.describeValues = function(hash, callback) {
  84. $.each(hash, function(desc, val) {
  85. describe(desc, function() {
  86. callback(val)
  87. })
  88. })
  89. }
  90. // Timezone Tests (needed?)
  91. // ---------------------------------------------------------------------------------------------------------------------
  92. const timezoneScenarios = {
  93. none: {
  94. description: 'when no timezone',
  95. value: null,
  96. moment: function(str) {
  97. return $.fullCalendar.moment.parseZone(str)
  98. }
  99. },
  100. local: {
  101. description: 'when local timezone',
  102. value: 'local',
  103. moment: function(str) {
  104. return moment(str)
  105. }
  106. },
  107. UTC: {
  108. description: 'when UTC timezone',
  109. value: 'UTC',
  110. moment: function(str) {
  111. return moment.utc(str)
  112. }
  113. }
  114. }
  115. window.describeTimezones = function(callback) {
  116. $.each(timezoneScenarios, function(name, scenario) {
  117. describe(scenario.description, function() {
  118. pushOptions({
  119. timezone: name
  120. })
  121. callback(scenario)
  122. })
  123. })
  124. }
  125. window.describeTimezone = function(name, callback) {
  126. var scenario = timezoneScenarios[name]
  127. describe(scenario.description, function() {
  128. pushOptions({
  129. timezone: name
  130. })
  131. callback(scenario)
  132. })
  133. }
  134. // Misc
  135. // ---------------------------------------------------------------------------------------------------------------------
  136. window.oneCall = function(func) {
  137. var called
  138. called = false
  139. return function() {
  140. if (!called) {
  141. called = true
  142. return func.apply(this, arguments)
  143. }
  144. }
  145. }
  146. window.spyOnMethod = function(Class, methodName, dontCallThrough) {
  147. var origMethod = Class.prototype.hasOwnProperty(methodName)
  148. ? Class.prototype[methodName]
  149. : null
  150. var spy = spyOn(Class.prototype, methodName)
  151. if (!dontCallThrough) {
  152. spy = spy.and.callThrough()
  153. }
  154. spy.restore = function() {
  155. if (origMethod) {
  156. Class.prototype[methodName] = origMethod
  157. } else {
  158. delete Class.prototype[methodName]
  159. }
  160. }
  161. return spy
  162. }
  163. // wraps an existing function in a spy, calling through to the function
  164. window.spyCall = function(func) {
  165. func = func || function() {}
  166. const obj = { func }
  167. spyOn(obj, 'func').and.callThrough()
  168. return obj.func
  169. }