Skip to content

Commit 0ae9fa7

Browse files
committed
test: add unit test for NgIfElse and NgIfThenElse
1 parent a8582b2 commit 0ae9fa7

File tree

1 file changed

+112
-2
lines changed

1 file changed

+112
-2
lines changed

Diff for: tests/app/tests/renderer-tests.ts

+112-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,48 @@ export class NgIfLabel {
9494
}
9595
}
9696

97+
@Component({
98+
selector: "ng-if-else",
99+
template: `
100+
<StackLayout>
101+
<Label *ngIf="show; else elseClause" text="If"></Label>
102+
103+
<ng-template #elseClause>
104+
<Label text="Else"></Label>
105+
</ng-template>
106+
</StackLayout>
107+
`
108+
})
109+
export class NgIfElseComponent {
110+
public show: boolean = true;
111+
constructor(public elementRef: ElementRef) {
112+
}
113+
}
114+
115+
116+
@Component({
117+
selector: "ng-if-then-else",
118+
template: `
119+
<StackLayout>
120+
<Placeholder *ngIf="show; then thenTemplate else elseTemplate">
121+
</Placeholder>
122+
123+
<ng-template #thenTemplate>
124+
<Label text="Then"></Label>
125+
</ng-template>
126+
127+
<ng-template #elseTemplate>
128+
<Label text="Else"></Label>
129+
</ng-template>
130+
</StackLayout>
131+
`
132+
})
133+
export class NgIfThenElseComponent {
134+
public show: boolean = true;
135+
constructor(public elementRef: ElementRef) {
136+
}
137+
}
138+
97139
@Component({
98140
selector: "ng-for-label",
99141
template: `<Label *ngFor="let item of items" [text]="item"></Label>`
@@ -112,10 +154,10 @@ describe("Renderer E2E", () => {
112154
LayoutWithLabel, LabelCmp, LabelContainer,
113155
ProjectableCmp, ProjectionContainer,
114156
StyledLabelCmp, StyledLabelCmp2,
115-
NgIfLabel, NgForLabel,
157+
NgIfLabel, NgIfElseComponent, NgIfThenElseComponent,
158+
NgForLabel,
116159
]).then((app) => {
117160
testApp = app;
118-
119161
});
120162
});
121163

@@ -229,6 +271,74 @@ describe("Renderer E2E", () => {
229271
});
230272
});
231273

274+
it("ngIfElse show 'if' template when condition is true", () => {
275+
return testApp.loadComponent(NgIfElseComponent).then(componentRef => {
276+
const component = <NgIfElseComponent>componentRef.instance;
277+
const componentRoot = component.elementRef.nativeElement;
278+
279+
testApp.appRef.tick();
280+
assert.equal(
281+
"(ProxyViewContainer " +
282+
"(StackLayout " +
283+
"(#comment), " + // ng-reflect comment
284+
"(Label[text=If]), (#comment)))", // the content to be displayed and its anchor
285+
286+
dumpView(componentRoot, true));
287+
});
288+
});
289+
290+
it("ngIfElse show 'else' template when condition is false", () => {
291+
return testApp.loadComponent(NgIfElseComponent).then(componentRef => {
292+
const component = <NgIfElseComponent>componentRef.instance;
293+
const componentRoot = component.elementRef.nativeElement;
294+
295+
component.show = false;
296+
testApp.appRef.tick();
297+
assert.equal(
298+
"(ProxyViewContainer " +
299+
"(StackLayout " +
300+
"(#comment), " + // ng-reflect comment
301+
"(Label[text=Else]), (#comment)))", // the content to be displayed and its anchor
302+
303+
dumpView(componentRoot, true));
304+
});
305+
});
306+
307+
it("ngIfThenElse show 'then' template when condition is true", () => {
308+
return testApp.loadComponent(NgIfThenElseComponent).then(componentRef => {
309+
const component = <NgIfThenElseComponent>componentRef.instance;
310+
const componentRoot = component.elementRef.nativeElement;
311+
312+
testApp.appRef.tick();
313+
assert.equal(
314+
"(ProxyViewContainer " +
315+
"(StackLayout " +
316+
"(#comment), " + // ng-reflect comment
317+
"(Label[text=Then]), (#comment), " + // the content to be displayed and its anchor
318+
"(#comment)))", // the anchor for the else template
319+
dumpView(componentRoot, true));
320+
});
321+
});
322+
323+
324+
it("ngIfThenElse show 'else' template when condition is false", () => {
325+
return testApp.loadComponent(NgIfThenElseComponent).then(componentRef => {
326+
const component = <NgIfThenElseComponent>componentRef.instance;
327+
const componentRoot = component.elementRef.nativeElement;
328+
329+
component.show = false;
330+
testApp.appRef.tick();
331+
assert.equal(
332+
"(ProxyViewContainer " +
333+
"(StackLayout " +
334+
"(#comment), " + // ng-reflect comment
335+
"(Label[text=Else]), (#comment), " + // the content to be displayed and its anchor
336+
"(#comment)))", // the anchor for the else template
337+
338+
dumpView(componentRoot, true));
339+
});
340+
});
341+
232342
it("ngFor creates element for each item", () => {
233343
return testApp.loadComponent(NgForLabel).then((componentRef) => {
234344
const componentRoot = componentRef.instance.elementRef.nativeElement;

0 commit comments

Comments
 (0)