Skip to content

Commit b599e72

Browse files
fix(uiSref): Ignore clicks if destination state is falsey
Fixes #244
1 parent 79b61a9 commit b599e72

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/directives/uiSref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class UISref implements OnChanges {
170170
/** When triggered by a (click) event, this function transitions to the UISref's target state */
171171
@HostListener('click', ['$event.button', '$event.ctrlKey', '$event.metaKey'])
172172
go(button: number, ctrlKey: boolean, metaKey: boolean) {
173-
if (this._anchorUISref && (this._anchorUISref.openInNewTab() || button || !isNumber(button) || ctrlKey || metaKey)) {
173+
if (this._anchorUISref && (this._anchorUISref.openInNewTab() || button || !isNumber(button) || ctrlKey || metaKey) || !this.state) {
174174
return;
175175
}
176176

test/uiSref/uiSref.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { By } from '@angular/platform-browser';
44

55
import { UIRouterModule } from '../../src/uiRouterNgModule';
66
import { UISref } from '../../src/directives/uiSref';
7-
import { UIRouter, TargetState, TransitionOptions } from '@uirouter/core';
7+
import { UIRouter, TargetState, TransitionOptions, StateService } from '@uirouter/core';
88
import { Subject } from 'rxjs/Subject';
99
import { Subscription } from 'rxjs/Subscription';
1010
import { clickOnElement } from '../testUtils';
@@ -46,6 +46,7 @@ describe('uiSref', () => {
4646
describe('when the uiSref is empty', () => {
4747
let des: DebugElement[];
4848
let fixture: ComponentFixture<TestComponent>;
49+
let gospy: jasmine.Spy;
4950

5051
beforeEach(() => {
5152
fixture = TestBed.configureTestingModule({
@@ -54,13 +55,23 @@ describe('uiSref', () => {
5455
}).createComponent(TestComponent);
5556
fixture.detectChanges();
5657
des = fixture.debugElement.queryAll(By.directive(UISref));
58+
const stateService = fixture.debugElement.injector.get(StateService);
59+
gospy = spyOn(stateService, "go");
5760
});
5861

5962

6063
it('should not bind "null" string to `href`', () => {
6164
expect(des[0].nativeElement.hasAttribute('href')).toBeFalsy();
6265
expect(des[1].nativeElement.hasAttribute('href')).toBeFalsy();
6366
});
67+
68+
it('should ignore the click event', () => {
69+
clickOnElement(des[0]);
70+
expect(gospy).not.toHaveBeenCalled();
71+
72+
clickOnElement(des[1]);
73+
expect(gospy).not.toHaveBeenCalled();
74+
});
6475
});
6576

6677
describe('when the uiSref is not empty', () => {
@@ -117,6 +128,7 @@ describe('uiSref', () => {
117128
describe('when target is not _blank', () => {
118129
beforeEach(() => {
119130
comp.targetA = '';
131+
comp.linkA = 'state';
120132
fixture.detectChanges();
121133
});
122134

0 commit comments

Comments
 (0)