2015-09-08 04:13:41 +03:00
|
|
|
'use strict'
|
2013-04-18 03:29:13 +04:00
|
|
|
|
2015-09-08 04:11:06 +03:00
|
|
|
jasmine.JQuery = function() {}
|
2012-08-27 01:29:46 +04:00
|
|
|
|
|
|
|
jasmine.JQuery.browserTagCaseIndependentHtml = function(html) {
|
2015-09-08 04:11:06 +03:00
|
|
|
var div = document.createElement('div')
|
2015-09-04 20:07:29 +03:00
|
|
|
div.innerHTML = html
|
|
|
|
return div.innerHTML
|
2015-09-08 04:11:06 +03:00
|
|
|
}
|
2012-08-27 01:29:46 +04:00
|
|
|
|
|
|
|
jasmine.JQuery.elementToString = function(element) {
|
2015-09-04 20:07:29 +03:00
|
|
|
if (element instanceof HTMLElement) {
|
|
|
|
return element.outerHTML
|
|
|
|
} else {
|
|
|
|
return element.html()
|
|
|
|
}
|
2015-09-08 04:11:06 +03:00
|
|
|
}
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:11:06 +03:00
|
|
|
jasmine.JQuery.matchersClass = {}
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
var jQueryMatchers = {
|
|
|
|
toHaveClass: function(className) {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return this.actual.classList.contains(className)
|
|
|
|
} else {
|
|
|
|
return this.actual.hasClass(className)
|
|
|
|
}
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toBeVisible: function() {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return this.actual.offsetWidth !== 0 || this.actual.offsetHeight !== 0
|
|
|
|
} else {
|
|
|
|
return this.actual.is(':visible')
|
|
|
|
}
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toBeHidden: function() {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return this.actual.offsetWidth === 0 && this.actual.offsetHeight === 0
|
|
|
|
} else {
|
|
|
|
return this.actual.is(':hidden')
|
|
|
|
}
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toBeSelected: function() {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return this.actual.selected
|
|
|
|
} else {
|
|
|
|
return this.actual.is(':selected')
|
|
|
|
}
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toBeChecked: function() {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return this.actual.checked
|
|
|
|
} else {
|
|
|
|
return this.actual.is(':checked')
|
|
|
|
}
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toBeEmpty: function() {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return this.actual.innerHTML === ''
|
|
|
|
} else {
|
|
|
|
return this.actual.is(':empty')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
toExist: function() {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return true
|
|
|
|
} else if (this.actual) {
|
|
|
|
return this.actual.size() > 0
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
toHaveAttr: function(attributeName, expectedAttributeValue) {
|
|
|
|
var actualAttributeValue
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
actualAttributeValue = this.actual.getAttribute(attributeName)
|
|
|
|
} else {
|
|
|
|
actualAttributeValue = this.actual.attr(attributeName)
|
|
|
|
}
|
2015-09-04 20:07:29 +03:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
return hasProperty(actualAttributeValue, expectedAttributeValue)
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toHaveId: function(id) {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return this.actual.getAttribute('id') == id
|
|
|
|
} else {
|
|
|
|
return this.actual.attr('id') == id
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
toHaveHtml: function(html) {
|
|
|
|
var actualHTML
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
actualHTML = this.actual.innerHTML
|
|
|
|
} else {
|
|
|
|
actualHTML = this.actual.html()
|
|
|
|
}
|
2015-09-04 20:07:29 +03:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
return actualHTML == jasmine.JQuery.browserTagCaseIndependentHtml(html)
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toHaveText: function(text) {
|
|
|
|
var actualText
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
actualText = this.actual.textContent
|
|
|
|
} else {
|
|
|
|
actualText = this.actual.text()
|
|
|
|
}
|
2015-09-04 20:07:29 +03:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
if (text && typeof text.test === 'function') {
|
|
|
|
return text.test(actualText)
|
|
|
|
} else {
|
|
|
|
return actualText == text
|
|
|
|
}
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toHaveValue: function(value) {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return this.actual.value == value
|
|
|
|
} else {
|
|
|
|
return this.actual.val() == value
|
|
|
|
}
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toHaveData: function(key, expectedValue) {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
2015-09-10 03:49:09 +03:00
|
|
|
var camelCaseKey
|
|
|
|
for (var part of key.split('-')) {
|
|
|
|
if (camelCaseKey) {
|
|
|
|
camelCaseKey += part[0].toUpperCase() + part.substring(1)
|
|
|
|
} else {
|
|
|
|
camelCaseKey = part
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasProperty(this.actual.dataset[camelCaseKey], expectedValue)
|
2015-09-08 04:13:41 +03:00
|
|
|
} else {
|
|
|
|
return hasProperty(this.actual.data(key), expectedValue)
|
|
|
|
}
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toMatchSelector: function(selector) {
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return this.actual.matches(selector)
|
|
|
|
} else {
|
|
|
|
return this.actual.is(selector)
|
|
|
|
}
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-10 04:03:13 +03:00
|
|
|
toContain: function(contained) {
|
2015-09-08 04:13:41 +03:00
|
|
|
if (this.actual instanceof HTMLElement) {
|
2015-09-10 04:03:13 +03:00
|
|
|
if (typeof contained === 'string') {
|
|
|
|
return this.actual.querySelector(contained)
|
|
|
|
} else {
|
|
|
|
return this.actual.contains(contained)
|
|
|
|
}
|
2015-09-08 04:13:41 +03:00
|
|
|
} else {
|
2015-09-10 04:03:13 +03:00
|
|
|
return this.actual.find(contained).size() > 0
|
2015-09-08 04:13:41 +03:00
|
|
|
}
|
|
|
|
},
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
toBeDisabled: function(selector){
|
|
|
|
if (this.actual instanceof HTMLElement) {
|
|
|
|
return this.actual.disabled
|
|
|
|
} else {
|
|
|
|
return this.actual.is(':disabled')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// tests the existence of a specific event binding
|
|
|
|
toHandle: function(eventName) {
|
|
|
|
var events = this.actual.data("events")
|
|
|
|
return events && events[eventName].length > 0
|
|
|
|
},
|
|
|
|
|
|
|
|
// tests the existence of a specific event binding + handler
|
|
|
|
toHandleWith: function(eventName, eventHandler) {
|
|
|
|
var stack = this.actual.data("events")[eventName]
|
|
|
|
var i
|
|
|
|
for (i = 0; i < stack.length; i++) {
|
|
|
|
if (stack[i].handler == eventHandler) {
|
|
|
|
return true
|
2012-08-27 01:29:46 +04:00
|
|
|
}
|
|
|
|
}
|
2015-09-08 04:13:41 +03:00
|
|
|
return false
|
2015-09-08 04:11:06 +03:00
|
|
|
}
|
2015-09-08 04:13:41 +03:00
|
|
|
}
|
2015-09-04 20:07:29 +03:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
var hasProperty = function(actualValue, expectedValue) {
|
|
|
|
if (expectedValue === undefined) {
|
|
|
|
return actualValue !== undefined
|
2015-09-08 04:11:06 +03:00
|
|
|
}
|
2015-09-08 04:13:41 +03:00
|
|
|
return actualValue == expectedValue
|
|
|
|
}
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
var bindMatcher = function(methodName) {
|
|
|
|
var builtInMatcher = jasmine.Matchers.prototype[methodName]
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
jasmine.JQuery.matchersClass[methodName] = function() {
|
|
|
|
if (this.actual && this.actual.jquery || this.actual instanceof HTMLElement) {
|
|
|
|
var result = jQueryMatchers[methodName].apply(this, arguments)
|
|
|
|
this.actual = jasmine.JQuery.elementToString(this.actual)
|
|
|
|
return result
|
|
|
|
}
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
if (builtInMatcher) {
|
|
|
|
return builtInMatcher.apply(this, arguments)
|
2015-09-08 04:11:06 +03:00
|
|
|
}
|
2012-08-27 01:29:46 +04:00
|
|
|
|
2015-09-08 04:13:41 +03:00
|
|
|
return false
|
2012-08-27 01:29:46 +04:00
|
|
|
}
|
2015-09-08 04:13:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for(var methodName in jQueryMatchers) {
|
|
|
|
bindMatcher(methodName)
|
|
|
|
}
|
2012-08-27 01:29:46 +04:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2015-09-08 04:11:06 +03:00
|
|
|
this.addMatchers(jasmine.JQuery.matchersClass)
|
|
|
|
})
|