-
Notifications
You must be signed in to change notification settings - Fork 1
/
expect-dom.js
233 lines (192 loc) · 7.13 KB
/
expect-dom.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
(function() {
function DOM(obj) {
if (!(this instanceof DOM)) return new DOM(obj);
if (obj && obj.tagName) {
this.element = obj;
} else if (obj && obj.jquery) {
this.element = obj.get(0);
} else {
throw "Do you provide a DOM or jQuery object?";
}
}
DOM.prototype.hasClass = function(className) {
return (' ' + this.element.className + ' ').indexOf(' ' + className + ' ') !== -1;
};
DOM.prototype.getAttr = function (attrName) {
var attr = this.element.attributes[attrName];
return attr && attr.value;
};
DOM.prototype.getId = function () {
return this.element.id;
};
DOM.prototype.getHtml = function () {
return this.element.innerHTML;
};
DOM.prototype.getText = function () {
return this.element.textContent || this.element.innerText;
};
DOM.prototype.getValue = function () {
return this.element.value;
};
DOM.prototype.isVisible = function () {
return this.element.offsetWidth || this.element.offsetHeight;
};
DOM.prototype.isSelected = function () {
return this.element.selected;
};
DOM.prototype.isChecked = function () {
return this.element.checked;
};
DOM.prototype.isEmpty = function () {
return !this.element.firstChild;
};
DOM.prototype.doesExist = function () {
return this.element;
};
DOM.prototype.doesMatch = function (selector) {
var matchSelector = this.element.matches ||
this.element.webkitMatchesSelector ||
this.element.mozMatchesSelector ||
this.element.oMatchesSelector ||
this.element.msMatchesSelector ||
matchesPolyfill;
return matchSelector.call(this.element, selector);
};
DOM.prototype.find = function (selector) {
return this.element.querySelectorAll(selector);
};
DOM.prototype.isDisabled = function () {
return this.element.disabled;
};
var assertions = {
cssclass: function(className) {
assert(this, DOM(this.obj).hasClass(className),
"Expected element to have class " + className,
"Expected element to not have class " + className);
},
attr: function(attrName, expectedValue) {
if (expectedValue)
assert(this, DOM(this.obj).getAttr(attrName) === expectedValue,
"Expected element to have attribute '" + attrName + "' with value '" + expectedValue + "'",
"Expected element to have attribute '" + attrName + "' with value other than '" + expectedValue + "'");
else
assert(this, DOM(this.obj).getAttr(attrName),
"Expected element to have attribute " + attrName,
"Expected element to not have attribute " + attrName);
},
id: function(id) {
assert(this, DOM(this.obj).getId() === id,
"Expected element to have id '" + id + "'",
"Expected element to not have id '" + id + "'");
},
html: function(html) {
var actualHtml = DOM(this.obj).getHtml();
assert(this, actualHtml === normalizeHtml(html),
"Expected element to have html '" + html + "' but had '" + actualHtml + "'",
"Expected element to not have html '" + actualHtml + "'");
},
text: function(text) {
var actualText = DOM(this.obj).getText();
if (text instanceof RegExp)
assert(this, text.test(actualText),
"Expected element text '" + actualText + "' to match regex '" + text.source + "'",
"Expected element text '" + actualText + "' to not match regex '" + text.source + "'");
else
assert(this, actualText === text,
"Expected element to have text '" + text + "' but had '" + actualText + "'",
"Expected element to not have text '" + actualText + "'");
},
value: function(value) {
var actualValue = DOM(this.obj).getValue();
assert(this, actualValue === value,
"Expected element to have value '" + value + "' but had '" + actualValue + "'",
"Expected element to not have value '" + actualValue + "'");
},
data: function(key, expectedValue) {
if (!this.obj.jquery) {
// object is pure DOM, throw not support exception
throw "assert have.data on pure DOM is not supported.";
}
if (expectedValue)
assert(this, this.obj.data(key) === expectedValue,
"Expected element to have data '" + key + "' with value '" + expectedValue + "'",
"Expected element to have data '" + key + "' with value other than '" + expectedValue + "'");
else
assert(this, this.obj.data(key) !== undefined,
"Expected element to have data " + key,
"Expected element to not have data " + key);
},
visible: function() {
assert(this, DOM(this.obj).isVisible(),
"Expected element to be visible",
"Expected element to not be visible");
},
hidden: function() {
assert(this, !DOM(this.obj).isVisible(),
"Expected element to be hidden",
"Expected element to not be hidden");
},
selected: function() {
assert(this, DOM(this.obj).isSelected(),
"Expected element to be selected",
"Expected element to not be selected");
},
checked: function() {
assert(this, DOM(this.obj).isChecked(),
"Expected element to be checked",
"Expected element to not be checked");
},
empty: function() {
assert(this, DOM(this.obj).isEmpty(),
"Expected element to be empty",
"Expected element to not be empty");
},
exist: function() {
assert(this, DOM(this.obj).doesExist(),
"Expected element to exist",
"Expected element to not exist");
},
matchSelector: function(selector) {
assert(this, DOM(this.obj).doesMatch(selector),
"Expected element to match selector '" + selector + "'",
"Expected element to not match selector '" + selector + "'");
},
containChild: function(selector) {
assert(this, DOM(this.obj).find(selector).length,
"Expected element to contain child '" + selector + "'",
"Expected element to not contain child '" + selector + "'");
},
disabled: function() {
assert(this, DOM(this.obj).isDisabled(),
"Expected element to be disabled",
"Expected element to not be disabled");
},
};
function normalizeHtml(html) {
var element = document.createElement('div');
element.innerHTML = html;
return element.innerHTML;
}
function assert(self, truth, msg, error) {
self.assert(truth,
function () { return msg; },
function () { return error; });
}
function matchesPolyfill(selector) {
var element = this;
var matches = (element.document || element.ownerDocument).querySelectorAll(selector);
var i = 0;
while (matches[i] && matches[i] !== element) {
i++;
}
return matches[i] ? true : false;
}
if (typeof expect === "undefined" || typeof expect.Assertion !== "function") {
throw "`expect` is not found. Do you forget to import expect.js?";
}
for (var assertion in assertions) {
if (assertions.hasOwnProperty(assertion)) {
expect.Assertion.prototype[assertion] = assertions[assertion];
}
}
})();