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-02 18:53:34 -04:00
|
|
|
import {NgIf} from '@angular/common';
|
|
|
|
import {Component} from '@angular/core';
|
2016-08-15 16:52:57 -04:00
|
|
|
import {TestBed, async} from '@angular/core/testing';
|
|
|
|
import {beforeEach, ddescribe, describe, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
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(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
declarations: [TestComponent],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work in a template attribute', async(() => {
|
|
|
|
const template = '<div><copy-me template="ngIf booleanCondition">hello</copy-me></div>';
|
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should work in a template element', async(() => {
|
|
|
|
const template =
|
|
|
|
'<div><template [ngIf]="booleanCondition"><copy-me>hello2</copy-me></template></div>';
|
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello2');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should toggle node when condition changes', async(() => {
|
|
|
|
const template = '<div><copy-me template="ngIf booleanCondition">hello</copy-me></div>';
|
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = true;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle nested if correctly', async(() => {
|
|
|
|
const template =
|
|
|
|
'<div><template [ngIf]="booleanCondition"><copy-me *ngIf="nestedBooleanCondition">hello</copy-me></template></div>';
|
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = true;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.nestedBooleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.nestedBooleanCondition = true;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should update several nodes with if', async(() => {
|
|
|
|
const template = '<div>' +
|
|
|
|
'<copy-me template="ngIf numberCondition + 1 >= 2">helloNumber</copy-me>' +
|
|
|
|
'<copy-me template="ngIf stringCondition == \'foo\'">helloString</copy-me>' +
|
|
|
|
'<copy-me template="ngIf functionCondition(stringCondition, numberCondition)">helloFunction</copy-me>' +
|
|
|
|
'</div>';
|
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(3);
|
|
|
|
expect(getDOM().getText(fixture.debugElement.nativeElement))
|
|
|
|
.toEqual('helloNumberhelloStringhelloFunction');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 0;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('helloString');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 1;
|
|
|
|
fixture.debugElement.componentInstance.stringCondition = 'bar';
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.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(() => {
|
|
|
|
const template = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>';
|
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 2;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not recreate the element if the condition goes from true to true (JS)', async(() => {
|
|
|
|
const template = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>';
|
|
|
|
|
|
|
|
TestBed.overrideComponent(TestComponent, {set: {template: template}});
|
|
|
|
let fixture = TestBed.createComponent(TestComponent);
|
|
|
|
fixture.detectChanges();
|
|
|
|
getDOM().addClass(
|
|
|
|
getDOM().querySelector(fixture.debugElement.nativeElement, 'copy-me'), 'foo');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 2;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(getDOM().hasClass(
|
|
|
|
getDOM().querySelector(fixture.debugElement.nativeElement, 'copy-me'), 'foo'))
|
|
|
|
.toBe(true);
|
|
|
|
}));
|
2015-05-26 17:22:35 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({selector: 'test-cmp', directives: [NgIf], 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
|
|
|
}
|
|
|
|
}
|