-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-auto-focus.spec.js
53 lines (43 loc) · 1.56 KB
/
angular-auto-focus.spec.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
/*eslint-env jasmine */
describe('angular auto focus', function(){
'use strict';
var $rootScope,
$compile,
$document,
$timeout;
beforeEach(module('angular-autofocus'));
beforeEach(inject(function(_$rootScope_, _$compile_, _$timeout_, _$document_){
$rootScope = _$rootScope_;
$compile = _$compile_;
$timeout = _$timeout_;
$document = _$document_;
}));
it('should focus on an input when the expression is true', function(){
var elm = angular.element('<input type="text" autofocus="{{true}}"/><');
spyOn(elm[0], 'focus');
$compile(elm)($rootScope);
$rootScope.$digest();
$timeout.flush();
expect(elm[0].focus).toHaveBeenCalled();
});
it('should find a focusable element if the parent is not focusable', function(){
var elm = angular.element('<div autofocus="{{true}}"><input type="text"/></div>');
spyOn(elm.find('input')[0], 'focus');
$compile(elm)($rootScope);
$rootScope.$digest();
$timeout.flush();
expect(elm.find('input')[0].focus).toHaveBeenCalled();
});
it('should blur the element if the expression changes to false', function(){
$rootScope.autofocusExp = true;
var elm = angular.element('<input type="text" autofocus="{{autofocusExp}}"/>');
$compile(elm)($rootScope);
$rootScope.$digest();
$timeout.flush();
spyOn(elm[0], 'blur');
$rootScope.autofocusExp = false;
$rootScope.$digest();
$timeout.flush();
expect(elm[0].blur).toHaveBeenCalled();
})
});