forked from urish/angular-placeholder-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
181 lines (159 loc) · 6.28 KB
/
tests.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
/* License: MIT.
* Copyright (C) 2013, Uri Shaked.
*/
'use strict';
describe('Directive: placeholder', function () {
var originalPlaceholderBrowserSupported = jQuery.placeholder.browser_supported;
var originalPlaceholderShim = jQuery.fn._placeholder_shim;
var element;
var $timeout;
beforeEach(module(function($provide) {
element = null;
spyOn(jQuery.fn, '_placeholder_shim').andCallThrough();
}));
beforeEach(module('placeholderShim'));
beforeEach(inject(function ($injector) {
$timeout = $injector.get('$timeout');
}));
afterEach(function() {
jQuery.placeholder.browser_supported = originalPlaceholderBrowserSupported;
jQuery.fn._placeholder_shim = originalPlaceholderShim;
if (element) {
element.remove();
}
});
describe('in browsers which natively support placeholders', function() {
beforeEach(function() {
jQuery.placeholder.browser_supported = function() {return true};
});
it('should not call the placeholder-shim plugin', inject(function ($rootScope, $compile) {
element = angular.element('<input placeholder="foobar" />');
angular.element('body').append(element);
element = $compile(element)($rootScope);
expect(jQuery.fn._placeholder_shim).not.toHaveBeenCalled();
}));
});
describe('in browsers with no placeholders support', function(){
beforeEach(function() {
jQuery.placeholder.browser_supported = function() {return false};
});
var $rootScope, $compile;
beforeEach(inject(function($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
}));
it('should call the placeholder-shim plugin, but only after a timeout', function () {
element = angular.element('<input placeholder="foobar" />');
angular.element('body').append(element);
element = $compile(element)($rootScope);
expect(jQuery.fn._placeholder_shim).not.toHaveBeenCalled();
$timeout.flush();
expect(jQuery.fn._placeholder_shim).toHaveBeenCalled();
});
it('should not call the placeholder-shim plugin until the element becomes visible', function() {
element = angular.element('<input placeholder="foobar" />');
element = $compile(element)($rootScope);
$rootScope.$digest();
$timeout.verifyNoPendingTasks();
expect(jQuery.fn._placeholder_shim).not.toHaveBeenCalled();
angular.element('body').append(element);
$rootScope.$digest();
$timeout.flush();
expect(jQuery.fn._placeholder_shim).toHaveBeenCalled();
});
it('should create an overlay for the placeholder', function () {
var element = angular.element('<input placeholder="foobar" />');
angular.element('body').append(element);
element = $compile(element)($rootScope);
$rootScope.$digest();
$timeout.flush();
expect(element.data('placeholder')).toBeDefined();
});
it('should call the shim when the element first becomes visible', function() {
var element = angular.element('<input placeholder="foobar" />');
element = $compile(element)($rootScope);
$timeout.verifyNoPendingTasks();
expect(jQuery.fn._placeholder_shim).not.toHaveBeenCalled();
angular.element('body').append(element);
$rootScope.$digest();
$timeout.flush();
expect(jQuery.fn._placeholder_shim).toHaveBeenCalled();
});
it('should hide the placeholder when the input gets a value', function () {
$rootScope.someValue = '';
var element = angular.element('<input placeholder="foobar" ng-model="someValue" />');
angular.element('body').append(element);
element = $compile(element)($rootScope);
$rootScope.$digest();
$timeout.flush();
var placeholder = element.data('placeholder');
expect(placeholder.is(':visible')).toBeTruthy();
$rootScope.someValue = 'something';
$rootScope.$digest();
expect(placeholder.is(':visible')).toBeFalsy();
});
it('should show the placeholder when the input is empty', function () {
$rootScope.someValue = 'test';
var element = angular.element('<input placeholder="foobar" ng-model="someValue" />');
angular.element('body').append(element);
element = $compile(element)($rootScope);
$rootScope.$digest();
$timeout.flush();
var placeholder = element.data('placeholder');
expect(placeholder.is(':visible')).toBeFalsy();
$rootScope.someValue = '';
$rootScope.$digest();
expect(placeholder.is(':visible')).toBeTruthy();
});
it('should hide the placeholder when the input has focus', function () {
var element = angular.element('<input placeholder="foobar" />');
angular.element('body').append(element);
element = $compile(element)($rootScope);
$timeout.flush();
element.focus();
var placeholder = element.data('placeholder');
expect(placeholder.is(':visible')).toBeFalsy();
});
it('should keep the element hidden after a digest cycle if it has focus', function () {
var element = angular.element('<input placeholder="foobar" />');
angular.element('body').append(element);
element = $compile(element)($rootScope);
$rootScope.$digest();
$timeout.flush();
element.focus();
$rootScope.$digest();
var placeholder = element.data('placeholder');
expect(placeholder.is(':visible')).toBeFalsy();
});
it('should support angular expressions in the placeholder attribute', function() {
$rootScope.myVar = 7;
var element = angular.element('<input placeholder="{{myVar + 5}}" />');
angular.element('body').append(element);
element = $compile(element)($rootScope);
$rootScope.$digest();
$timeout.flush();
expect(element.data('placeholder').text()).toBe('12');
$rootScope.myVar = 37;
$rootScope.$digest();
expect(element.data('placeholder').text()).toBe('42');
});
it('should cancel the overlay creation timeout on scope destroy', function () {
var scope = $rootScope.$new();
var element = angular.element('<input placeholder="foobar" />');
angular.element('body').append(element);
element = $compile(element)(scope);
$rootScope.$digest();
scope.$destroy();
$timeout.verifyNoPendingTasks();
expect(element.data('placeholder')).toBeUndefined();
});
it('should gracefully handle the situation when the scope was destroyed before the element was added to DOM', function () {
var scope = $rootScope.$new();
var element = angular.element('<input placeholder="foobar" />');
element = $compile(element)(scope);
$rootScope.$digest();
scope.$destroy();
$timeout.verifyNoPendingTasks();
});
});
});