Skip to content

Commit

Permalink
feat: Update uiSref input type from string to StateOrName (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
oBusk committed Dec 2, 2021
1 parent be3ba79 commit fe26fe0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/directives/uiSref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UIRouter, extend, Obj, TransitionOptions, TargetState, isNumber, isNullOrUndefined } from '@uirouter/core';
import { UIRouter, extend, Obj, StateOrName, TransitionOptions, TargetState, isNumber, isNullOrUndefined } from '@uirouter/core';
import {
Directive,
Inject,
Expand Down Expand Up @@ -87,7 +87,7 @@ export class UISref implements OnChanges {
* <a uiSref="hoome">Home</a>
* ```
*/
@Input('uiSref') state: string;
@Input('uiSref') state: StateOrName;

/**
* `@Input('uiParams')` The parameter values to use (as key/values)
Expand Down Expand Up @@ -132,7 +132,7 @@ export class UISref implements OnChanges {
}

/** @internal */
set uiSref(val: string) {
set uiSref(val: StateOrName) {
this.state = val;
this.update();
}
Expand Down
15 changes: 14 additions & 1 deletion test/uiSref/uiSref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { By } from '@angular/platform-browser';

import { UIRouterModule } from '../../src/uiRouterNgModule';
import { UISref } from '../../src/directives/uiSref';
import { UIRouter, TargetState, TransitionOptions } from '@uirouter/core';
import { UIRouter, StateDeclaration, TargetState, TransitionOptions } from '@uirouter/core';
import { Subscription } from 'rxjs';
import { clickOnElement } from '../testUtils';

Expand All @@ -14,6 +14,7 @@ describe('uiSref', () => {
template: `
<a [uiSref]="linkA" [target]="targetA" [uiParams]="linkAParams" [uiOptions]="linkAOptions"></a>
<a [uiSref]="linkB"></a>
<a [uiSref]="linkC"></a>
`,
})
class TestComponent {
Expand All @@ -22,6 +23,7 @@ describe('uiSref', () => {
linkAOptions: TransitionOptions;
targetA: string;
linkB: string;
linkC: StateDeclaration;

@ViewChildren(UISref) srefs: QueryList<UISref>;

Expand All @@ -35,6 +37,7 @@ describe('uiSref', () => {
this.linkAOptions = null;
this.targetA = '';
this.linkB = '';
this.linkC = {};
}
}

Expand Down Expand Up @@ -108,6 +111,16 @@ describe('uiSref', () => {
expect(gospy.mock.calls[0][0]).toBe('stateref');
});

it('should handle when param is stateDeclaration', () => {
const { fixture, srefElements, router } = setup();
const gospy = jest.spyOn(router.stateService, 'go');
fixture.componentInstance.linkC = { name: 'stateref' };
fixture.detectChanges();
clickOnElement(srefElements[2]);
expect(gospy).toHaveBeenCalledTimes(1);
expect(gospy.mock.calls[0][0]).toEqual({ name: 'stateref' });
});

it('should ignore the click event when target is _blank', () => {
const { fixture, srefElements, router } = setup();
const gospy = jest.spyOn(router.stateService, 'go');
Expand Down

0 comments on commit fe26fe0

Please sign in to comment.