Skip to content

Commit

Permalink
fix(ui-sref-active-eq): Compare parameter values using typed parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Nov 30, 2016
1 parent c1989bc commit 67e4997
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,11 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {

if (!isDefined(state)) { return undefined; }
if ($state.$current !== state) { return false; }
return params ? equalForKeys(state.params.$$values(params), $stateParams) : true;

return !params || objectKeys(params).reduce(function(acc, key) {
var paramDef = state.params[key];
return acc && !paramDef || paramDef.type.equals($stateParams[key], params[key]);
}, true);
};

/**
Expand Down Expand Up @@ -1338,7 +1342,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
}
}

return true;
return objectKeys(params).reduce(function(acc, key) {
var paramDef = state.params[key];
return acc && !paramDef || paramDef.type.equals($stateParams[key], params[key]);
}, true);
};


Expand Down
24 changes: 24 additions & 0 deletions test/stateDirectivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,30 @@ describe('uiSrefActive', function() {
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
}));

// Test for #3154
it('should compare ui-sref-active-eq using typed parameters', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><a ui-sref="arrayparam({ foo: [1,2,3] })" ui-sref-active-eq="active">foo 123</a></div>');
template = $compile(el)($rootScope);
$rootScope.$digest();

expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');

$state.transitionTo('arrayparam', {foo: [1,2,3] });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');

$state.transitionTo('arrayparam', {foo: [1,2,3], bar: 'asdf' });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');

$state.transitionTo('arrayparam', {foo: [1,2] });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
}));

it('should update in response to ui-sref param expression changes', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><a ui-sref="contacts.item.detail({ foo: fooId })" ui-sref-active="active">Contacts</a></div>');
template = $compile(el)($rootScope);
Expand Down

0 comments on commit 67e4997

Please sign in to comment.