Skip to content

Commit 6ae9dac

Browse files
Merge pull request #176 from euangoddard/master
Added tests to cover supporting browser default behaviour on links
2 parents cadfa3f + fd01f30 commit 6ae9dac

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
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 !== 0 || ctrlKey || metaKey)) {
173+
if (this._anchorUISref && (this._anchorUISref.openInNewTab() || button != 0 || ctrlKey || metaKey)) {
174174
return;
175175
}
176176

test/testUtils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { forEach, map, omit, pick } from '@uirouter/core';
2-
import { TestBed } from '@angular/core/testing';
2+
import { DebugElement } from "@angular/core";
33
let stateProps = ["resolve", "resolvePolicy", "data", "template", "templateUrl", "url", "name", "params"];
44

55
export function tree2Array(tree, inheritName) {
@@ -58,4 +58,6 @@ export function PromiseResult(promise?) {
5858
}
5959
}
6060

61-
61+
export function clickOnElement(element: DebugElement, button = 0, metaKey = false, ctrlKey = false) {
62+
element.triggerEventHandler('click', {button, metaKey, ctrlKey});
63+
}

test/uiSref/uiSref.spec.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { UISref } from '../../src/directives/uiSref';
77
import { UIRouter, TargetState, TransitionOptions } from '@uirouter/core';
88
import { Subject } from 'rxjs/Subject';
99
import { Subscription } from "rxjs/Subscription";
10+
import { clickOnElement } from "../testUtils";
1011

1112
describe('uiSref', () => {
1213
@Component({
@@ -104,7 +105,7 @@ describe('uiSref', () => {
104105

105106
describe('when clicked', () => {
106107
beforeEach(() => {
107-
des[0].triggerEventHandler('click', {button: 0, metaKey: false, ctrlKey: false});
108+
clickOnElement(des[0]);
108109
});
109110

110111
it('should ignore the click event', () => {
@@ -121,14 +122,42 @@ describe('uiSref', () => {
121122

122123
describe('when clicked', () => {
123124
beforeEach(() => {
124-
des[0].triggerEventHandler('click', {button: 0, metaKey: false, ctrlKey: false});
125+
clickOnElement(des[0]);
125126
});
126127

127128
it('should navigate to the state', () => {
128129
expect(uiRouterMock.stateService.go).toHaveBeenCalled();
129130
});
130131
});
131132
});
133+
134+
describe('opening in a new tab', () => {
135+
beforeEach(() => {
136+
comp.targetA = '';
137+
fixture.detectChanges();
138+
});
139+
140+
it('should fallback to the browser default when the button is not the left', () => {
141+
clickOnElement(des[0], 1);
142+
expect(uiRouterMock.stateService.go).not.toHaveBeenCalled();
143+
});
144+
145+
it('should fallback to the browser default when the button is not set', () => {
146+
clickOnElement(des[0], null);
147+
expect(uiRouterMock.stateService.go).not.toHaveBeenCalled();
148+
});
149+
150+
it('should fallback to the browser default when the meta key is pressed', () => {
151+
clickOnElement(des[0], 0, true);
152+
expect(uiRouterMock.stateService.go).not.toHaveBeenCalled();
153+
});
154+
155+
it('should fallback to the browser default when the ctrl key is pressed', () => {
156+
clickOnElement(des[0], 0, false, true);
157+
expect(uiRouterMock.stateService.go).not.toHaveBeenCalled();
158+
});
159+
});
160+
132161
});
133162

134163
describe('when the bound values change', () => {

test/uiSrefStatus/uiSrefStatus.spec.ts

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

55
import { SrefStatus, UISrefStatus } from '../../src/directives/uiSrefStatus';
66
import { UIRouterModule } from '../../src/uiRouterNgModule';
7+
import { clickOnElement } from "../testUtils";
78

89
describe('uiSrefStatus', () => {
910
@Component({
@@ -39,7 +40,7 @@ describe('uiSrefStatus', () => {
3940
describe('when click on `foo` uiSref', () => {
4041
beforeEach(async(() => {
4142
spyOn(component, 'updated');
42-
de.triggerEventHandler('click', {button: 0, metaKey: false, ctrlKey: false});
43+
clickOnElement(de);
4344
}));
4445

4546
it('should emit a event with a TargetState pointing to `foo`', () => {

0 commit comments

Comments
 (0)