|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | describe('ngShow / ngHide', function() {
|
4 |
| - var element; |
| 4 | + var $scope, $compile, element; |
5 | 5 |
|
| 6 | + function expectVisibility(exprs, ngShowOrNgHide, shownOrHidden) { |
| 7 | + element = $compile('<div></div>')($scope); |
| 8 | + forEach(exprs, function (expr) { |
| 9 | + var childElem = $compile('<div ' + ngShowOrNgHide + '="' + expr + '"></div>')($scope); |
| 10 | + element.append(childElem); |
| 11 | + $scope.$digest(); |
| 12 | + expect(childElem)[shownOrHidden === 'shown' ? 'toBeShown' : 'toBeHidden'](); |
| 13 | + }); |
| 14 | + } |
| 15 | + |
| 16 | + beforeEach(inject(function ($rootScope, _$compile_) { |
| 17 | + $scope = $rootScope.$new(); |
| 18 | + $compile = _$compile_; |
| 19 | + })); |
6 | 20 |
|
7 | 21 | afterEach(function() {
|
8 | 22 | dealoc(element);
|
9 | 23 | });
|
10 | 24 |
|
11 | 25 | describe('ngShow', function() {
|
12 |
| - it('should show and hide an element', inject(function($rootScope, $compile) { |
| 26 | + function expectShown() { |
| 27 | + expectVisibility(arguments, 'ng-show', 'shown'); |
| 28 | + } |
| 29 | + |
| 30 | + function expectHidden() { |
| 31 | + expectVisibility(arguments, 'ng-show', '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 | + expectHidden('false', 'undefined', 'null', 'NaN', '\'\'', '0'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should show the element if condition is a non-empty string', function() { |
| 67 | + expectShown('\'f\'', '\'0\'', '\'false\'', '\'no\'', '\'n\'', '\'[]\''); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should show the element if condition is an object', function() { |
| 71 | + expectShown('[]', '{}'); |
| 72 | + }); |
41 | 73 | });
|
42 | 74 |
|
43 | 75 | describe('ngHide', function() {
|
44 |
| - it('should hide an element', inject(function($rootScope, $compile) { |
| 76 | + function expectShown() { |
| 77 | + expectVisibility(arguments, 'ng-hide', 'shown'); |
| 78 | + } |
| 79 | + |
| 80 | + function expectHidden() { |
| 81 | + expectVisibility(arguments, 'ng-hide', 'hidden'); |
| 82 | + } |
| 83 | + |
| 84 | + it('should hide an element', function() { |
45 | 85 | element = jqLite('<div ng-hide="exp"></div>');
|
46 |
| - element = $compile(element)($rootScope); |
| 86 | + element = $compile(element)($scope); |
47 | 87 | expect(element).toBeShown();
|
48 |
| - $rootScope.exp = true; |
49 |
| - $rootScope.$digest(); |
| 88 | + $scope.exp = true; |
| 89 | + $scope.$digest(); |
50 | 90 | expect(element).toBeHidden();
|
51 |
| - })); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should show the element if condition is falsy', function() { |
| 94 | + expectShown('false', 'undefined', 'null', 'NaN', '\'\'', '0'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should hide the element if condition is a non-empty string', function() { |
| 98 | + expectHidden('\'f\'', '\'0\'', '\'false\'', '\'no\'', '\'n\'', '\'[]\''); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should hide the element if condition is an object', function() { |
| 102 | + expectHidden('[]', '{}'); |
| 103 | + }); |
52 | 104 | });
|
53 | 105 | });
|
54 | 106 |
|
|
0 commit comments