2016-04-07 20:17:50 -04:00
|
|
|
import {AsyncTestCompleter, TestComponentBuilder, beforeEach, ddescribe, describe, el, expect, iit, inject, it, xit,} from 'angular2/testing_internal';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-11-19 18:09:34 -05:00
|
|
|
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
import {Component} from 'angular2/core';
|
2015-11-18 18:55:43 -05:00
|
|
|
import {NgIf} from 'angular2/common';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-11-06 20:34:07 -05:00
|
|
|
import {IS_DART} from 'angular2/src/facade/lang';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
export function main() {
|
2015-11-23 19:02:19 -05:00
|
|
|
describe('ngIf directive', () => {
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should work in a template attribute',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var html = '<div><copy-me template="ngIf booleanCondition">hello</copy-me></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-26 17:22:35 -04:00
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should work in a template element',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-26 17:22:35 -04:00
|
|
|
var html =
|
2015-11-23 19:02:19 -05:00
|
|
|
'<div><template [ngIf]="booleanCondition"><copy-me>hello2</copy-me></template></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello2');
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-26 17:22:35 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('should toggle node when condition changes',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var html = '<div><copy-me template="ngIf booleanCondition">hello</copy-me></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = true;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-26 17:22:35 -04:00
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should handle nested if correctly',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-26 17:22:35 -04:00
|
|
|
var html =
|
2015-11-23 19:02:19 -05:00
|
|
|
'<div><template [ngIf]="booleanCondition"><copy-me *ngIf="nestedBooleanCondition">hello</copy-me></template></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = true;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.nestedBooleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.nestedBooleanCondition = true;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.booleanCondition = false;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-26 17:22:35 -04:00
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should update several nodes with if',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2016-04-07 20:17:50 -04:00
|
|
|
var html = '<div>' +
|
2015-11-23 19:02:19 -05:00
|
|
|
'<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>' +
|
2015-05-26 17:22:35 -04:00
|
|
|
'</div>';
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(3);
|
|
|
|
expect(DOM.getText(fixture.debugElement.nativeElement))
|
|
|
|
.toEqual('helloNumberhelloStringhelloFunction');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 0;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.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(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('helloNumber');
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-26 17:22:35 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
2015-08-11 17:00:54 -04:00
|
|
|
if (!IS_DART) {
|
2015-05-26 17:22:35 -04:00
|
|
|
it('should not add the element twice if the condition goes from true to true (JS)',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var html = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 2;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(1);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('hello');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-26 17:22:35 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not recreate the element if the condition goes from true to true (JS)',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var html = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
DOM.addClass(DOM.querySelector(fixture.debugElement.nativeElement, 'copy-me'), 'foo');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.numberCondition = 2;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(DOM.hasClass(
|
|
|
|
DOM.querySelector(fixture.debugElement.nativeElement, 'copy-me'), 'foo'))
|
|
|
|
.toBe(true);
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
2015-05-26 17:22:35 -04:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2015-08-11 17:00:54 -04:00
|
|
|
if (IS_DART) {
|
2015-05-26 17:22:35 -04:00
|
|
|
it('should not create the element if the condition is not a boolean (DART)',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var html = '<div><copy-me template="ngIf numberCondition">hello</copy-me></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => {
|
|
|
|
expect(() => fixture.detectChanges()).toThrowError();
|
|
|
|
expect(DOM.querySelectorAll(fixture.debugElement.nativeElement, 'copy-me').length)
|
|
|
|
.toEqual(0);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
async.done();
|
|
|
|
});
|
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-04-07 20:17:50 -04:00
|
|
|
this.stringCondition = 'foo';
|
|
|
|
this.functionCondition = function(s, n) { return s == 'foo' && n == 1; };
|
2015-05-26 17:22:35 -04:00
|
|
|
}
|
|
|
|
}
|