This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
/
Copy pathmatchers.js
467 lines (408 loc) · 13.6 KB
/
matchers.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
'use strict';
beforeEach(function() {
function cssMatcher(presentClasses, absentClasses) {
return function() {
return {
compare: function(actual) {
var element = angular.element(actual);
var present = true;
var absent = false;
angular.forEach(presentClasses.split(' '), function(className) {
present = present && element.hasClass(className);
});
angular.forEach(absentClasses.split(' '), function(className) {
absent = absent || element.hasClass(className);
});
var message = function() {
return 'Expected to have ' + presentClasses +
(absentClasses ? (' and not have ' + absentClasses + '') : '') +
' but had ' + element[0].className + '.';
};
return {
pass: present && !absent,
message: message
};
}
};
};
}
function DOMTester(a, b) {
if (a && b && a.nodeType > 0 && b.nodeType > 0) {
return a === b;
}
}
function isNgElementHidden(element) {
// we need to check element.getAttribute for SVG nodes
var hidden = true;
forEach(angular.element(element), function(element) {
if ((' ' + (element.getAttribute('class') || '') + ' ').indexOf(' ng-hide ') === -1) {
hidden = false;
}
});
return hidden;
}
function MinErrMatcher(isNot, namespace, code, content, wording) {
var codeRegex = new RegExp('^' + escapeRegexp('[' + namespace + ':' + code + ']'));
var contentRegex = angular.isUndefined(content) || jasmine.isA_('RegExp', content) ?
content : new RegExp(escapeRegexp(content));
this.test = test;
function escapeRegexp(str) {
// This function escapes all special regex characters.
// We use it to create matching regex from arbitrary strings.
// http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
}
function test(exception) {
var exceptionMessage = (exception && exception.message) || exception || '';
var codeMatches = codeRegex.test(exceptionMessage);
var contentMatches = angular.isUndefined(contentRegex) || contentRegex.test(exceptionMessage);
var matches = codeMatches && contentMatches;
return {
pass: isNot ? !matches : matches,
message: message
};
function message() {
return 'Expected ' + wording.inputType + (isNot ? ' not' : '') + ' to ' +
wording.expectedAction + ' ' + namespace + 'MinErr(\'' + code + '\')' +
(contentRegex ? ' matching ' + contentRegex.toString() : '') +
(!exception ? '.' : ', but it ' + wording.actualAction + ': ' + exceptionMessage);
}
}
}
jasmine.addMatchers({
toBeEmpty: cssMatcher('ng-empty', 'ng-not-empty'),
toBeNotEmpty: cssMatcher('ng-not-empty', 'ng-empty'),
toBeInvalid: cssMatcher('ng-invalid', 'ng-valid'),
toBeValid: cssMatcher('ng-valid', 'ng-invalid'),
toBeDirty: cssMatcher('ng-dirty', 'ng-pristine'),
toBePristine: cssMatcher('ng-pristine', 'ng-dirty'),
toBeUntouched: cssMatcher('ng-untouched', 'ng-touched'),
toBeTouched: cssMatcher('ng-touched', 'ng-untouched'),
toBeAPromise: function() {
return {
compare: generateCompare(false),
negativeCompare: generateCompare(true)
};
function generateCompare(isNot) {
return function(actual) {
var message = valueFn(
'Expected object ' + (isNot ? 'not ' : '') + 'to be a promise');
return { pass: isPromiseLike(actual), message: message };
};
}
},
toBeShown: function() {
return {
compare: generateCompare(false),
negativeCompare: generateCompare(true)
};
function generateCompare(isNot) {
return function(actual) {
var message = valueFn('Expected element ' + (isNot ? '' : 'not ') + 'to have \'ng-hide\' class');
var pass = !isNgElementHidden(actual);
if (isNot) {
pass = !pass;
}
return { pass: pass, message: message };
};
}
},
toBeHidden: function() {
return {
compare: generateCompare(false),
negativeCompare: generateCompare(true)
};
function generateCompare(isNot) {
return function(actual) {
var message = valueFn('Expected element ' + (isNot ? 'not ' : '') + 'to have \'ng-hide\' class');
var pass = isNgElementHidden(actual);
if (isNot) {
pass = !pass;
}
return { pass: pass, message: message };
};
}
},
toEqual: function(util) {
return {
compare: function(actual, expected) {
if (actual && actual.$$log) {
actual = (typeof expected === 'string')
? actual.toString()
: actual.toArray();
}
return {
pass: util.equals(actual, expected, [DOMTester])
};
}
};
},
toEqualOneOf: function(util) {
return {
compare: function(actual) {
var expectedArgs = Array.prototype.slice.call(arguments, 1);
return {
pass: expectedArgs.some(function(expected) {
return util.equals(actual, expected, [DOMTester]);
})
};
}
};
},
toEqualData: function() {
return {
compare: function(actual, expected) {
return { pass: angular.equals(actual, expected) };
}
};
},
toHaveBeenCalledOnce: function() {
return {
compare: function(actual) {
if (arguments.length > 1) {
throw new Error('`toHaveBeenCalledOnce` does not take arguments, ' +
'use `toHaveBeenCalledOnceWith`');
}
if (!jasmine.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + jasmine.pp(actual) + '.');
}
var count = actual.calls.count();
var pass = count === 1;
var message = function() {
var msg = 'Expected spy ' + actual.and.identity() + (pass ? ' not ' : ' ') +
'to have been called once, but ';
switch (count) {
case 0:
msg += 'it was never called.';
break;
case 1:
msg += 'it was called once.';
break;
default:
msg += 'it was called ' + count + ' times.';
break;
}
return msg;
};
return {
pass: pass,
message: message
};
}
};
},
toHaveBeenCalledOnceWith: function(util, customEqualityTesters) {
return {
compare: generateCompare(false),
negativeCompare: generateCompare(true)
};
function generateCompare(isNot) {
return function(actual) {
if (!jasmine.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + jasmine.pp(actual) + '.');
}
var expectedArgs = Array.prototype.slice.call(arguments, 1);
var actualCount = actual.calls.count();
var actualArgs = actualCount && actual.calls.argsFor(0);
var pass = (actualCount === 1) && util.equals(actualArgs, expectedArgs);
if (isNot) pass = !pass;
var message = function() {
var msg = 'Expected spy ' + actual.and.identity() + (isNot ? ' not ' : ' ') +
'to have been called once with ' + jasmine.pp(expectedArgs) + ', but ';
if (isNot) {
msg += 'it was.';
} else {
switch (actualCount) {
case 0:
msg += 'it was never called.';
break;
case 1:
msg += 'it was called with ' + jasmine.pp(actualArgs) + '.';
break;
default:
msg += 'it was called ' + actualCount + ' times.';
break;
}
}
return msg;
};
return {
pass: pass,
message: message
};
};
}
},
toBeOneOf: function() {
return {
compare: function(actual) {
var expectedArgs = Array.prototype.slice.call(arguments, 1);
return { pass: expectedArgs.indexOf(actual) !== -1 };
}
};
},
toHaveClass: function() {
return {
compare: generateCompare(false),
negativeCompare: generateCompare(true)
};
function hasClass(element, selector) {
if (!element.getAttribute) return false;
return ((' ' + (element.getAttribute('class') || '') + ' ').replace(/[\n\t]/g, ' ').
indexOf(' ' + selector + ' ') > -1);
}
function generateCompare(isNot) {
return function(actual, clazz) {
var message = function() {
return 'Expected \'' + angular.mock.dump(actual) + '\'' + (isNot ? ' not ' : '') + ' to have class \'' + clazz + '\'.';
};
var classes = clazz.trim().split(/\s+/);
for (var i = 0; i < classes.length; ++i) {
if (!hasClass(actual[0], classes[i])) {
return { pass: isNot };
}
}
return { pass: !isNot };
};
}
},
toEqualMinErr: function() {
return {
compare: generateCompare(false),
negativeCompare: generateCompare(true)
};
function generateCompare(isNot) {
return function(actual, namespace, code, content) {
var matcher = new MinErrMatcher(isNot, namespace, code, content, {
inputType: 'error',
expectedAction: 'equal',
actualAction: 'was'
});
return matcher.test(actual);
};
}
},
toThrowMinErr: function() {
return {
compare: generateCompare(false),
negativeCompare: generateCompare(true)
};
function generateCompare(isNot) {
return function(actual, namespace, code, content) {
var exception;
if (!angular.isFunction(actual)) {
throw new Error('Actual is not a function');
}
try {
actual();
} catch (e) {
exception = e;
}
var matcher = new MinErrMatcher(isNot, namespace, code, content, {
inputType: 'function',
expectedAction: 'throw',
actualAction: 'threw'
});
return matcher.test(exception);
};
}
},
toBeMarkedAsSelected: function() {
// Selected is special because the element property and attribute reflect each other's state.
// Support: IE 9 only
// IE9 will wrongly report hasAttribute('selected') === true when the property is
// undefined or null, and the dev tools show that no attribute is set
return {
compare: function(actual) {
var errors = [];
if (actual.selected === null || typeof actual.selected === 'undefined' || actual.selected === false) {
errors.push('Expected option property "selected" to be truthy');
}
// Support: IE 9 only
if (msie !== 9 && actual.hasAttribute('selected') === false) {
errors.push('Expected option to have attribute "selected"');
}
var result = {
pass: errors.length === 0,
message: errors.join('\n')
};
return result;
},
negativeCompare: function(actual) {
var errors = [];
if (actual.selected) {
errors.push('Expected option property "selected" to be falsy');
}
// Support: IE 9 only
if (msie !== 9 && actual.hasAttribute('selected')) {
errors.push('Expected option not to have attribute "selected"');
}
var result = {
pass: errors.length === 0,
message: errors.join('\n')
};
return result;
}
};
}
});
});
/**
* Create jasmine.Spy on given method, but ignore calls without arguments
* This is helpful when need to spy only setter methods and ignore getters
*/
function spyOnlyCallsWithArgs(obj, method) {
var originalFn = obj[method];
var spy = spyOn(obj, method);
obj[method] = function() {
if (arguments.length) return spy.apply(this, arguments);
return originalFn.apply(this);
};
return spy;
}
// Minimal implementation to mock what was removed from Jasmine 1.x
function createAsync(doneFn) {
function Job() {
this.next = [];
}
Job.prototype.done = function() {
return this.runs(doneFn);
};
Job.prototype.runs = function(fn) {
var newJob = new Job();
this.next.push(function() {
fn();
newJob.start();
});
return newJob;
};
Job.prototype.waitsFor = function(fn, error, timeout) {
var newJob = new Job();
timeout = timeout || 5000;
this.next.push(function() {
var counter = 0,
intervalId = window.setInterval(function() {
if (fn()) {
window.clearInterval(intervalId);
newJob.start();
}
counter += 5;
if (counter > timeout) {
window.clearInterval(intervalId);
throw new Error(error);
}
}, 5);
});
return newJob;
};
Job.prototype.waits = function(timeout) {
return this.waitsFor(function() { return true; }, undefined, timeout);
};
Job.prototype.start = function() {
var i;
for (i = 0; i < this.next.length; i += 1) {
this.next[i]();
}
};
return new Job();
}