jasmine-jquery.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. 'use strict'
  2. jasmine.JQuery = function() {}
  3. jasmine.JQuery.browserTagCaseIndependentHtml = function(html) {
  4. var div = document.createElement('div')
  5. div.innerHTML = html
  6. return div.innerHTML
  7. }
  8. jasmine.JQuery.elementToString = function(element) {
  9. if (element instanceof HTMLElement) {
  10. return element.outerHTML
  11. } else {
  12. return element.html()
  13. }
  14. }
  15. jasmine.JQuery.matchersClass = {}
  16. var jQueryMatchers = {
  17. toHaveClass: function(className) {
  18. if (this.actual instanceof HTMLElement) {
  19. return this.actual.classList.contains(className)
  20. } else {
  21. return this.actual.hasClass(className)
  22. }
  23. },
  24. toBeVisible: function() {
  25. if (this.actual instanceof HTMLElement) {
  26. return this.actual.offsetWidth !== 0 || this.actual.offsetHeight !== 0
  27. } else {
  28. return this.actual.is(':visible')
  29. }
  30. },
  31. toBeHidden: function() {
  32. if (this.actual instanceof HTMLElement) {
  33. return this.actual.offsetWidth === 0 && this.actual.offsetHeight === 0
  34. } else {
  35. return this.actual.is(':hidden')
  36. }
  37. },
  38. toBeSelected: function() {
  39. if (this.actual instanceof HTMLElement) {
  40. return this.actual.selected
  41. } else {
  42. return this.actual.is(':selected')
  43. }
  44. },
  45. toBeChecked: function() {
  46. if (this.actual instanceof HTMLElement) {
  47. return this.actual.checked
  48. } else {
  49. return this.actual.is(':checked')
  50. }
  51. },
  52. toBeEmpty: function() {
  53. if (this.actual instanceof HTMLElement) {
  54. return this.actual.innerHTML === ''
  55. } else {
  56. return this.actual.is(':empty')
  57. }
  58. },
  59. toExist: function() {
  60. if (this.actual instanceof HTMLElement) {
  61. return true
  62. } else if (this.actual) {
  63. return this.actual.size() > 0
  64. } else {
  65. return false
  66. }
  67. },
  68. toHaveAttr: function(attributeName, expectedAttributeValue) {
  69. var actualAttributeValue
  70. if (this.actual instanceof HTMLElement) {
  71. actualAttributeValue = this.actual.getAttribute(attributeName)
  72. } else {
  73. actualAttributeValue = this.actual.attr(attributeName)
  74. }
  75. return hasProperty(actualAttributeValue, expectedAttributeValue)
  76. },
  77. toHaveId: function(id) {
  78. if (this.actual instanceof HTMLElement) {
  79. return this.actual.getAttribute('id') == id
  80. } else {
  81. return this.actual.attr('id') == id
  82. }
  83. },
  84. toHaveHtml: function(html) {
  85. var actualHTML
  86. if (this.actual instanceof HTMLElement) {
  87. actualHTML = this.actual.innerHTML
  88. } else {
  89. actualHTML = this.actual.html()
  90. }
  91. return actualHTML == jasmine.JQuery.browserTagCaseIndependentHtml(html)
  92. },
  93. toHaveText: function(text) {
  94. var actualText
  95. if (this.actual instanceof HTMLElement) {
  96. actualText = this.actual.textContent
  97. } else {
  98. actualText = this.actual.text()
  99. }
  100. if (text && typeof text.test === 'function') {
  101. return text.test(actualText)
  102. } else {
  103. return actualText == text
  104. }
  105. },
  106. toHaveValue: function(value) {
  107. if (this.actual instanceof HTMLElement) {
  108. return this.actual.value == value
  109. } else {
  110. return this.actual.val() == value
  111. }
  112. },
  113. toHaveData: function(key, expectedValue) {
  114. if (this.actual instanceof HTMLElement) {
  115. var camelCaseKey
  116. for (var part of key.split('-')) {
  117. if (camelCaseKey) {
  118. camelCaseKey += part[0].toUpperCase() + part.substring(1)
  119. } else {
  120. camelCaseKey = part
  121. }
  122. }
  123. return hasProperty(this.actual.dataset[camelCaseKey], expectedValue)
  124. } else {
  125. return hasProperty(this.actual.data(key), expectedValue)
  126. }
  127. },
  128. toMatchSelector: function(selector) {
  129. if (this.actual instanceof HTMLElement) {
  130. return this.actual.matches(selector)
  131. } else {
  132. return this.actual.is(selector)
  133. }
  134. },
  135. toContain: function(contained) {
  136. if (this.actual instanceof HTMLElement) {
  137. if (typeof contained === 'string') {
  138. return this.actual.querySelector(contained)
  139. } else {
  140. return this.actual.contains(contained)
  141. }
  142. } else {
  143. return this.actual.find(contained).size() > 0
  144. }
  145. },
  146. toBeDisabled: function(selector){
  147. if (this.actual instanceof HTMLElement) {
  148. return this.actual.disabled
  149. } else {
  150. return this.actual.is(':disabled')
  151. }
  152. },
  153. // tests the existence of a specific event binding
  154. toHandle: function(eventName) {
  155. var events = this.actual.data("events")
  156. return events && events[eventName].length > 0
  157. },
  158. // tests the existence of a specific event binding + handler
  159. toHandleWith: function(eventName, eventHandler) {
  160. var stack = this.actual.data("events")[eventName]
  161. var i
  162. for (i = 0; i < stack.length; i++) {
  163. if (stack[i].handler == eventHandler) {
  164. return true
  165. }
  166. }
  167. return false
  168. }
  169. }
  170. var hasProperty = function(actualValue, expectedValue) {
  171. if (expectedValue === undefined) {
  172. return actualValue !== undefined
  173. }
  174. return actualValue == expectedValue
  175. }
  176. var bindMatcher = function(methodName) {
  177. var builtInMatcher = jasmine.Matchers.prototype[methodName]
  178. jasmine.JQuery.matchersClass[methodName] = function() {
  179. if (this.actual && this.actual.jquery || this.actual instanceof HTMLElement) {
  180. var result = jQueryMatchers[methodName].apply(this, arguments)
  181. this.actual = jasmine.JQuery.elementToString(this.actual)
  182. return result
  183. }
  184. if (builtInMatcher) {
  185. return builtInMatcher.apply(this, arguments)
  186. }
  187. return false
  188. }
  189. }
  190. for(var methodName in jQueryMatchers) {
  191. bindMatcher(methodName)
  192. }
  193. beforeEach(function() {
  194. this.addMatchers(jasmine.JQuery.matchersClass)
  195. })