|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | describe('ngShow / ngHide', function() {
|
4 |
| - var element; |
| 4 | + var $scope, $compile, element; |
5 | 5 |
|
| 6 | + beforeEach(inject(function ($rootScope, _$compile_) { |
| 7 | + $scope = $rootScope.$new(); |
| 8 | + $compile = _$compile_; |
| 9 | + })); |
6 | 10 |
|
7 | 11 | afterEach(function() {
|
8 | 12 | dealoc(element);
|
9 | 13 | });
|
10 | 14 |
|
11 | 15 | describe('ngShow', function() {
|
12 |
| - it('should show and hide an element', inject(function($rootScope, $compile) { |
| 16 | + function assertVisibility(exprs, hiddenOrShown) { |
| 17 | + element = $compile('<div></div>')($scope); |
| 18 | + forEach(exprs, function (expr) { |
| 19 | + var childElem = $compile('<div ng-show="' + expr + '"></div>')($scope); |
| 20 | + element.append(childElem); |
| 21 | + $scope.$digest(); |
| 22 | + expect(childElem)[hiddenOrShown === 'shown' ? 'toBeShown' : 'toBeHidden'](); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + function assertShown() { |
| 27 | + assertVisibility(arguments, 'shown'); |
| 28 | + } |
| 29 | + |
| 30 | + function assertHidden() { |
| 31 | + assertVisibility(arguments, 'hidden'); |
| 32 | + } |
| 33 | + |
| 34 | + it('should show and hide an element', function() { |
13 | 35 | element = jqLite('<div ng-show="exp"></div>');
|
14 |
| - element = $compile(element)($rootScope); |
15 |
| - $rootScope.$digest(); |
| 36 | + element = $compile(element)($scope); |
| 37 | + $scope.$digest(); |
16 | 38 | expect(element).toBeHidden();
|
17 |
| - $rootScope.exp = true; |
18 |
| - $rootScope.$digest(); |
| 39 | + $scope.exp = true; |
| 40 | + $scope.$digest(); |
19 | 41 | expect(element).toBeShown();
|
20 |
| - })); |
21 |
| - |
| 42 | + }); |
22 | 43 |
|
23 | 44 | // https://github.com/angular/angular.js/issues/5414
|
24 |
| - it('should show if the expression is a function with a no arguments', inject(function($rootScope, $compile) { |
| 45 | + it('should show if the expression is a function with a no arguments', function() { |
25 | 46 | element = jqLite('<div ng-show="exp"></div>');
|
26 |
| - element = $compile(element)($rootScope); |
27 |
| - $rootScope.exp = function(){}; |
28 |
| - $rootScope.$digest(); |
| 47 | + element = $compile(element)($scope); |
| 48 | + $scope.exp = function(){}; |
| 49 | + $scope.$digest(); |
29 | 50 | expect(element).toBeShown();
|
30 |
| - })); |
31 |
| - |
| 51 | + }); |
32 | 52 |
|
33 |
| - it('should make hidden element visible', inject(function($rootScope, $compile) { |
| 53 | + it('should make hidden element visible', function() { |
34 | 54 | element = jqLite('<div class="ng-hide" ng-show="exp"></div>');
|
35 |
| - element = $compile(element)($rootScope); |
| 55 | + element = $compile(element)($scope); |
36 | 56 | expect(element).toBeHidden();
|
37 |
| - $rootScope.exp = true; |
38 |
| - $rootScope.$digest(); |
| 57 | + $scope.exp = true; |
| 58 | + $scope.$digest(); |
39 | 59 | expect(element).toBeShown();
|
40 |
| - })); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should hide the element if condition is falsy', function() { |
| 63 | + assertHidden('false', 'undefined', 'null', 'NaN', '\'\'', '0'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should show the element if condition is a non-empty string', function() { |
| 67 | + assertShown('\'f\'', '\'0\'', '\'false\'', '\'no\'', '\'n\'', '\'[]\''); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should show the element if condition is an object', function() { |
| 71 | + assertShown('[]', '{}'); |
| 72 | + }); |
41 | 73 | });
|
42 | 74 |
|
43 | 75 | describe('ngHide', function() {
|
44 |
| - it('should hide an element', inject(function($rootScope, $compile) { |
| 76 | + it('should hide an element', function() { |
45 | 77 | element = jqLite('<div ng-hide="exp"></div>');
|
46 |
| - element = $compile(element)($rootScope); |
| 78 | + element = $compile(element)($scope); |
47 | 79 | expect(element).toBeShown();
|
48 |
| - $rootScope.exp = true; |
49 |
| - $rootScope.$digest(); |
| 80 | + $scope.exp = true; |
| 81 | + $scope.$digest(); |
50 | 82 | expect(element).toBeHidden();
|
51 |
| - })); |
| 83 | + }); |
52 | 84 | });
|
53 | 85 | });
|
54 | 86 |
|
|
0 commit comments