Skip to content

Commit d837d06

Browse files
committed
test(ng-let): add query strategy specs
1 parent c589131 commit d837d06

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
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+
});

0 commit comments

Comments
 (0)