-
Notifications
You must be signed in to change notification settings - Fork 69
/
chai-jquery.js
241 lines (218 loc) · 7.09 KB
/
chai-jquery.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
234
235
236
237
238
239
240
241
(function (chaiJquery) {
// Module systems magic dance.
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// NodeJS
module.exports = chaiJquery;
} else if (typeof define === "function" && define.amd) {
// AMD
define(['jquery'], function ($) {
return function (chai, utils) {
return chaiJquery(chai, utils, $);
};
});
} else {
// Other environment (usually <script> tag): plug in to global chai instance directly.
chai.use(function (chai, utils) {
return chaiJquery(chai, utils, jQuery);
});
}
}(function (chai, utils, $) {
var inspect = utils.inspect,
flag = utils.flag;
$ = $ || jQuery;
var setPrototypeOf = '__proto__' in Object ?
function (object, prototype) {
object.__proto__ = prototype;
} :
function (object, prototype) {
var excludeNames = /^(?:length|name|arguments|caller)$/;
function copyProperties(dst, src) {
Object.getOwnPropertyNames(src).forEach(function (name) {
if (!excludeNames.test(name)) {
Object.defineProperty(dst, name,
Object.getOwnPropertyDescriptor(src, name));
}
});
}
copyProperties(object, prototype);
copyProperties(object, Object.getPrototypeOf(prototype));
};
$.fn.inspect = function (depth) {
var el = $('<div />').append(this.clone());
if (depth !== undefined) {
var children = el.children();
while (depth-- > 0)
children = children.children();
children.html('...');
}
return el.html();
};
var props = {attr: 'attribute', css: 'CSS property', prop: 'property'};
for (var prop in props) {
(function (prop, description) {
chai.Assertion.addMethod(prop, function (name, val) {
var actual = flag(this, 'object')[prop](name);
if (!flag(this, 'negate') || undefined === val) {
this.assert(
undefined !== actual
, 'expected #{this} to have a #{exp} ' + description
, 'expected #{this} not to have a #{exp} ' + description
, name
);
}
if (undefined !== val) {
this.assert(
val === actual
, 'expected #{this} to have a ' + inspect(name) + ' ' + description + ' with the value #{exp}, but the value was #{act}'
, 'expected #{this} not to have a ' + inspect(name) + ' ' + description + ' with the value #{act}'
, val
, actual
);
}
flag(this, 'object', actual);
});
})(prop, props[prop]);
}
chai.Assertion.addMethod('data', function (name, val) {
// Work around a chai bug (https://github.com/logicalparadox/chai/issues/16)
if (flag(this, 'negate') && undefined !== val && undefined === flag(this, 'object').data(name)) {
return;
}
var assertion = new chai.Assertion(flag(this, 'object').data());
if (flag(this, 'negate'))
assertion = assertion.not;
return arguments.length === 1 ? assertion.property(name) : assertion.property(name, val);
});
chai.Assertion.addMethod('class', function (className) {
this.assert(
flag(this, 'object').hasClass(className)
, 'expected #{this} to have class #{exp}'
, 'expected #{this} not to have class #{exp}'
, className
);
});
chai.Assertion.addMethod('id', function (id) {
this.assert(
flag(this, 'object').attr('id') === id
, 'expected #{this} to have id #{exp}'
, 'expected #{this} not to have id #{exp}'
, id
);
});
chai.Assertion.addMethod('html', function (html) {
var actual = flag(this, 'object').html();
this.assert(
actual === html
, 'expected #{this} to have HTML #{exp}, but the HTML was #{act}'
, 'expected #{this} not to have HTML #{exp}'
, html
, actual
);
});
chai.Assertion.addMethod('text', function (text) {
var actual = flag(this, 'object').text();
this.assert(
actual === text
, 'expected #{this} to have text #{exp}, but the text was #{act}'
, 'expected #{this} not to have text #{exp}'
, text
, actual
);
});
chai.Assertion.addMethod('value', function (value) {
var actual = flag(this, 'object').val();
this.assert(
flag(this, 'object').val() === value
, 'expected #{this} to have value #{exp}, but the value was #{act}'
, 'expected #{this} not to have value #{exp}'
, value
, actual
);
});
chai.Assertion.addMethod('descendants', function (selector) {
this.assert(
flag(this, 'object').has(selector).length > 0
, 'expected #{this} to have #{exp}'
, 'expected #{this} not to have #{exp}'
, selector
);
});
$.each(['visible', 'hidden', 'selected', 'checked', 'enabled', 'disabled'], function (i, attr) {
chai.Assertion.addProperty(attr, function () {
this.assert(
flag(this, 'object').is(':' + attr)
, 'expected #{this} to be ' + attr
, 'expected #{this} not to be ' + attr);
});
});
chai.Assertion.overwriteProperty('exist', function (_super) {
return function () {
var obj = flag(this, 'object');
if (obj instanceof $) {
this.assert(
obj.length > 0
, 'expected ' + inspect(obj.selector) + ' to exist'
, 'expected ' + inspect(obj.selector) + ' not to exist');
} else {
_super.apply(this, arguments);
}
};
});
chai.Assertion.overwriteProperty('empty', function (_super) {
return function () {
var obj = flag(this, 'object');
if (obj instanceof $) {
this.assert(
obj.is(':empty')
, 'expected #{this} to be empty'
, 'expected #{this} not to be empty');
} else {
_super.apply(this, arguments);
}
};
});
chai.Assertion.overwriteMethod('match', function (_super) {
return function (selector) {
var obj = flag(this, 'object');
if (obj instanceof $) {
this.assert(
obj.is(selector)
, 'expected #{this} to match #{exp}'
, 'expected #{this} not to match #{exp}'
, selector
);
} else {
_super.apply(this, arguments);
}
}
});
chai.Assertion.overwriteChainableMethod('contain',
function (_super) {
return function (text) {
var obj = flag(this, 'object');
if (obj instanceof $) {
this.assert(
obj.is(':contains(\'' + text + '\')')
, 'expected #{this} to contain #{exp}'
, 'expected #{this} not to contain #{exp}'
, text);
} else {
_super.apply(this, arguments);
}
}
},
function(_super) {
return function() {
_super.call(this);
};
}
);
chai.Assertion.addMethod('focus', function () {
this.assert(
// Can't use `$().is(':focus')` because of certain webkit browsers
// see https://github.com/ariya/phantomjs/issues/10427
flag(this, 'object').get(0) === document.activeElement
, 'expected #{this} to have focus'
, 'expected #{this} not to have focus');
});
}));