You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

704 lines
22 KiB

  1. /*!
  2. Jasmine-jQuery: a set of jQuery helpers for Jasmine tests.
  3. Version 1.5.91
  4. https://github.com/velesin/jasmine-jquery
  5. Copyright (c) 2010-2013 Wojciech Zawistowski, Travis Jeffery
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. +function (jasmine, $) { "use strict";
  24. jasmine.spiedEventsKey = function (selector, eventName) {
  25. return [$(selector).selector, eventName].toString()
  26. }
  27. jasmine.getFixtures = function () {
  28. return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures()
  29. }
  30. jasmine.getStyleFixtures = function () {
  31. return jasmine.currentStyleFixtures_ = jasmine.currentStyleFixtures_ || new jasmine.StyleFixtures()
  32. }
  33. jasmine.Fixtures = function () {
  34. this.containerId = 'jasmine-fixtures'
  35. this.fixturesCache_ = {}
  36. this.fixturesPath = 'spec/javascripts/fixtures'
  37. }
  38. jasmine.Fixtures.prototype.set = function (html) {
  39. this.cleanUp()
  40. return this.createContainer_(html)
  41. }
  42. jasmine.Fixtures.prototype.appendSet= function (html) {
  43. this.addToContainer_(html)
  44. }
  45. jasmine.Fixtures.prototype.preload = function () {
  46. this.read.apply(this, arguments)
  47. }
  48. jasmine.Fixtures.prototype.load = function () {
  49. this.cleanUp()
  50. this.createContainer_(this.read.apply(this, arguments))
  51. }
  52. jasmine.Fixtures.prototype.appendLoad = function () {
  53. this.addToContainer_(this.read.apply(this, arguments))
  54. }
  55. jasmine.Fixtures.prototype.read = function () {
  56. var htmlChunks = []
  57. , fixtureUrls = arguments
  58. for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
  59. htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex]))
  60. }
  61. return htmlChunks.join('')
  62. }
  63. jasmine.Fixtures.prototype.clearCache = function () {
  64. this.fixturesCache_ = {}
  65. }
  66. jasmine.Fixtures.prototype.cleanUp = function () {
  67. $('#' + this.containerId).remove()
  68. }
  69. jasmine.Fixtures.prototype.sandbox = function (attributes) {
  70. var attributesToSet = attributes || {}
  71. return $('<div id="sandbox" />').attr(attributesToSet)
  72. }
  73. jasmine.Fixtures.prototype.createContainer_ = function (html) {
  74. var container = $('<div>')
  75. .attr('id', this.containerId)
  76. .html(html)
  77. $(document.body).append(container)
  78. return container
  79. }
  80. jasmine.Fixtures.prototype.addToContainer_ = function (html){
  81. var container = $(document.body).find('#'+this.containerId).append(html)
  82. if(!container.length){
  83. this.createContainer_(html)
  84. }
  85. }
  86. jasmine.Fixtures.prototype.getFixtureHtml_ = function (url) {
  87. if (typeof this.fixturesCache_[url] === 'undefined') {
  88. this.loadFixtureIntoCache_(url)
  89. }
  90. return this.fixturesCache_[url]
  91. }
  92. jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) {
  93. var self = this
  94. , url = this.makeFixtureUrl_(relativeUrl)
  95. , request = $.ajax({
  96. async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
  97. cache: false,
  98. url: url,
  99. success: function (data, status, $xhr) {
  100. self.fixturesCache_[relativeUrl] = $xhr.responseText
  101. },
  102. error: function (jqXHR, status, errorThrown) {
  103. throw new Error('Fixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')')
  104. }
  105. })
  106. }
  107. jasmine.Fixtures.prototype.makeFixtureUrl_ = function (relativeUrl){
  108. return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
  109. }
  110. jasmine.Fixtures.prototype.proxyCallTo_ = function (methodName, passedArguments) {
  111. return this[methodName].apply(this, passedArguments)
  112. }
  113. jasmine.StyleFixtures = function () {
  114. this.fixturesCache_ = {}
  115. this.fixturesNodes_ = []
  116. this.fixturesPath = 'spec/javascripts/fixtures'
  117. }
  118. jasmine.StyleFixtures.prototype.set = function (css) {
  119. this.cleanUp()
  120. this.createStyle_(css)
  121. }
  122. jasmine.StyleFixtures.prototype.appendSet = function (css) {
  123. this.createStyle_(css)
  124. }
  125. jasmine.StyleFixtures.prototype.preload = function () {
  126. this.read_.apply(this, arguments)
  127. }
  128. jasmine.StyleFixtures.prototype.load = function () {
  129. this.cleanUp()
  130. this.createStyle_(this.read_.apply(this, arguments))
  131. }
  132. jasmine.StyleFixtures.prototype.appendLoad = function () {
  133. this.createStyle_(this.read_.apply(this, arguments))
  134. }
  135. jasmine.StyleFixtures.prototype.cleanUp = function () {
  136. while(this.fixturesNodes_.length) {
  137. this.fixturesNodes_.pop().remove()
  138. }
  139. }
  140. jasmine.StyleFixtures.prototype.createStyle_ = function (html) {
  141. var styleText = $('<div></div>').html(html).text()
  142. , style = $('<style>' + styleText + '</style>')
  143. this.fixturesNodes_.push(style)
  144. $('head').append(style)
  145. }
  146. jasmine.StyleFixtures.prototype.clearCache = jasmine.Fixtures.prototype.clearCache
  147. jasmine.StyleFixtures.prototype.read_ = jasmine.Fixtures.prototype.read
  148. jasmine.StyleFixtures.prototype.getFixtureHtml_ = jasmine.Fixtures.prototype.getFixtureHtml_
  149. jasmine.StyleFixtures.prototype.loadFixtureIntoCache_ = jasmine.Fixtures.prototype.loadFixtureIntoCache_
  150. jasmine.StyleFixtures.prototype.makeFixtureUrl_ = jasmine.Fixtures.prototype.makeFixtureUrl_
  151. jasmine.StyleFixtures.prototype.proxyCallTo_ = jasmine.Fixtures.prototype.proxyCallTo_
  152. jasmine.getJSONFixtures = function () {
  153. return jasmine.currentJSONFixtures_ = jasmine.currentJSONFixtures_ || new jasmine.JSONFixtures()
  154. }
  155. jasmine.JSONFixtures = function () {
  156. this.fixturesCache_ = {}
  157. this.fixturesPath = 'spec/javascripts/fixtures/json'
  158. }
  159. jasmine.JSONFixtures.prototype.load = function () {
  160. this.read.apply(this, arguments)
  161. return this.fixturesCache_
  162. }
  163. jasmine.JSONFixtures.prototype.read = function () {
  164. var fixtureUrls = arguments
  165. for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
  166. this.getFixtureData_(fixtureUrls[urlIndex])
  167. }
  168. return this.fixturesCache_
  169. }
  170. jasmine.JSONFixtures.prototype.clearCache = function () {
  171. this.fixturesCache_ = {}
  172. }
  173. jasmine.JSONFixtures.prototype.getFixtureData_ = function (url) {
  174. if (!this.fixturesCache_[url]) this.loadFixtureIntoCache_(url)
  175. return this.fixturesCache_[url]
  176. }
  177. jasmine.JSONFixtures.prototype.loadFixtureIntoCache_ = function (relativeUrl) {
  178. var self = this
  179. , url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
  180. $.ajax({
  181. async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
  182. cache: false,
  183. dataType: 'json',
  184. url: url,
  185. success: function (data) {
  186. self.fixturesCache_[relativeUrl] = data
  187. },
  188. error: function (jqXHR, status, errorThrown) {
  189. throw new Error('JSONFixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')')
  190. }
  191. })
  192. }
  193. jasmine.JSONFixtures.prototype.proxyCallTo_ = function (methodName, passedArguments) {
  194. return this[methodName].apply(this, passedArguments)
  195. }
  196. jasmine.JQuery = function () {}
  197. jasmine.JQuery.browserTagCaseIndependentHtml = function (html) {
  198. return $('<div/>').append(html).html()
  199. }
  200. jasmine.JQuery.elementToString = function (element) {
  201. var domEl = $(element).get(0)
  202. if (domEl === undefined || domEl.cloneNode)
  203. return $('<div />').append($(element).clone()).html()
  204. else
  205. return element.toString()
  206. }
  207. jasmine.JQuery.matchersClass = {}
  208. !function (namespace) {
  209. var data = {
  210. spiedEvents: {}
  211. , handlers: []
  212. }
  213. namespace.events = {
  214. spyOn: function (selector, eventName) {
  215. var handler = function (e) {
  216. data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] = jasmine.util.argsToArray(arguments)
  217. }
  218. $(selector).on(eventName, handler)
  219. data.handlers.push(handler)
  220. return {
  221. selector: selector,
  222. eventName: eventName,
  223. handler: handler,
  224. reset: function (){
  225. delete data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
  226. }
  227. }
  228. },
  229. args: function (selector, eventName) {
  230. var actualArgs = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
  231. if (!actualArgs) {
  232. throw "There is no spy for " + eventName + " on " + selector.toString() + ". Make sure to create a spy using spyOnEvent."
  233. }
  234. return actualArgs
  235. },
  236. wasTriggered: function (selector, eventName) {
  237. return !!(data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)])
  238. },
  239. wasTriggeredWith: function (selector, eventName, expectedArgs, env) {
  240. var actualArgs = jasmine.JQuery.events.args(selector, eventName).slice(1)
  241. if (Object.prototype.toString.call(expectedArgs) !== '[object Array]') {
  242. actualArgs = actualArgs[0]
  243. }
  244. return env.equals_(expectedArgs, actualArgs)
  245. },
  246. wasPrevented: function (selector, eventName) {
  247. var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
  248. , e = args ? args[0] : undefined
  249. return e && e.isDefaultPrevented()
  250. },
  251. wasStopped: function (selector, eventName) {
  252. var args = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
  253. , e = args ? args[0] : undefined
  254. return e && e.isPropagationStopped()
  255. },
  256. cleanUp: function () {
  257. data.spiedEvents = {}
  258. data.handlers = []
  259. }
  260. }
  261. }(jasmine.JQuery)
  262. !function (){
  263. var jQueryMatchers = {
  264. toHaveClass: function (className) {
  265. return this.actual.hasClass(className)
  266. },
  267. toHaveCss: function (css){
  268. for (var prop in css){
  269. var value = css[prop]
  270. // see issue #147 on gh
  271. ;if (value === 'auto' && this.actual.get(0).style[prop] === 'auto') continue
  272. if (this.actual.css(prop) !== value) return false
  273. }
  274. return true
  275. },
  276. toBeVisible: function () {
  277. return this.actual.is(':visible')
  278. },
  279. toBeHidden: function () {
  280. return this.actual.is(':hidden')
  281. },
  282. toBeSelected: function () {
  283. return this.actual.is(':selected')
  284. },
  285. toBeChecked: function () {
  286. return this.actual.is(':checked')
  287. },
  288. toBeEmpty: function () {
  289. return this.actual.is(':empty')
  290. },
  291. toExist: function () {
  292. return this.actual.length
  293. },
  294. toHaveLength: function (length) {
  295. return this.actual.length === length
  296. },
  297. toHaveAttr: function (attributeName, expectedAttributeValue) {
  298. return hasProperty(this.actual.attr(attributeName), expectedAttributeValue)
  299. },
  300. toHaveProp: function (propertyName, expectedPropertyValue) {
  301. return hasProperty(this.actual.prop(propertyName), expectedPropertyValue)
  302. },
  303. toHaveId: function (id) {
  304. return this.actual.attr('id') == id
  305. },
  306. toHaveHtml: function (html) {
  307. return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html)
  308. },
  309. toContainHtml: function (html){
  310. var actualHtml = this.actual.html()
  311. , expectedHtml = jasmine.JQuery.browserTagCaseIndependentHtml(html)
  312. return (actualHtml.indexOf(expectedHtml) >= 0)
  313. },
  314. toHaveText: function (text) {
  315. var trimmedText = $.trim(this.actual.text())
  316. if (text && $.isFunction(text.test)) {
  317. return text.test(trimmedText)
  318. } else {
  319. return trimmedText == text
  320. }
  321. },
  322. toContainText: function (text) {
  323. var trimmedText = $.trim(this.actual.text())
  324. if (text && $.isFunction(text.test)) {
  325. return text.test(trimmedText)
  326. } else {
  327. return trimmedText.indexOf(text) != -1
  328. }
  329. },
  330. toHaveValue: function (value) {
  331. return this.actual.val() === value
  332. },
  333. toHaveData: function (key, expectedValue) {
  334. return hasProperty(this.actual.data(key), expectedValue)
  335. },
  336. toBe: function (selector) {
  337. return this.actual.is(selector)
  338. },
  339. toContain: function (selector) {
  340. return this.actual.find(selector).length
  341. },
  342. toBeMatchedBy: function (selector) {
  343. return this.actual.filter(selector).length
  344. },
  345. toBeDisabled: function (selector){
  346. return this.actual.is(':disabled')
  347. },
  348. toBeFocused: function (selector) {
  349. return this.actual[0] === this.actual[0].ownerDocument.activeElement
  350. },
  351. toHandle: function (event) {
  352. var events = $._data(this.actual.get(0), "events")
  353. if(!events || !event || typeof event !== "string") {
  354. return false
  355. }
  356. var namespaces = event.split(".")
  357. , eventType = namespaces.shift()
  358. , sortedNamespaces = namespaces.slice(0).sort()
  359. , namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)")
  360. if(events[eventType] && namespaces.length) {
  361. for(var i = 0; i < events[eventType].length; i++) {
  362. var namespace = events[eventType][i].namespace
  363. if(namespaceRegExp.test(namespace)) {
  364. return true
  365. }
  366. }
  367. } else {
  368. return events[eventType] && events[eventType].length > 0
  369. }
  370. },
  371. toHandleWith: function (eventName, eventHandler) {
  372. var normalizedEventName = eventName.split('.')[0]
  373. , stack = $._data(this.actual.get(0), "events")[normalizedEventName]
  374. for (var i = 0; i < stack.length; i++) {
  375. if (stack[i].handler == eventHandler) return true
  376. }
  377. return false
  378. }
  379. }
  380. var hasProperty = function (actualValue, expectedValue) {
  381. if (expectedValue === undefined) return actualValue !== undefined
  382. return actualValue == expectedValue
  383. }
  384. var bindMatcher = function (methodName) {
  385. var builtInMatcher = jasmine.Matchers.prototype[methodName]
  386. jasmine.JQuery.matchersClass[methodName] = function () {
  387. if (this.actual
  388. && (this.actual instanceof $
  389. || jasmine.isDomNode(this.actual))) {
  390. this.actual = $(this.actual)
  391. var result = jQueryMatchers[methodName].apply(this, arguments)
  392. , element
  393. if (this.actual.get && (element = this.actual.get()[0]) && !$.isWindow(element) && element.tagName !== "HTML")
  394. this.actual = jasmine.JQuery.elementToString(this.actual)
  395. return result
  396. }
  397. if (builtInMatcher) {
  398. return builtInMatcher.apply(this, arguments)
  399. }
  400. return false
  401. }
  402. }
  403. for(var methodName in jQueryMatchers) {
  404. bindMatcher(methodName)
  405. }
  406. }()
  407. beforeEach(function () {
  408. this.addMatchers(jasmine.JQuery.matchersClass)
  409. this.addMatchers({
  410. toHaveBeenTriggeredOn: function (selector) {
  411. this.message = function () {
  412. return [
  413. "Expected event " + this.actual + " to have been triggered on " + selector,
  414. "Expected event " + this.actual + " not to have been triggered on " + selector
  415. ]
  416. }
  417. return jasmine.JQuery.events.wasTriggered(selector, this.actual)
  418. }
  419. })
  420. this.addMatchers({
  421. toHaveBeenTriggered: function (){
  422. var eventName = this.actual.eventName
  423. , selector = this.actual.selector
  424. this.message = function () {
  425. return [
  426. "Expected event " + eventName + " to have been triggered on " + selector,
  427. "Expected event " + eventName + " not to have been triggered on " + selector
  428. ]
  429. }
  430. return jasmine.JQuery.events.wasTriggered(selector, eventName)
  431. }
  432. })
  433. this.addMatchers({
  434. toHaveBeenTriggeredOnAndWith: function () {
  435. var selector = arguments[0]
  436. , expectedArgs = arguments[1]
  437. , wasTriggered = jasmine.JQuery.events.wasTriggered(selector, this.actual)
  438. this.message = function () {
  439. if (wasTriggered) {
  440. var actualArgs = jasmine.JQuery.events.args(selector, this.actual, expectedArgs)[1]
  441. return [
  442. "Expected event " + this.actual + " to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs),
  443. "Expected event " + this.actual + " not to have been triggered with " + jasmine.pp(expectedArgs) + " but it was triggered with " + jasmine.pp(actualArgs)
  444. ]
  445. } else {
  446. return [
  447. "Expected event " + this.actual + " to have been triggered on " + selector,
  448. "Expected event " + this.actual + " not to have been triggered on " + selector
  449. ]
  450. }
  451. }
  452. return wasTriggered && jasmine.JQuery.events.wasTriggeredWith(selector, this.actual, expectedArgs, this.env)
  453. }
  454. })
  455. this.addMatchers({
  456. toHaveBeenPreventedOn: function (selector) {
  457. this.message = function () {
  458. return [
  459. "Expected event " + this.actual + " to have been prevented on " + selector,
  460. "Expected event " + this.actual + " not to have been prevented on " + selector
  461. ]
  462. }
  463. return jasmine.JQuery.events.wasPrevented(selector, this.actual)
  464. }
  465. })
  466. this.addMatchers({
  467. toHaveBeenPrevented: function () {
  468. var eventName = this.actual.eventName
  469. , selector = this.actual.selector
  470. this.message = function () {
  471. return [
  472. "Expected event " + eventName + " to have been prevented on " + selector,
  473. "Expected event " + eventName + " not to have been prevented on " + selector
  474. ]
  475. }
  476. return jasmine.JQuery.events.wasPrevented(selector, eventName)
  477. }
  478. })
  479. this.addMatchers({
  480. toHaveBeenStoppedOn: function (selector) {
  481. this.message = function () {
  482. return [
  483. "Expected event " + this.actual + " to have been stopped on " + selector,
  484. "Expected event " + this.actual + " not to have been stopped on " + selector
  485. ]
  486. }
  487. return jasmine.JQuery.events.wasStopped(selector, this.actual)
  488. }
  489. })
  490. this.addMatchers({
  491. toHaveBeenStopped: function () {
  492. var eventName = this.actual.eventName
  493. , selector = this.actual.selector
  494. this.message = function () {
  495. return [
  496. "Expected event " + eventName + " to have been stopped on " + selector,
  497. "Expected event " + eventName + " not to have been stopped on " + selector
  498. ]
  499. }
  500. return jasmine.JQuery.events.wasStopped(selector, eventName)
  501. }
  502. })
  503. jasmine.getEnv().addEqualityTester(function (a, b) {
  504. if(a instanceof jQuery && b instanceof jQuery) {
  505. if(a.size() != b.size()) {
  506. return jasmine.undefined
  507. }
  508. else if(a.is(b)) {
  509. return true
  510. }
  511. }
  512. return jasmine.undefined
  513. })
  514. })
  515. afterEach(function () {
  516. jasmine.getFixtures().cleanUp()
  517. jasmine.getStyleFixtures().cleanUp()
  518. jasmine.JQuery.events.cleanUp()
  519. })
  520. }(window.jasmine, window.jQuery)
  521. +function (jasmine, global) { "use strict";
  522. global.readFixtures = function () {
  523. return jasmine.getFixtures().proxyCallTo_('read', arguments)
  524. }
  525. global.preloadFixtures = function () {
  526. jasmine.getFixtures().proxyCallTo_('preload', arguments)
  527. }
  528. global.loadFixtures = function () {
  529. jasmine.getFixtures().proxyCallTo_('load', arguments)
  530. }
  531. global.appendLoadFixtures = function () {
  532. jasmine.getFixtures().proxyCallTo_('appendLoad', arguments)
  533. }
  534. global.setFixtures = function (html) {
  535. return jasmine.getFixtures().proxyCallTo_('set', arguments)
  536. }
  537. global.appendSetFixtures = function () {
  538. jasmine.getFixtures().proxyCallTo_('appendSet', arguments)
  539. }
  540. global.sandbox = function (attributes) {
  541. return jasmine.getFixtures().sandbox(attributes)
  542. }
  543. global.spyOnEvent = function (selector, eventName) {
  544. return jasmine.JQuery.events.spyOn(selector, eventName)
  545. }
  546. global.preloadStyleFixtures = function () {
  547. jasmine.getStyleFixtures().proxyCallTo_('preload', arguments)
  548. }
  549. global.loadStyleFixtures = function () {
  550. jasmine.getStyleFixtures().proxyCallTo_('load', arguments)
  551. }
  552. global.appendLoadStyleFixtures = function () {
  553. jasmine.getStyleFixtures().proxyCallTo_('appendLoad', arguments)
  554. }
  555. global.setStyleFixtures = function (html) {
  556. jasmine.getStyleFixtures().proxyCallTo_('set', arguments)
  557. }
  558. global.appendSetStyleFixtures = function (html) {
  559. jasmine.getStyleFixtures().proxyCallTo_('appendSet', arguments)
  560. }
  561. global.loadJSONFixtures = function () {
  562. return jasmine.getJSONFixtures().proxyCallTo_('load', arguments)
  563. }
  564. global.getJSONFixture = function (url) {
  565. return jasmine.getJSONFixtures().proxyCallTo_('read', arguments)[url]
  566. }
  567. }(jasmine, window);