This repository has been 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.5k
/
Copy pathngShowHideSpec.js
229 lines (189 loc) · 6.65 KB
/
ngShowHideSpec.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
'use strict';
describe('ngShow / ngHide', function() {
var $scope, $compile, element;
function expectVisibility(exprs, ngShowOrNgHide, shownOrHidden) {
element = $compile('<div></div>')($scope);
forEach(exprs, function(expr) {
var childElem = $compile('<div ' + ngShowOrNgHide + '="' + expr + '"></div>')($scope);
element.append(childElem);
$scope.$digest();
expect(childElem)[shownOrHidden === 'shown' ? 'toBeShown' : 'toBeHidden']();
});
}
beforeEach(inject(function($rootScope, _$compile_) {
$scope = $rootScope.$new();
$compile = _$compile_;
}));
afterEach(function() {
dealoc(element);
});
describe('ngShow', function() {
function expectShown() {
expectVisibility(arguments, 'ng-show', 'shown');
}
function expectHidden() {
expectVisibility(arguments, 'ng-show', 'hidden');
}
it('should show and hide an element', function() {
element = jqLite('<div ng-show="exp"></div>');
element = $compile(element)($scope);
$scope.$digest();
expect(element).toBeHidden();
$scope.exp = true;
$scope.$digest();
expect(element).toBeShown();
});
// https://github.com/angular/angular.js/issues/5414
it('should show if the expression is a function with a no arguments', function() {
element = jqLite('<div ng-show="exp"></div>');
element = $compile(element)($scope);
$scope.exp = function() {};
$scope.$digest();
expect(element).toBeShown();
});
it('should make hidden element visible', function() {
element = jqLite('<div class="ng-hide" ng-show="exp"></div>');
element = $compile(element)($scope);
expect(element).toBeHidden();
$scope.exp = true;
$scope.$digest();
expect(element).toBeShown();
});
it('should hide the element if condition is falsy', function() {
expectHidden('false', 'undefined', 'null', 'NaN', '\'\'', '0');
});
it('should show the element if condition is a non-empty string', function() {
expectShown('\'f\'', '\'0\'', '\'false\'', '\'no\'', '\'n\'', '\'[]\'');
});
it('should show the element if condition is an object', function() {
expectShown('[]', '{}');
});
});
describe('ngHide', function() {
function expectShown() {
expectVisibility(arguments, 'ng-hide', 'shown');
}
function expectHidden() {
expectVisibility(arguments, 'ng-hide', 'hidden');
}
it('should hide an element', function() {
element = jqLite('<div ng-hide="exp"></div>');
element = $compile(element)($scope);
expect(element).toBeShown();
$scope.exp = true;
$scope.$digest();
expect(element).toBeHidden();
});
it('should show the element if condition is falsy', function() {
expectShown('false', 'undefined', 'null', 'NaN', '\'\'', '0');
});
it('should hide the element if condition is a non-empty string', function() {
expectHidden('\'f\'', '\'0\'', '\'false\'', '\'no\'', '\'n\'', '\'[]\'');
});
it('should hide the element if condition is an object', function() {
expectHidden('[]', '{}');
});
});
});
describe('ngShow / ngHide animations', function() {
var body, element, $rootElement;
function html(content) {
body.append($rootElement);
$rootElement.html(content);
element = $rootElement.children().eq(0);
return element;
}
beforeEach(function() {
// we need to run animation on attached elements;
body = jqLite(window.document.body);
});
afterEach(function() {
dealoc(body);
dealoc(element);
body.removeAttr('ng-animation-running');
});
beforeEach(module('ngAnimateMock'));
beforeEach(module(function($animateProvider, $provide) {
return function(_$rootElement_) {
$rootElement = _$rootElement_;
};
}));
describe('ngShow', function() {
it('should fire off the $animate.show and $animate.hide animation', inject(function($compile, $rootScope, $animate) {
var item;
var $scope = $rootScope.$new();
$scope.on = true;
element = $compile(html(
'<div ng-show="on">data</div>'
))($scope);
$scope.$digest();
item = $animate.queue.shift();
expect(item.event).toBe('removeClass');
expect(item.element.text()).toBe('data');
expect(item.element).toBeShown();
$scope.on = false;
$scope.$digest();
item = $animate.queue.shift();
expect(item.event).toBe('addClass');
expect(item.element.text()).toBe('data');
expect(item.element).toBeHidden();
}));
it('should apply the temporary `.ng-hide-animate` class to the element',
inject(function($compile, $rootScope, $animate) {
var item;
var $scope = $rootScope.$new();
$scope.on = false;
element = $compile(html(
'<div class="show-hide" ng-show="on">data</div>'
))($scope);
$scope.$digest();
item = $animate.queue.shift();
expect(item.event).toEqual('addClass');
expect(item.options.tempClasses).toEqual('ng-hide-animate');
$scope.on = true;
$scope.$digest();
item = $animate.queue.shift();
expect(item.event).toEqual('removeClass');
expect(item.options.tempClasses).toEqual('ng-hide-animate');
}));
});
describe('ngHide', function() {
it('should fire off the $animate.show and $animate.hide animation', inject(function($compile, $rootScope, $animate) {
var item;
var $scope = $rootScope.$new();
$scope.off = true;
element = $compile(html(
'<div ng-hide="off">datum</div>'
))($scope);
$scope.$digest();
item = $animate.queue.shift();
expect(item.event).toBe('addClass');
expect(item.element.text()).toBe('datum');
expect(item.element).toBeHidden();
$scope.off = false;
$scope.$digest();
item = $animate.queue.shift();
expect(item.event).toBe('removeClass');
expect(item.element.text()).toBe('datum');
expect(item.element).toBeShown();
}));
it('should apply the temporary `.ng-hide-animate` class to the element',
inject(function($compile, $rootScope, $animate) {
var item;
var $scope = $rootScope.$new();
$scope.on = false;
element = $compile(html(
'<div class="show-hide" ng-hide="on">data</div>'
))($scope);
$scope.$digest();
item = $animate.queue.shift();
expect(item.event).toEqual('removeClass');
expect(item.options.tempClasses).toEqual('ng-hide-animate');
$scope.on = true;
$scope.$digest();
item = $animate.queue.shift();
expect(item.event).toEqual('addClass');
expect(item.options.tempClasses).toEqual('ng-hide-animate');
}));
});
});