Skip to content

Commit 98a5386

Browse files
test(ui-sref): Implement failing test for #1031
- ui-sref with only parameter values should always refer to the current state (not context sensitive)
1 parent b5c731d commit 98a5386

File tree

1 file changed

+85
-17
lines changed

1 file changed

+85
-17
lines changed

test/stateDirectivesSpec.js

+85-17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ describe('uiStateRef', function() {
1515
}).state('other', {
1616
url: '/other/:id',
1717
template: 'other'
18+
}).state('other.detail', {
19+
url: '/detail',
20+
template: 'detail'
1821
}).state('contacts', {
1922
url: '/contacts',
2023
template: '<a ui-sref=".item({ id: 5 })" class="item">Person</a> <ui-view></ui-view>'
@@ -26,8 +29,16 @@ describe('uiStateRef', function() {
2629
});
2730
}));
2831

29-
beforeEach(inject(function($document) {
32+
beforeEach(inject(function($document, $timeout) {
3033
document = $document[0];
34+
timeoutFlush = function () {
35+
try {
36+
$timeout.flush();
37+
} catch (e) {
38+
// Angular 1.0.8 throws 'No deferred tasks to be flushed' if there is nothing in queue.
39+
// Behave as Angular >=1.1.5 and do nothing in such case.
40+
}
41+
}
3142
}));
3243

3344
function triggerClick(el, options) {
@@ -93,15 +104,6 @@ describe('uiStateRef', function() {
93104
$compile(el)(scope);
94105
$compile(el2)(scope);
95106
scope.$digest();
96-
97-
timeoutFlush = function () {
98-
try {
99-
$timeout.flush();
100-
} catch (e) {
101-
// Angular 1.0.8 throws 'No deferred tasks to be flushed' if there is nothing in queue.
102-
// Behave as Angular >=1.1.5 and do nothing in such case.
103-
}
104-
}
105107
};
106108

107109
describe('links', function() {
@@ -236,20 +238,48 @@ describe('uiStateRef', function() {
236238
expect(obj($stateParams)).toEqualData({});
237239
}));
238240

239-
it('should allow passing params to current state', inject(function($compile, $rootScope, $state) {
240-
$state.current.name = 'contacts.item.detail';
241+
// Test for #1031
242+
it('should allow passing params to current state', inject(function($compile, $rootScope, $state, $q) {
243+
$state.go('other', { id: 'abc' });
244+
$rootScope.$index = 'def';
245+
$rootScope.$digest();
241246

242247
el = angular.element("<a ui-sref=\"{id: $index}\">Details</a>");
243-
$rootScope.$index = 3;
244-
$rootScope.$apply();
245-
246248
$compile(el)($rootScope);
247249
$rootScope.$digest();
248-
expect(el.attr('href')).toBe('#/contacts/3');
250+
251+
expect($state.current.name).toBe('other');
252+
expect($state.params).toEqualValues({ id: 'abc' });
253+
expect(el.attr('href')).toBe('#/other/def');
254+
255+
triggerClick(el);
256+
timeoutFlush();
257+
$q.flush();
258+
259+
expect($state.current.name).toBe('other');
260+
expect($state.params).toEqualValues({ id: 'def' });
261+
262+
$rootScope.$index = 'ghi';
263+
$state.go('other.detail');
264+
$rootScope.$digest();
265+
266+
expect($state.current.name).toBe('other.detail');
267+
expect($state.params).toEqualValues({ id: 'def' });
268+
269+
expect(el.attr('href')).toBe('#/other/ghi/detail');
270+
271+
triggerClick(el);
272+
timeoutFlush();
273+
$q.flush();
274+
275+
expect($state.current.name).toBe('other.detail');
276+
expect($state.params).toEqualValues({ id: 'ghi' });
277+
249278
}));
250279

251280
it('should allow multi-line attribute values when passing params to current state', inject(function($compile, $rootScope, $state) {
252-
$state.current.name = 'contacts.item.detail';
281+
$state.go('contacts.item.detail', { id: '123' });
282+
$rootScope.$digest();
253283

254284
el = angular.element("<a ui-sref=\"{\n\tid: $index\n}\">Details</a>");
255285
$rootScope.$index = 3;
@@ -376,6 +406,44 @@ describe('uiStateRef', function() {
376406
expect(angular.element(template[0]).attr('href')).toBe('#/other/123');
377407
});
378408

409+
// Test for #1031
410+
it('should allow passing params to current state using empty ui-state', inject(function($compile, $rootScope, $state, $q) {
411+
$state.go('other', { id: 'abc' });
412+
$rootScope.$index = 'def';
413+
$rootScope.$digest();
414+
415+
el = angular.element('<a ui-state="" ui-state-params="{id: $index}">Details</a>');
416+
$compile(el)($rootScope);
417+
$rootScope.$digest();
418+
419+
expect($state.current.name).toBe('other');
420+
expect($state.params).toEqualValues({ id: 'abc' });
421+
expect(el.attr('href')).toBe('#/other/def');
422+
423+
triggerClick(el);
424+
timeoutFlush();
425+
$q.flush();
426+
427+
expect($state.current.name).toBe('other');
428+
expect($state.params).toEqualValues({ id: 'def' });
429+
430+
$rootScope.$index = 'ghi';
431+
$state.go('other.detail');
432+
$rootScope.$digest();
433+
434+
expect($state.current.name).toBe('other.detail');
435+
expect($state.params).toEqualValues({ id: 'def' });
436+
437+
expect(el.attr('href')).toBe('#/other/ghi/detail');
438+
439+
triggerClick(el);
440+
timeoutFlush();
441+
$q.flush();
442+
443+
expect($state.current.name).toBe('other.detail');
444+
expect($state.params).toEqualValues({ id: 'ghi' });
445+
}));
446+
379447
it('retains the old href if the new points to a non-state', function () {
380448
expect(angular.element(template[0]).attr('href')).toBe('#/contacts');
381449
scope.state = 'nostate';

0 commit comments

Comments
 (0)