Skip to content

Commit 26e8c5a

Browse files
committed
fix(dropdownToggle): Refactor test and fix initial hiding of dropdown target
1 parent 0fe989a commit 26e8c5a

File tree

2 files changed

+69
-51
lines changed

2 files changed

+69
-51
lines changed

src/dropdownToggle/dropdownToggle.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/*
2-
* dropdownToggle - Provides dropdown menu functionality in place of bootstrap js
2+
* dropdownToggle - Provides dropdown menu functionality
33
* @restrict class or attribute
44
* @example:
5-
<li class="dropdown">
6-
<a class="dropdown-toggle">My Dropdown Menu</a>
7-
<ul class="dropdown-menu">
8-
<li ng-repeat="choice in dropChoices">
9-
<a ng-href="{{choice.href}}">{{choice.text}}</a>
10-
</li>
11-
</ul>
12-
</li>
5+
6+
<a dropdown-toggle="#dropdown-menu">My Dropdown Menu</a>
7+
<ul id="dropdown-menu" class="f-dropdown">
8+
<li ng-repeat="choice in dropChoices">
9+
<a ng-href="{{choice.href}}">{{choice.text}}</a>
10+
</li>
11+
</ul>
1312
*/
1413
angular.module('mm.foundation.dropdownToggle', [ 'mm.foundation.position' ])
1514

@@ -22,10 +21,11 @@ angular.module('mm.foundation.dropdownToggle', [ 'mm.foundation.position' ])
2221
dropdownToggle: '@'
2322
},
2423
link: function(scope, element, attrs) {
24+
var dropdown = angular.element($document[0].querySelector(scope.dropdownToggle));
2525

2626
scope.$watch('$location.path', function() { closeMenu(); });
27-
element.bind('click', function (event) {
28-
var dropdown = angular.element($document[0].querySelector(scope.dropdownToggle));
27+
element.bind('click', function (event) {
28+
dropdown = angular.element($document[0].querySelector(scope.dropdownToggle));
2929
var elementWasOpen = (element === openElement);
3030

3131
event.preventDefault();
@@ -60,6 +60,10 @@ angular.module('mm.foundation.dropdownToggle', [ 'mm.foundation.position' ])
6060
$document.bind('click', closeMenu);
6161
}
6262
});
63+
64+
if (dropdown) {
65+
dropdown.css('display', 'none');
66+
}
6367
}
6468
};
6569
}]);
+54-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe('dropdownToggle', function() {
2-
var $compile, $rootScope, $document, $location;
2+
var $compile, $rootScope, $document, $location, elm, toggleElm, targetElm;
33

44
beforeEach(module('mm.foundation.dropdownToggle'));
55

@@ -8,6 +8,7 @@ describe('dropdownToggle', function() {
88
$rootScope = _$rootScope_;
99
$document = _$document_;
1010
$location = _$location_;
11+
$scope = $rootScope.$new();
1112

1213
}));
1314

@@ -19,53 +20,66 @@ describe('dropdownToggle', function() {
1920
'<div><a dropdown-toggle="#' + id + '">Trigger</a>' +
2021
'<ul id="' + id + '"><li>Hello</li></ul></div>'
2122
).appendTo('body');
22-
return $compile(element)($rootScope);
23+
return $compile(element)($scope);
2324
}
2425

25-
it('should toggle on `a` click', function() {
26-
var elm = dropdown();
27-
expect(elm.find('ul').css('display')).toBe('none');
28-
elm.find('a').click();
29-
expect(elm.find('ul').css('display')).toBe('block');
30-
elm.find('a').click();
31-
expect(elm.find('ul').css('display')).toBe('none');
32-
elm.remove();
26+
afterEach(function() {
27+
if (elm) {
28+
elm.remove();
29+
}
3330
});
3431

35-
it('should close on elm click', function() {
36-
var elm = dropdown();
37-
elm.find('a').click();
38-
elm.click();
39-
expect(elm.find('ul').css('display')).toBe('none');
40-
elm.remove();
41-
});
32+
describe('with a single dropdown', function() {
33+
beforeEach(function() {
34+
elm = dropdown();
35+
toggleElm = elm.find('a');
36+
targetElm = elm.find('ul');
37+
});
4238

43-
it('should close on document click', function() {
44-
var elm = dropdown();
45-
elm.find('a').click();
46-
$document.click();
47-
expect(elm.find('ul').css('display')).toBe('none');
48-
elm.remove();
49-
});
39+
it('should initially hide the target element', function() {
40+
expect(targetElm.css('display')).toBe('none');
41+
});
42+
43+
it('should toggle on `a` click', function() {
44+
expect(targetElm.css('display')).toBe('none');
45+
toggleElm.click();
46+
expect(targetElm.css('display')).toBe('block');
47+
toggleElm.click();
48+
expect(targetElm.css('display')).toBe('none');
49+
});
50+
51+
it('should close on elm click', function() {
52+
toggleElm.click();
53+
elm.click();
54+
expect(targetElm.css('display')).toBe('none');
55+
});
56+
57+
it('should close on document click', function() {
58+
toggleElm.click();
59+
expect(targetElm.css('display')).toBe('block');
60+
$document.click();
61+
expect(targetElm.css('display')).toBe('none');
62+
});
5063

51-
it('should close on $location change', function() {
52-
var elm = dropdown();
53-
elm.find('a').click();
54-
$location.path('/foo');
55-
$rootScope.$apply();
56-
expect(elm.find('ul').css('display')).toBe('none');
57-
elm.remove();
64+
it('should close on $location change', function() {
65+
toggleElm.click();
66+
$location.path('/foo');
67+
$rootScope.$apply();
68+
expect(targetElm.css('display')).toBe('none');
69+
});
5870
});
5971

60-
it('should only allow one dropdown to be open at once', function() {
61-
var elm1 = dropdown('target1');
62-
var elm2 = dropdown('target2');
63-
elm1.find('a').click();
64-
elm2.find('a').click();
65-
expect(elm1.find('ul').css('display')).toBe('none');
66-
expect(elm2.find('ul').css('display')).toBe('block');
67-
elm1.remove();
68-
elm2.remove();
72+
describe('with multiple dropdowns', function() {
73+
it('should only allow one dropdown to be open at once', function() {
74+
var elm1 = dropdown('target1');
75+
var elm2 = dropdown('target2');
76+
elm1.find('a').click();
77+
elm2.find('a').click();
78+
expect(elm1.find('ul').css('display')).toBe('none');
79+
expect(elm2.find('ul').css('display')).toBe('block');
80+
elm1.remove();
81+
elm2.remove();
82+
});
6983
});
7084
});
7185

0 commit comments

Comments
 (0)