Skip to content

Commit 42d739d

Browse files
fix(uiView): Always inject and/or bind NOWAIT resolve as a Promise object.
Closes #232
1 parent aa59fa8 commit 42d739d

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/directives/uiView.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ export class UIView implements OnInit, OnDestroy {
324324
.getTokens()
325325
.map(token => context.getResolvable(token))
326326
.filter(r => r.resolved);
327-
const newProviders = resolvables.map(r => ({ provide: r.token, useValue: r.data }));
327+
328+
const newProviders = resolvables.map(r => ({ provide: r.token, useValue: context.injector().get(r.token) }));
328329

329330
const parentInject = { context: this._uiViewData.config.viewDecl.$context, fqn: this._uiViewData.fqn };
330331
newProviders.push({ provide: UIView.PARENT_INJECT, useValue: parentInject });
@@ -367,12 +368,14 @@ export class UIView implements OnInit, OnDestroy {
367368
resolvable: context.getResolvable(tuple.token),
368369
});
369370

371+
const injector = context.injector();
372+
370373
explicitInputTuples
371374
.concat(implicitInputTuples)
372375
.map(addResolvable)
373376
.filter(tuple => tuple.resolvable && tuple.resolvable.resolved)
374377
.forEach(tuple => {
375-
component[tuple.prop] = tuple.resolvable.data;
378+
component[tuple.prop] = injector.get(tuple.resolvable.token);
376379
});
377380
}
378381
}

test/uiView/resolveBinding.spec.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
import { Component, DebugElement, Input, NgModuleFactoryLoader, SystemJsNgModuleLoader } from '@angular/core';
1+
import { Component, Inject, Input } from '@angular/core';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
3-
import { UIRouterModule } from '../../src/uiRouterNgModule';
4-
import { Ng2StateDeclaration } from '../../src/interface';
5-
import { UIView } from '../../src/directives/uiView';
3+
import { Ng2StateDeclaration, UIRouterModule, UIView } from '../../src';
64
import { By } from '@angular/platform-browser';
7-
import { UIRouter } from '@uirouter/core';
5+
import { Resolvable, UIRouter } from '@uirouter/core';
86

97
describe('uiView', () => {
108
describe('should map resolve data to inputs', () => {
119
@Component({ template: `<h3>hey</h3> ` })
1210
class ManyResolvesComponent {
11+
constructor(@Inject('resolve1') foo, @Inject('Resolve2') bar, @Inject('resolve5') baz) {
12+
this.injectedValues = Array.from(arguments);
13+
}
14+
15+
injectedValues: any[];
1316
@Input() resolve1;
1417
@Input() resolve2;
1518
@Input('resolve3') _resolve3;
1619
@Input('resolve4') _resolve4;
20+
@Input() resolve5;
1721
}
1822

1923
let comp: ManyResolvesComponent;
@@ -34,6 +38,7 @@ describe('uiView', () => {
3438
{ token: 'Resolve2', resolveFn: () => 'resolve2' },
3539
{ token: 'resolve3', resolveFn: () => 'resolve3' },
3640
{ token: 'Resolve4', resolveFn: () => 'resolve4' },
41+
new Resolvable('resolve5', () => 'resolve5', [], { async: 'NOWAIT' }),
3742
],
3843
};
3944

@@ -52,6 +57,8 @@ describe('uiView', () => {
5257
});
5358
});
5459

60+
/////////////////////////////////////////
61+
5562
it('should bind resolve by name to component input name', () => {
5663
expect(comp.resolve1).toBe('resolve1');
5764
});
@@ -67,5 +74,26 @@ describe('uiView', () => {
6774
it('should bind resolve by name to the component input templateName specified in state `bindings`', () => {
6875
expect(comp._resolve4).toBe('resolve4');
6976
});
77+
78+
it('should bind NOWAIT resolve as a promise object', () => {
79+
expect(comp.resolve5).toBeDefined();
80+
expect(typeof comp.resolve5.then).toBe('function');
81+
});
82+
83+
/////////////////////////////////////////
84+
85+
it('should inject resolve by name to constructor', () => {
86+
expect(comp.injectedValues[0]).toBe('resolve1');
87+
});
88+
89+
it('should inject resolve by resolve name (not binding name) to the constructor', () => {
90+
expect(comp.injectedValues[1]).toBe('resolve2');
91+
});
92+
93+
it('should inject NOWAIT resolve as a promise object', () => {
94+
expect(comp.injectedValues[2]).toBeDefined();
95+
expect(typeof comp.injectedValues[2]).toBe('object');
96+
expect(typeof comp.injectedValues[2].then).toBe('function');
97+
});
7098
});
7199
});

0 commit comments

Comments
 (0)