Skip to content

Update uiSref input type to match functionality #943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be changed on line 125 too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused? L125 in this file?

@Inject(UIView.PARENT_INJECT) parent: ParentUIViewInject

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, typo. I meant Line 135

  /** @internal */
  set uiSref(val: string) { // here
    this.state = val;
    this.update();
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I suppose so? But I can't really find anywhere this internal method is being used tbh? Was thinking it was for testing or maybe something angular-hybrid does, but I can't find anywhere we're uiSref is given a value on the class directly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do believe the setter is how the data-binding is applied

  @Input('uiSref') state: StateOrName;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm well the decorator should bind the directive input "uiSref" to the class property "state". But nevertheless, whatever it's use, the setter should have the same type as the value it sets, I've updated it.


/**
* `@Input('uiParams')` The parameter values to use (as key/values)
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 @@ -39,6 +41,7 @@ describe('uiSref', () => {
this.linkAOptions = null;
this.targetA = '';
this.linkB = '';
this.linkC = {};
}
}

Expand Down Expand Up @@ -112,6 +115,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