|
| 1 | +describe('collapse directive', function() { |
| 2 | + var elementH, compileFnH, scope, $compile, $animate, $q; |
| 3 | + |
| 4 | + beforeEach(module('ui.bootstrap.collapse')); |
| 5 | + beforeEach(module('ngAnimateMock')); |
| 6 | + beforeEach(inject(function(_$rootScope_, _$compile_, _$animate_, _$q_) { |
| 7 | + scope = _$rootScope_; |
| 8 | + $compile = _$compile_; |
| 9 | + $animate = _$animate_; |
| 10 | + $q = _$q_; |
| 11 | + })); |
| 12 | + |
| 13 | + beforeEach(function() { |
| 14 | + elementH = angular.element( |
| 15 | + '<div uib-collapse="isCollapsed" ' |
| 16 | + + 'expanding="expanding()" ' |
| 17 | + + 'expanded="expanded()" ' |
| 18 | + + 'collapsing="collapsing()" ' |
| 19 | + + 'collapsed="collapsed()" ' |
| 20 | + + 'horizontal>' |
| 21 | + + 'Some Content</div>'); |
| 22 | + compileFnH = $compile(elementH); |
| 23 | + angular.element(document.body).append(elementH); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(function() { |
| 27 | + elementH.remove(); |
| 28 | + }); |
| 29 | + |
| 30 | + function initCallbacks() { |
| 31 | + scope.collapsing = jasmine.createSpy('scope.collapsing'); |
| 32 | + scope.collapsed = jasmine.createSpy('scope.collapsed'); |
| 33 | + scope.expanding = jasmine.createSpy('scope.expanding'); |
| 34 | + scope.expanded = jasmine.createSpy('scope.expanded'); |
| 35 | + } |
| 36 | + |
| 37 | + function assertCallbacks(expected) { |
| 38 | + ['collapsing', 'collapsed', 'expanding', 'expanded'].forEach(function(cbName) { |
| 39 | + if (expected[cbName]) { |
| 40 | + expect(scope[cbName]).toHaveBeenCalled(); |
| 41 | + } else { |
| 42 | + expect(scope[cbName]).not.toHaveBeenCalled(); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + it('should be hidden on initialization if isCollapsed = true', function() { |
| 48 | + initCallbacks(); |
| 49 | + scope.isCollapsed = true; |
| 50 | + compileFnH(scope); |
| 51 | + scope.$digest(); |
| 52 | + expect(elementH.width()).toBe(0); |
| 53 | + assertCallbacks({ collapsed: true }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should not trigger any animation on initialization if isCollapsed = true', function() { |
| 57 | + var wrapperFn = function() { |
| 58 | + $animate.flush(); |
| 59 | + }; |
| 60 | + |
| 61 | + scope.isCollapsed = true; |
| 62 | + compileFnH(scope); |
| 63 | + scope.$digest(); |
| 64 | + |
| 65 | + expect(wrapperFn).toThrowError(/No pending animations ready to be closed or flushed/); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should collapse if isCollapsed = true on subsequent use', function() { |
| 69 | + scope.isCollapsed = false; |
| 70 | + compileFnH(scope); |
| 71 | + scope.$digest(); |
| 72 | + initCallbacks(); |
| 73 | + scope.isCollapsed = true; |
| 74 | + scope.$digest(); |
| 75 | + $animate.flush(); |
| 76 | + expect(elementH.width()).toBe(0); |
| 77 | + assertCallbacks({ collapsing: true, collapsed: true }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should show after toggled from collapsed', function() { |
| 81 | + initCallbacks(); |
| 82 | + scope.isCollapsed = true; |
| 83 | + compileFnH(scope); |
| 84 | + scope.$digest(); |
| 85 | + expect(elementH.width()).toBe(0); |
| 86 | + assertCallbacks({ collapsed: true }); |
| 87 | + scope.collapsed.calls.reset(); |
| 88 | + |
| 89 | + scope.isCollapsed = false; |
| 90 | + scope.$digest(); |
| 91 | + $animate.flush(); |
| 92 | + expect(elementH.width()).not.toBe(0); |
| 93 | + assertCallbacks({ expanding: true, expanded: true }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should not trigger any animation on initialization if isCollapsed = false', function() { |
| 97 | + var wrapperFn = function() { |
| 98 | + $animate.flush(); |
| 99 | + }; |
| 100 | + |
| 101 | + scope.isCollapsed = false; |
| 102 | + compileFnH(scope); |
| 103 | + scope.$digest(); |
| 104 | + |
| 105 | + expect(wrapperFn).toThrowError(/No pending animations ready to be closed or flushed/); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should expand if isCollapsed = false on subsequent use', function() { |
| 109 | + scope.isCollapsed = false; |
| 110 | + compileFnH(scope); |
| 111 | + scope.$digest(); |
| 112 | + scope.isCollapsed = true; |
| 113 | + scope.$digest(); |
| 114 | + $animate.flush(); |
| 115 | + initCallbacks(); |
| 116 | + scope.isCollapsed = false; |
| 117 | + scope.$digest(); |
| 118 | + $animate.flush(); |
| 119 | + expect(elementH.width()).not.toBe(0); |
| 120 | + assertCallbacks({ expanding: true, expanded: true }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should collapse if isCollapsed = true on subsequent uses', function() { |
| 124 | + scope.isCollapsed = false; |
| 125 | + compileFnH(scope); |
| 126 | + scope.$digest(); |
| 127 | + scope.isCollapsed = true; |
| 128 | + scope.$digest(); |
| 129 | + $animate.flush(); |
| 130 | + scope.isCollapsed = false; |
| 131 | + scope.$digest(); |
| 132 | + $animate.flush(); |
| 133 | + initCallbacks(); |
| 134 | + scope.isCollapsed = true; |
| 135 | + scope.$digest(); |
| 136 | + $animate.flush(); |
| 137 | + expect(elementH.width()).toBe(0); |
| 138 | + assertCallbacks({ collapsing: true, collapsed: true }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should change aria-expanded attribute', function() { |
| 142 | + scope.isCollapsed = false; |
| 143 | + compileFnH(scope); |
| 144 | + scope.$digest(); |
| 145 | + expect(elementH.attr('aria-expanded')).toBe('true'); |
| 146 | + |
| 147 | + scope.isCollapsed = true; |
| 148 | + scope.$digest(); |
| 149 | + $animate.flush(); |
| 150 | + expect(elementH.attr('aria-expanded')).toBe('false'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should change aria-hidden attribute', function() { |
| 154 | + scope.isCollapsed = false; |
| 155 | + compileFnH(scope); |
| 156 | + scope.$digest(); |
| 157 | + expect(elementH.attr('aria-hidden')).toBe('false'); |
| 158 | + |
| 159 | + scope.isCollapsed = true; |
| 160 | + scope.$digest(); |
| 161 | + $animate.flush(); |
| 162 | + expect(elementH.attr('aria-hidden')).toBe('true'); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('expanding callback returning a promise', function() { |
| 166 | + var defer, collapsedWidth; |
| 167 | + |
| 168 | + beforeEach(function() { |
| 169 | + defer = $q.defer(); |
| 170 | + |
| 171 | + scope.isCollapsed = true; |
| 172 | + scope.expanding = function() { |
| 173 | + return defer.promise; |
| 174 | + }; |
| 175 | + compileFnH(scope); |
| 176 | + scope.$digest(); |
| 177 | + collapsedWidth = elementH.width(); |
| 178 | + |
| 179 | + // set flag to expand ... |
| 180 | + scope.isCollapsed = false; |
| 181 | + scope.$digest(); |
| 182 | + |
| 183 | + // ... shouldn't expand yet ... |
| 184 | + expect(elementH.attr('aria-expanded')).not.toBe('true'); |
| 185 | + expect(elementH.width()).toBe(collapsedWidth); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should wait for it to resolve before animating', function() { |
| 189 | + defer.resolve(); |
| 190 | + |
| 191 | + // should now expand |
| 192 | + scope.$digest(); |
| 193 | + $animate.flush(); |
| 194 | + |
| 195 | + expect(elementH.attr('aria-expanded')).toBe('true'); |
| 196 | + expect(elementH.width()).toBeGreaterThan(collapsedWidth); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should not animate if it rejects', function() { |
| 200 | + defer.reject(); |
| 201 | + |
| 202 | + // should NOT expand |
| 203 | + scope.$digest(); |
| 204 | + |
| 205 | + expect(elementH.attr('aria-expanded')).not.toBe('true'); |
| 206 | + expect(elementH.width()).toBe(collapsedWidth); |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
| 210 | + describe('collapsing callback returning a promise', function() { |
| 211 | + var defer, expandedWidth; |
| 212 | + |
| 213 | + beforeEach(function() { |
| 214 | + defer = $q.defer(); |
| 215 | + scope.isCollapsed = false; |
| 216 | + scope.collapsing = function() { |
| 217 | + return defer.promise; |
| 218 | + }; |
| 219 | + compileFnH(scope); |
| 220 | + scope.$digest(); |
| 221 | + |
| 222 | + expandedWidth = elementH.width(); |
| 223 | + |
| 224 | + // set flag to collapse ... |
| 225 | + scope.isCollapsed = true; |
| 226 | + scope.$digest(); |
| 227 | + |
| 228 | + // ... but it shouldn't collapse yet ... |
| 229 | + expect(elementH.attr('aria-expanded')).not.toBe('false'); |
| 230 | + expect(elementH.width()).toBe(expandedWidth); |
| 231 | + }); |
| 232 | + |
| 233 | + it('should wait for it to resolve before animating', function() { |
| 234 | + defer.resolve(); |
| 235 | + |
| 236 | + // should now collapse |
| 237 | + scope.$digest(); |
| 238 | + $animate.flush(); |
| 239 | + |
| 240 | + expect(elementH.attr('aria-expanded')).toBe('false'); |
| 241 | + expect(elementH.width()).toBeLessThan(expandedWidth); |
| 242 | + }); |
| 243 | + |
| 244 | + it('should not animate if it rejects', function() { |
| 245 | + defer.reject(); |
| 246 | + |
| 247 | + // should NOT collapse |
| 248 | + scope.$digest(); |
| 249 | + |
| 250 | + expect(elementH.attr('aria-expanded')).not.toBe('false'); |
| 251 | + expect(elementH.width()).toBe(expandedWidth); |
| 252 | + }); |
| 253 | + }); |
| 254 | + |
| 255 | +}); |
0 commit comments