-
Notifications
You must be signed in to change notification settings - Fork 27
/
focusIf.spec.js
107 lines (93 loc) · 3.42 KB
/
focusIf.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
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
(function() {
'use strict';
describe('Directive: focus-if [A]', function() {
var $scope,
$compile,
$timeout,
$document,
tpl = '<input type="text" focus-if="{{watchProperty}}" focus-delay="{{delay}}" />',
element;
beforeEach(module('focus-if'));
beforeEach(function() {
jasmine.addMatchers({
toHaveFocus: function() {
return {
compare: function(dom) {
var focused;
if (!(dom instanceof Node)) {
dom = dom[0];
}
focused = dom == document.activeElement;
return {
pass: focused,
message: focused ? 'Expected ' + dom + ' not to have focus'
: 'Expected ' + dom + ' to have focus'
};
}
};
}
});
});
beforeEach(inject(function($rootScope, _$compile_, _$timeout_, _$document_) {
$scope = $rootScope.$new();
$compile = _$compile_;
$timeout = _$timeout_;
$document = _$document_;
}));
afterEach(function() {
element.remove();
});
describe('<input focus-if>', function() {
it('should have focus immediately', onLinkTest);
});
describe('<input focus-if focus-delay="500">', function() {
it('should have focus after 500ms', function() {
$scope.delay = 500;
onLinkTest();
});
});
function onLinkTest() {
compile();
flush();
expect(element).toHaveFocus();
}
function compile() {
element = $compile(tpl)($scope);
angular.element($document[0].body).append(element);
}
function flush() {
$timeout.flush(($scope.delay || 0) - 1);
expect(element).not.toHaveFocus();
$timeout.flush(1);
}
describe('<input focus-if="watchProperty">', function() {
beforeEach(watchBeforeEach);
it('should not have focus immediately when $scope.watchProperty is falsy', falsyWatchTest);
it('should have focus immediately when $scope.watchProperty is truthy', truthyWatchTest);
});
describe('<input focus-if="watchProperty" focus-delay="500">', function() {
beforeEach(function() {
$scope.delay = 500;
watchBeforeEach();
});
it('should not have focus after 500ms when $scope.focus-if is falsy', falsyWatchTest);
it('should have focus after 500ms when $scope.focus-if is truthy', truthyWatchTest);
});
function watchBeforeEach() {
$scope.watchProperty = 'focus';
$scope.focus = false;
compile();
}
function falsyWatchTest() {
$timeout.verifyNoPendingTasks();
expect(element).not.toHaveFocus();
}
function truthyWatchTest() {
falsyWatchTest();
$scope.focus = true;
$scope.$digest();
flush();
expect(element).toHaveFocus();
}
});
})();