Skip to content

Commit 4f3a8bf

Browse files
authored
Merge pull request UiPath#39 from UiPath/feature/ng_let_static_support
Feature / Allow ngLet content to be queried statically
2 parents be688ab + d837d06 commit 4f3a8bf

File tree

2 files changed

+150
-28
lines changed

2 files changed

+150
-28
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import {
2+
Component,
3+
ElementRef,
4+
ViewChild,
5+
} from '@angular/core';
6+
import {
7+
ComponentFixture,
8+
TestBed,
9+
} from '@angular/core/testing';
10+
11+
import { of } from 'rxjs';
12+
import { delay } from 'rxjs/operators';
13+
14+
import { UiNgLetModule } from './ui-ng-let.module';
15+
16+
describe('Directive: UiClipboard', () => {
17+
@Component({
18+
template: `
19+
<ng-container *ngLet="data$ | async as data">
20+
<div #child>
21+
<p>{{data?.value}}</p>
22+
</div>
23+
</ng-container>
24+
`,
25+
})
26+
class NgLetStaticFixtureComponent {
27+
@ViewChild('child', {
28+
static: true,
29+
})
30+
public child!: ElementRef<HTMLDivElement>;
31+
32+
public data$ = of({
33+
value: 'smth',
34+
}).pipe(delay(100));
35+
}
36+
describe('QueryStrategy: static', () => {
37+
let fixture: ComponentFixture<NgLetStaticFixtureComponent>;
38+
let component: NgLetStaticFixtureComponent;
39+
40+
beforeEach(() => {
41+
TestBed.configureTestingModule({
42+
imports: [
43+
UiNgLetModule,
44+
],
45+
declarations: [
46+
NgLetStaticFixtureComponent,
47+
],
48+
});
49+
fixture = TestBed.createComponent(NgLetStaticFixtureComponent);
50+
component = fixture.componentInstance;
51+
52+
fixture.detectChanges();
53+
});
54+
55+
afterEach(() => {
56+
fixture.destroy();
57+
});
58+
59+
it('should allow the content to be queried onInit (static: true)', async () => {
60+
expect(component.child).toBeDefined();
61+
62+
await fixture.whenStable();
63+
64+
expect(component.child.nativeElement.innerText).toEqual('');
65+
66+
fixture.detectChanges();
67+
68+
expect(component.child).toBeDefined();
69+
expect(component.child.nativeElement.innerText).toEqual('smth');
70+
});
71+
});
72+
73+
@Component({
74+
template: `
75+
<ng-container *ngLet="data$ | async as data">
76+
<div #child>
77+
<p>{{data?.value}}</p>
78+
</div>
79+
</ng-container>
80+
`,
81+
})
82+
class NgLetDynamicFixtureComponent {
83+
@ViewChild('child', {
84+
static: false,
85+
})
86+
public child!: ElementRef<HTMLDivElement>;
87+
88+
public data$ = of({
89+
value: 'smth',
90+
}).pipe(delay(100));
91+
}
92+
describe('QueryStrategy: static', () => {
93+
let fixture: ComponentFixture<NgLetDynamicFixtureComponent>;
94+
let component: NgLetDynamicFixtureComponent;
95+
96+
beforeEach(() => {
97+
TestBed.configureTestingModule({
98+
imports: [
99+
UiNgLetModule,
100+
],
101+
declarations: [
102+
NgLetDynamicFixtureComponent,
103+
],
104+
});
105+
fixture = TestBed.createComponent(NgLetDynamicFixtureComponent);
106+
component = fixture.componentInstance;
107+
108+
fixture.detectChanges();
109+
});
110+
111+
afterEach(() => {
112+
fixture.destroy();
113+
});
114+
115+
it('should allow the content to be queried on afterViewInit (static: false)', async () => {
116+
expect(component.child).toBeDefined();
117+
118+
await fixture.whenStable();
119+
120+
expect(component.child.nativeElement.innerText).toEqual('');
121+
122+
fixture.detectChanges();
123+
124+
expect(component.child).toBeDefined();
125+
expect(component.child.nativeElement.innerText).toEqual('smth');
126+
});
127+
});
128+
});
Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {
2-
Directive,
3-
Input,
4-
OnInit,
5-
TemplateRef,
6-
ViewContainerRef,
2+
Directive,
3+
Input,
4+
TemplateRef,
5+
ViewContainerRef,
76
} from '@angular/core';
87

98
/**
@@ -26,33 +25,28 @@ class NgLetContext {
2625
// tslint:disable-next-line: directive-selector
2726
selector: '[ngLet]',
2827
})
29-
export class UiNgLetDirective implements OnInit {
28+
export class UiNgLetDirective {
3029
private _context = new NgLetContext();
3130

32-
/**
33-
* The context bound to the decorated area.
34-
*
35-
*/
36-
@Input()
31+
/**
32+
* The context bound to the decorated area.
33+
*
34+
*/
35+
@Input()
3736
set ngLet(value: any) {
3837
this._context.$implicit = this._context.ngLet = value;
3938
}
4039

41-
/**
42-
* @ignore
43-
*/
44-
constructor(
45-
private _vcr: ViewContainerRef,
46-
private _templateRef: TemplateRef<NgLetContext>,
47-
) { }
48-
49-
/**
50-
* @ignore
51-
*/
52-
ngOnInit() {
53-
this._vcr.createEmbeddedView(
54-
this._templateRef,
55-
this._context,
56-
);
57-
}
40+
/**
41+
* @ignore
42+
*/
43+
constructor(
44+
private _vcr: ViewContainerRef,
45+
private _templateRef: TemplateRef<NgLetContext>,
46+
) {
47+
this._vcr.createEmbeddedView(
48+
this._templateRef,
49+
this._context,
50+
);
51+
}
5852
}

0 commit comments

Comments
 (0)