Skip to content

Commit

Permalink
feat(uiSrefStatus): emit state/params in events
Browse files Browse the repository at this point in the history
  • Loading branch information
cristHian Gz committed Jul 15, 2017
1 parent 4226a19 commit 6ea35f2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/directives/uiSrefStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Directive, Output, EventEmitter, ContentChildren, QueryList } from '@angular/core';
import { UISref } from './uiSref';
import {
PathNode, Transition, TargetState, StateObject, anyTrueR, tail, unnestR, Predicate, UIRouterGlobals, Param, PathUtils
PathNode, Transition, TargetState, StateObject, anyTrueR, tail, unnestR, Predicate, UIRouterGlobals, Param, PathUtils, StateOrName
} from '@uirouter/core';

import { Subscription } from 'rxjs/Subscription';
Expand Down Expand Up @@ -32,14 +32,17 @@ export interface SrefStatus {
entering: boolean;
/** A transition is exiting the sref's target state */
exiting: boolean;
/** The sref's target state identifier */
identifier: StateOrName;
}

/** @internalapi */
const inactiveStatus: SrefStatus = {
active: false,
exact: false,
entering: false,
exiting: false
exiting: false,
identifier: null,
};

/**
Expand Down Expand Up @@ -117,16 +120,18 @@ function getSrefStatus(event: TransEvt, srefTarget: TargetState): SrefStatus {
exact: isExact(),
entering: isStartEvent ? isEntering() : false,
exiting: isStartEvent ? isExiting() : false,
identifier: srefTarget.identifier(),
} as SrefStatus;
}

/** @internalapi */
function mergeSrefStatus(left: SrefStatus, right: SrefStatus) {
return {
active: left.active || right.active,
exact: left.exact || right.exact,
entering: left.entering || right.entering,
exiting: left.exiting || right.exiting,
active: left.active || right.active,
exact: left.exact || right.exact,
entering: left.entering || right.entering,
exiting: left.exiting || right.exiting,
identifier: left.identifier || right.identifier,
};
}

Expand All @@ -152,7 +157,7 @@ function mergeSrefStatus(left: SrefStatus, right: SrefStatus) {
* ```
*
* The `uiSrefStatus` event is emitted whenever an enclosed `uiSref`'s status changes.
* The event emitted is of type [[SrefStatus]], and has boolean values for `active`, `exact`, `entering`, and `exiting`.
* The event emitted is of type [[SrefStatus]], and has boolean values for `active`, `exact`, `entering`, and `exiting`; also has a [[StateOrName]] `identifier`value.
*
* The values from this event can be captured and stored on a component (then applied, e.g., using ngClass).
*
Expand Down
55 changes: 55 additions & 0 deletions test/uiSrefStatus/uiSrefStatus.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Component, DebugElement } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { UISref } from '../../src/directives/uiSref';
import { SrefStatus } from '../../src/directives/uiSrefStatus';
import { UIRouterModule } from '../../src/uiRouterNgModule';

import { Subject } from 'rxjs/Subject';

describe('uiSrefStatus', () => {
@Component({
template: '<a uiSref="foo" (uiSrefStatus)="updated($event)"></a>'
})
class TestComponent extends Subject<SrefStatus> {
updated(event: SrefStatus) {
this.next(event);
}
}

let de: DebugElement;
let comp: TestComponent;
let fixture: ComponentFixture<TestComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [UIRouterModule.forRoot({
states: [{ name: 'foo' }],
useHash: true,
})]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
de = fixture.debugElement.query(By.directive(UISref));
});

describe('when click on `foo` uiSref', () => {
beforeEach(() => {
fixture.detectChanges();
});

it('should emit a event with identifier equals to `foo`', done => {
comp.subscribe(event => {
expect(event.identifier).toEqual('foo');
done();
});
de.triggerEventHandler('click', {});
});
});
});

0 comments on commit 6ea35f2

Please sign in to comment.