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-09-08 23:37:20 -04:00
|
|
|
import {ComponentFixture, 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-09-08 23:37:20 -04:00
|
|
|
let fixture: ComponentFixture<any>;
|
|
|
|
|
|
|
|
function getComponent(): TestComponent { return fixture.componentInstance; }
|
|
|
|
|
|
|
|
afterEach(() => { fixture = null; });
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-08-15 16:52:57 -04:00
|
|
|
beforeEach(() => {
|
2016-09-08 23:37:20 -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-09-08 23:37:20 -04:00
|
|
|
fixture = createTestComponent(template);
|
2016-08-15 16:52:57 -04:00
|
|
|
|
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(1);
|
|
|
|
expect(fixture.nativeElement).toHaveText('hello');
|
2016-08-15 16:52:57 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
fixture = createTestComponent(template);
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(1);
|
|
|
|
expect(fixture.nativeElement).toHaveText('hello2');
|
2016-08-15 16:52:57 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
fixture = createTestComponent(template);
|
|
|
|
getComponent().booleanCondition = false;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(0);
|
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
getComponent().booleanCondition = true;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(1);
|
|
|
|
expect(fixture.nativeElement).toHaveText('hello');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
getComponent().booleanCondition = false;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(0);
|
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
2016-08-15 16:52:57 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
fixture = createTestComponent(template);
|
|
|
|
|
|
|
|
getComponent().booleanCondition = false;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(0);
|
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
getComponent().booleanCondition = true;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(1);
|
|
|
|
expect(fixture.nativeElement).toHaveText('hello');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
getComponent().nestedBooleanCondition = false;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(0);
|
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
getComponent().nestedBooleanCondition = true;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(1);
|
|
|
|
expect(fixture.nativeElement).toHaveText('hello');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
getComponent().booleanCondition = false;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(0);
|
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
2016-08-15 16:52:57 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
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>';
|
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
fixture = createTestComponent(template);
|
|
|
|
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(3);
|
|
|
|
expect(getDOM().getText(fixture.nativeElement))
|
2016-08-15 16:52:57 -04:00
|
|
|
.toEqual('helloNumberhelloStringhelloFunction');
|
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
getComponent().numberCondition = 0;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(1);
|
|
|
|
expect(fixture.nativeElement).toHaveText('helloString');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
getComponent().numberCondition = 1;
|
|
|
|
getComponent().stringCondition = 'bar';
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(1);
|
|
|
|
expect(fixture.nativeElement).toHaveText('helloNumber');
|
2016-08-15 16:52:57 -04:00
|
|
|
}));
|
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
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
fixture = createTestComponent(template);
|
|
|
|
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(1);
|
|
|
|
expect(fixture.nativeElement).toHaveText('hello');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
getComponent().numberCondition = 2;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().querySelectorAll(fixture.nativeElement, 'span').length).toEqual(1);
|
|
|
|
expect(fixture.nativeElement).toHaveText('hello');
|
2016-08-15 16:52:57 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
fixture = createTestComponent(template);
|
|
|
|
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
getDOM().addClass(getDOM().querySelector(fixture.nativeElement, 'span'), 'foo');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
2016-09-08 23:37:20 -04:00
|
|
|
getComponent().numberCondition = 2;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-08 23:37:20 -04:00
|
|
|
expect(getDOM().hasClass(getDOM().querySelector(fixture.nativeElement, 'span'), 'foo'))
|
2016-08-15 16:52:57 -04:00
|
|
|
.toBe(true);
|
|
|
|
}));
|
2016-12-08 00:41:27 -05:00
|
|
|
|
|
|
|
describe('else', () => {
|
|
|
|
it('should support else', async(() => {
|
|
|
|
const template = '<div>' +
|
|
|
|
'<span *ngIf="booleanCondition; else elseBlock">TRUE</span>' +
|
|
|
|
'<template #elseBlock>FALSE</template>' +
|
|
|
|
'</div>';
|
|
|
|
|
|
|
|
fixture = createTestComponent(template);
|
|
|
|
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('TRUE');
|
|
|
|
|
|
|
|
getComponent().booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('FALSE');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should support then and else', async(() => {
|
|
|
|
const template = '<div>' +
|
|
|
|
'<span *ngIf="booleanCondition; then thenBlock; else elseBlock">IGNORE</span>' +
|
|
|
|
'<template #thenBlock>THEN</template>' +
|
|
|
|
'<template #elseBlock>ELSE</template>' +
|
|
|
|
'</div>';
|
|
|
|
|
|
|
|
fixture = createTestComponent(template);
|
|
|
|
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('THEN');
|
|
|
|
|
|
|
|
getComponent().booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('ELSE');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should support dynamic else', async(() => {
|
|
|
|
const template = '<div>' +
|
|
|
|
'<span *ngIf="booleanCondition; else nestedBooleanCondition ? b1 : b2">TRUE</span>' +
|
|
|
|
'<template #b1>FALSE1</template>' +
|
|
|
|
'<template #b2>FALSE2</template>' +
|
|
|
|
'</div>';
|
|
|
|
|
|
|
|
fixture = createTestComponent(template);
|
|
|
|
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('TRUE');
|
|
|
|
|
|
|
|
getComponent().booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('FALSE1');
|
|
|
|
|
|
|
|
getComponent().nestedBooleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('FALSE2');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should support binding to variable', async(() => {
|
|
|
|
const template = '<div>' +
|
|
|
|
'<span *ngIf="booleanCondition; else elseBlock; let v">{{v}}</span>' +
|
|
|
|
'<template #elseBlock let-v>{{v}}</template>' +
|
|
|
|
'</div>';
|
|
|
|
|
|
|
|
fixture = createTestComponent(template);
|
|
|
|
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('true');
|
|
|
|
|
|
|
|
getComponent().booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.nativeElement).toHaveText('false');
|
|
|
|
}));
|
|
|
|
});
|
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 {
|
2016-09-08 23:37:20 -04:00
|
|
|
booleanCondition: boolean = true;
|
|
|
|
nestedBooleanCondition: boolean = true;
|
|
|
|
numberCondition: number = 1;
|
|
|
|
stringCondition: string = 'foo';
|
|
|
|
functionCondition: Function = (s: any, n: any): boolean => s == 'foo' && n == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createTestComponent(template: string): ComponentFixture<TestComponent> {
|
|
|
|
return TestBed.overrideComponent(TestComponent, {set: {template: template}})
|
|
|
|
.createComponent(TestComponent);
|
2015-05-26 17:22:35 -04:00
|
|
|
}
|