Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing is-active testing for sibling, equal and ancestor routes #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js
sudo: false
node_js:
- "4.1"
- "6.10.3"
before_script:
- npm install -g bower
- bower install
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"angular": "^1.4.0",
"angular-bootstrap": "^1.2.1",
"angular-ui-router": "^0.2.14",
"angular-ui-router": "^0.3.2",
"angular-sanitize": "^1.4.0"
},
"devDependencies": {
Expand Down
34 changes: 31 additions & 3 deletions src/ui-router-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ angular.module('ui.router.tabs').directive(
}

var currentStateEqualTo = function(tab) {

var isEqual = $state.is(tab.route, tab.params, tab.options);
return isEqual;
};
Expand All @@ -76,11 +75,39 @@ angular.module('ui.router.tabs').directive(
}
};

$scope.is_sibling = function(tab) {
var isSiblingOfCurrentRoute = false;

if (tab && tab.route && tab.route.indexOf('.') > -1) {
var previousRoutePath = $state.current.name.split('.');
var currentRoutePath = tab.route.split('.');
// console.log('old:', previousRoutePath);
// console.log('new:', currentRoutePath);
var isSibling = true;
for (var i = 0; i < currentRoutePath.length - 2; i++) {
if (previousRoutePath[i] !== currentRoutePath[i]) {
isSibling = false;
}
}
isSiblingOfCurrentRoute = isSibling;
}

return isSiblingOfCurrentRoute;
};

/* whether to highlight given route as part of the current state */
$scope.is_active = function(tab) {

var isAncestorOfCurrentRoute = $state.includes(tab.route, tab.params, tab.options);
return isAncestorOfCurrentRoute;
// console.log('existing route:', $state.current.name);
// console.log('new route:', tab);

var isAncestorOfCurrentRoute = !!$state.includes(tab.route, tab.params, tab.options);
var isSiblingOfCurrentRoute = $scope.is_sibling(tab);

// console.log('is ancestor:', isAncestorOfCurrentRoute);
// console.log('is sibling:', isSiblingOfCurrentRoute);
// console.log('is equal:', currentStateEqualTo(tab));
return isAncestorOfCurrentRoute || isSiblingOfCurrentRoute || currentStateEqualTo(tab);
};

$scope.update_tabs = function() {
Expand All @@ -92,6 +119,7 @@ angular.module('ui.router.tabs').directive(
tab.class = tab.class || '';

tab.active = $scope.is_active(tab);

if (tab.active) {
$scope.tabs.active = index;
}
Expand Down
63 changes: 58 additions & 5 deletions src/ui-router-tabs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,14 @@ describe('Directive : UI Router : Tabs', function() {
var targetTabIndex = 2;
var targetRoute = scope.tabConfiguration[targetTabIndex].route;

state.go(targetRoute);
scope.$apply();

expect(get_current_state()).toEqual(initialRoute);
expect(scope.tabConfiguration[initialTabIndex].active).toBeTruthy();
try {
state.go(targetRoute);
scope.$apply();
}
catch (e) {
expect(get_current_state()).toEqual(initialRoute);
expect(scope.tabConfiguration[initialTabIndex].active).toBeTruthy();
}
});

it('should not change the route or active tab heading if a $stateNotFound event triggers during the route change', function() {
Expand Down Expand Up @@ -280,4 +283,54 @@ describe('Directive : UI Router : Tabs', function() {
expect(get_current_state()).toEqual(previous_state);
expect(spy.notCalled).toBeTruthy();
});

describe('is active', function() {
it('should highlight the active tab for the same route', function() {

var tabIndex = 0;
renderView();

$ngView.find('a').eq(tabIndex).click();

scope.$apply();
spy.reset();

expect(directive_scope.is_active(scope.tabConfiguration[tabIndex])).toBeTruthy();
});

it('should highlight the active tab for sibling routes', function() {

var firstTabIndex = 1;
renderView();

$ngView.find('a').eq(firstTabIndex).click();

scope.$apply();
spy.reset();

var secondTabIndex = 2;

$ngView.find('a').eq(secondTabIndex).click();
scope.$apply();

expect(directive_scope.is_active(scope.tabConfiguration[firstTabIndex])).toBeTruthy();
});
it('should highlight the active tab for common ancestor routes', function() {

var firstTabIndex = 0;
renderView();

$ngView.find('a').eq(firstTabIndex).click();

scope.$apply();
spy.reset();

var secondTabIndex = 2;

$ngView.find('a').eq(secondTabIndex).click();
scope.$apply();

expect(directive_scope.is_active(scope.tabConfiguration[firstTabIndex])).toBeTruthy();
});
});
});