Skip to content

Commit bd67d25

Browse files
author
gcca
committed
feat(uiSrefStatus): emit state/params in events
1 parent 9ecc6e2 commit bd67d25

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

src/directives/uiSrefStatus.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Directive, Output, EventEmitter, ContentChildren, QueryList } from '@angular/core';
44
import { UISref } from './uiSref';
55
import {
6-
PathNode, Transition, TargetState, StateObject, anyTrueR, tail, unnestR, Predicate, UIRouterGlobals, Param, PathUtils
6+
PathNode, Transition, TargetState, StateObject, anyTrueR, tail, unnestR, Predicate, UIRouterGlobals, Param, PathUtils, StateOrName
77
} from '@uirouter/core';
88

99
import { Subscription } from 'rxjs/Subscription';
@@ -32,14 +32,17 @@ export interface SrefStatus {
3232
entering: boolean;
3333
/** A transition is exiting the sref's target state */
3434
exiting: boolean;
35+
/** The sref's target state identifier */
36+
identifier: StateOrName;
3537
}
3638

3739
/** @internalapi */
3840
const inactiveStatus: SrefStatus = {
3941
active: false,
4042
exact: false,
4143
entering: false,
42-
exiting: false
44+
exiting: false,
45+
identifier: null,
4346
};
4447

4548
/**
@@ -117,16 +120,18 @@ function getSrefStatus(event: TransEvt, srefTarget: TargetState): SrefStatus {
117120
exact: isExact(),
118121
entering: isStartEvent ? isEntering() : false,
119122
exiting: isStartEvent ? isExiting() : false,
123+
identifier: srefTarget.identifier(),
120124
} as SrefStatus;
121125
}
122126

123127
/** @internalapi */
124128
function mergeSrefStatus(left: SrefStatus, right: SrefStatus) {
125129
return {
126-
active: left.active || right.active,
127-
exact: left.exact || right.exact,
130+
active: left.active || right.active,
131+
exact: left.exact || right.exact,
128132
entering: left.entering || right.entering,
129-
exiting: left.exiting || right.exiting,
133+
exiting: left.exiting || right.exiting,
134+
identifier: left.identifier || right.identifier,
130135
};
131136
}
132137

@@ -152,7 +157,7 @@ function mergeSrefStatus(left: SrefStatus, right: SrefStatus) {
152157
* ```
153158
*
154159
* The `uiSrefStatus` event is emitted whenever an enclosed `uiSref`'s status changes.
155-
* The event emitted is of type [[SrefStatus]], and has boolean values for `active`, `exact`, `entering`, and `exiting`.
160+
* The event emitted is of type [[SrefStatus]], and has boolean values for `active`, `exact`, `entering`, and `exiting`; also has a [[StateOrName]] `identifier`value.
156161
*
157162
* The values from this event can be captured and stored on a component (then applied, e.g., using ngClass).
158163
*
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Component, DebugElement } from '@angular/core';
2+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { By } from '@angular/platform-browser';
4+
5+
import { SrefStatus, UISrefStatus } from '../../src/directives/uiSrefStatus';
6+
import { UIRouterModule } from '../../src/uiRouterNgModule';
7+
8+
describe('uiSrefStatus', () => {
9+
@Component({
10+
template: '<a uiSref="foo" (uiSrefStatus)="updated($event)"></a>',
11+
})
12+
class TestComponent {
13+
updated(event: SrefStatus) {
14+
throw new Error('updated() method must be spied');
15+
}
16+
}
17+
18+
let component: TestComponent;
19+
let de: DebugElement;
20+
let fixture: ComponentFixture<TestComponent>;
21+
22+
beforeEach(async(() => {
23+
TestBed.configureTestingModule({
24+
declarations: [TestComponent],
25+
imports: [UIRouterModule.forRoot({
26+
states: [{ name: 'foo' }],
27+
useHash: true,
28+
})]
29+
}).compileComponents();
30+
}));
31+
32+
beforeEach(() => {
33+
fixture = TestBed.createComponent(TestComponent);
34+
component = fixture.componentInstance;
35+
fixture.detectChanges();
36+
de = fixture.debugElement.query(By.directive(UISrefStatus));
37+
});
38+
39+
describe('when click on `foo` uiSref', () => {
40+
beforeEach(async(() => {
41+
spyOn(component, 'updated');
42+
de.triggerEventHandler('click', {});
43+
}));
44+
45+
it('should emit a event with identifier equals to `foo`', () => {
46+
expect(component.updated).toHaveBeenCalledWith(jasmine.objectContaining({
47+
identifier: 'foo',
48+
}));
49+
});
50+
});
51+
});

0 commit comments

Comments
 (0)