Skip to content

Commit

Permalink
fix(uiSrefActive): Apply active classes on lazy loaded states
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperlewau committed Apr 14, 2015
1 parent bc34b45 commit f0ddbe7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/stateDirectives.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,13 @@ function $StateRefActiveDirective($state, $stateParams, $interpolate) {
// Allow uiSref to communicate with uiSrefActive[Equals]
this.$$addStateInfo = function (newState, newParams) {
var state = $state.get(newState, stateContext($element));
if (state) {
states.push({
state: state,
params: newParams
});
update();
}

states.push({
state: state || { name: newState },
params: newParams
});

update();
};

$scope.$on('$stateChangeSuccess', update);
Expand Down
39 changes: 38 additions & 1 deletion test/stateDirectivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,12 @@ describe('uiStateRef', function() {
});

describe('uiSrefActive', function() {
var el, template, scope, document;
var el, template, scope, document, _stateProvider;

beforeEach(module('ui.router'));

beforeEach(module(function($stateProvider) {
_stateProvider = $stateProvider;
$stateProvider.state('top', {
url: ''
}).state('contacts', {
Expand Down Expand Up @@ -511,6 +512,42 @@ describe('uiSrefActive', function() {
$q.flush();
expect(angular.element(template[0]).attr('class')).toBe('ng-scope active');
}));

it('should match fuzzy on lazy loaded states', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><a ui-sref="contacts.lazy" ui-sref-active="active">Lazy Contact</a></div>');
template = $compile(el)($rootScope);
$rootScope.$digest();

$rootScope.$on('$stateNotFound', function () {
_stateProvider.state('contacts.lazy', {});
});

$state.transitionTo('contacts.item', { id: 1 });
$q.flush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');

$state.transitionTo('contacts.lazy');
$q.flush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
}));

it('should match exactly on lazy loaded states', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><a ui-sref="contacts.lazy" ui-sref-active-eq="active">Lazy Contact</a></div>');
template = $compile(el)($rootScope);
$rootScope.$digest();

$rootScope.$on('$stateNotFound', function () {
_stateProvider.state('contacts.lazy', {});
});

$state.transitionTo('contacts.item', { id: 1 });
$q.flush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');

$state.transitionTo('contacts.lazy');
$q.flush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
}));
});

describe('uiView controllers or onEnter handlers', function() {
Expand Down

0 comments on commit f0ddbe7

Please sign in to comment.