forked from angular/material
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-utils.js
37 lines (35 loc) · 1.03 KB
/
test-utils.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
var TestUtil = {
/**
* Creates a jQuery event for unit testing such that an event name and optional keyCode can be passed in.
*
*/
triggerEvent: function (element, eventName, eventData) {
eventData = eventData || {};
var e = $.extend({}, $.Event(eventName), eventData);
if(eventData.keyCode){
e.which = eventData.keyCode;
}
element.trigger(e);
},
/**
* Mocks angular.element#focus for the duration of the test
* @example
* it('some focus test', inject(function($document) {
* TestUtil.mockFocus(this); // 'this' is the test instance
* doSomething();
* expect($document.activeElement).toBe(someElement[0]);
* }));
*/
mockElementFocus: function(test) {
var focus = angular.element.prototype.focus;
inject(function($document) {
angular.element.prototype.focus = function() {
$document.activeElement = this[0];
};
});
// Un-mock focus after the test is done
test.after(function() {
angular.element.prototype.focus = focus;
});
}
};