2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-19 15:51:01 -04:00
|
|
|
import {CommonModule} from '@angular/common';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {Component} from '@angular/core';
|
2016-08-15 16:52:57 -04:00
|
|
|
import {TestBed, async} from '@angular/core/testing';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {expect} from '@angular/platform-browser/testing/matchers';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
export function main() {
|
2015-11-23 19:02:19 -05:00
|
|
|
describe('ngIf directive', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-08-15 16:52:57 -04:00
|
|
|
beforeEach(() => {
|
2016-08-19 15:51:01 -04:00
|
|
|
TestBed.configureTestingModule({declarations: [TestComponent], imports: [CommonModule]});
|
2016-08-15 16:52:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work in a template attribute', async(() => {
|
2016-08-23 13:52:40 -04:00
|
|
|
const template = '<div><span template="ngIf booleanCondition">hello</span></div>';
|
2016-08-15 16:52:57 -04:00
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should work in a template element', async(() => {
|
|
|
|
const template =
|
2016-08-23 13:52:40 -04:00
|
|
|
'<div><template [ngIf]="booleanCondition"><span>hello2</span></template></div>';
|
2016-08-15 16:52:57 -04:00
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello2');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should toggle node when condition changes', async(() => {
|
2016-08-23 13:52:40 -04:00
|
|
|
const template = '<div><span template="ngIf booleanCondition">hello</span></div>';
|
2016-08-15 16:52:57 -04:00
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = true;
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle nested if correctly', async(() => {
|
|
|
|
const template =
|
2016-08-23 13:52:40 -04:00
|
|
|
'<div><template [ngIf]="booleanCondition"><span *ngIf="nestedBooleanCondition">hello</span></template></div>';
|
2016-08-15 16:52:57 -04:00
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = true;
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.nestedBooleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.nestedBooleanCondition = true;
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should update several nodes with if', async(() => {
|
|
|
|
const template = '<div>' +
|
2016-08-23 13:52:40 -04:00
|
|
|
'<span template="ngIf numberCondition + 1 >= 2">helloNumber</span>' +
|
|
|
|
'<span template="ngIf stringCondition == \'foo\'">helloString</span>' +
|
|
|
|
'<span template="ngIf functionCondition(stringCondition, numberCondition)">helloFunction</span>' +
|
2016-08-15 16:52:57 -04:00
|
|
|
'</div>';
|
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(3);
|
|
|
|
expect(getDOM().getText(fixture.debugElement.nativeElement))
|
|
|
|
.toEqual('helloNumberhelloStringhelloFunction');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 0;
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('helloString');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 1;
|
|
|
|
fixture.debugElement.componentInstance.stringCondition = 'bar';
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('helloNumber');
|
|
|
|
}));
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2016-07-30 22:31:25 -04:00
|
|
|
it('should not add the element twice if the condition goes from true to true (JS)',
|
2016-08-15 16:52:57 -04:00
|
|
|
async(() => {
|
2016-08-23 13:52:40 -04:00
|
|
|
const template = '<div><span template="ngIf numberCondition">hello</span></div>';
|
2016-08-15 16:52:57 -04:00
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 2;
|
|
|
|
fixture.detectChanges();
|
2016-08-23 13:52:40 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'span').length)
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not recreate the element if the condition goes from true to true (JS)', async(() => {
|
2016-08-23 13:52:40 -04:00
|
|
|
const template = '<div><span template="ngIf numberCondition">hello</span></div>';
|
2016-08-15 16:52:57 -04:00
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.detectChanges();
|
|
|
|
getDOM().addClass(
|
2016-08-23 13:52:40 -04:00
|
|
|
getDOM().querySelector(fixture.debugElement.nativeElement, 'span'), 'foo');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 2;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().hasClass(
|
2016-08-23 13:52:40 -04:00
|
|
|
getDOM().querySelector(fixture.debugElement.nativeElement, 'span'), 'foo'))
|
2016-08-15 16:52:57 -04:00
|
|
|
.toBe(true);
|
|
|
|
}));
|
2015-05-26 17:22:35 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-19 15:51:01 -04:00
|
|
|
@Component({selector: 'test-cmp', template: ''})
|
2015-05-26 17:22:35 -04:00
|
|
|
class TestComponent {
|
|
|
|
booleanCondition: boolean;
|
|
|
|
nestedBooleanCondition: boolean;
|
|
|
|
numberCondition: number;
|
|
|
|
stringCondition: string;
|
|
|
|
functionCondition: Function;
|
|
|
|
constructor() {
|
|
|
|
this.booleanCondition = true;
|
|
|
|
this.nestedBooleanCondition = true;
|
|
|
|
this.numberCondition = 1;
|
2016-06-08 19:38:52 -04:00
|
|
|
this.stringCondition = 'foo';
|
2016-06-12 00:23:37 -04:00
|
|
|
this.functionCondition = function(s: any, n: any): boolean { return s == 'foo' && n == 1; };
|
2015-05-26 17:22:35 -04:00
|
|
|
}
|
|
|
|
}
|