jasmine-dom-matchers.js
4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*jsl:declare jasmine*/
/*jsl:declare Sizzle*/
/*jsl:declare Prototype*/
/*jsl:declare jQuery*/
jasmine.DOM = {};
jasmine.DOM.browserTagCaseIndependentHtml = function(html)
{
var div= document.createElement('div');
div.innerHTML= html;
return div.innerHTML;
}
jasmine.DOM.elementToString = function(element)
{
var div= document.createElement('div');
div.appendChild(element.cloneNode(true));
return div.innerHTML;
}
jasmine.DOM.trim= function(string)
{
var str= string.replace(/^\s+/, '');
for (var i = str.length - 1; i > 0; --i)
if (/\S/.test(str.charAt(i)))
{
str = str.substring(0, i + 1);
break;
}
return str;
}
jasmine.DOM.slice= function(arrayLike, startIndex)
{
return [].slice.call(arrayLike, startIndex||0);
}
jasmine.DOM.uniqueId= 1;
jasmine.DOM.assignId= function(element)
{
return element.id || (element.id=('jasmine_id_' + jasmine.DOM.uniqueId++));
};
/**
jasmine.DOM.queryAll(selector[, scope]) -> array
*/
jasmine.DOM.queryAll= (function(){
if ('undefined'!==typeof(Sizzle))
return Sizzle;
if ('undefined'!==typeof(Prototype))
return function(selector, node)
{
return Element.getElementsBySelector(node||document, selector);
};
if ('undefined'!==typeof(jQuery))
return function(selector, node)
{
var result= jQuery(selector, node);
var nodes= [];
var len= result.length;
for (var i=0; i<len; ++i)
nodes.push(result[i]);
return nodes;
};
if (document.querySelectorAll)
return function(selector, node)
{
if (!node)
node= document;
else if (node!==document)
selector = ['#', jasmine.DOM.assignId(node), ' ', selector].join('');
return jasmine.DOM.slice(node.querySelectorAll(selector));
};
throw new Error("Can't determine selector engine...");
})();
jasmine.DOM.matchers = {};
(function(){
var matchers = {
toHaveClass: function(className)
{
var classes= jasmine.DOM.trim(this.actual.className).split(" ");
return -1!==classes.indexOf(className);
},
toBeVisible: function()
{
return (this.actual.offsetWidth!==0 || this.actual.offsetHeight!==0);
},
toBeHidden: function()
{
return (0===this.actual.offsetWidth && 0===this.actual.offsetHeight);
},
toBeSelected: function()
{
return this.actual.selected;
},
toBeChecked: function()
{
return this.actual.checked;
},
toBeEmpty: function()
{
return !this.actual.firstChild;
},
toExist: function()
{
return !!this.actual;
},
toHaveAttr: function(attributeName, expectedAttributeValue)
{
if (!this.actual.hasAttribute(attributeName))
return false;
return comparePropertyValues(this.actual.getAttribute(attributeName), expectedAttributeValue);
},
toHaveId: function(id)
{
return this.actual.id===id;
},
toHaveHtml: function(html)
{
return this.actual.innerHTML === jasmine.DOM.browserTagCaseIndependentHtml(html);
},
toHaveText: function(text)
{
return (this.actual.textContent||this.actual.innerText) === text;
},
toHaveValue: function(value)
{
return this.actual.value === value;
},
toMatchSelector: function(selector)
{
// This isn't efficient
var nodes= jasmine.DOM.queryAll(selector);
return -1!==nodes.indexOf(this.actual);
},
toContain: function(selector)
{
var nodes= jasmine.DOM.queryAll(selector, this.actual);
return nodes.length > 0;
}
};
function comparePropertyValues(actualValue, expectedValue)
{
if (void(0) === expectedValue)
return void(0) !== actualValue;
return actualValue == expectedValue;
}
function bindMatcher(methodName)
{
var originalMatcher = jasmine.Matchers.prototype[methodName];
jasmine.DOM.matchers[methodName] = function()
{
// If the actual value is a DOM node...
if (this.actual && this.actual.nodeType)
{
var result = matchers[methodName].apply(this, arguments);
this.actual = jasmine.DOM.elementToString(this.actual);
return result;
}
if (originalMatcher)
return originalMatcher.apply(this, arguments);
return false;
}
}
for (var methodName in matchers)
bindMatcher(methodName);
})();
beforeEach(function() {
this.addMatchers(jasmine.DOM.matchers);
});
afterEach(function() {
jasmine.getFixtures().cleanUp();
});