2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
beforeEach,
|
|
|
|
beforeEachProviders,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
2016-04-28 20:50:03 -04:00
|
|
|
} from '@angular/core/testing/testing_internal';
|
|
|
|
import {expect} from '@angular/platform-browser/testing';
|
|
|
|
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
|
|
|
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {ListWrapper} from '../../src/facade/collection';
|
|
|
|
import {IS_DART} from '../../src/facade/lang';
|
|
|
|
import {Component, TemplateRef, ContentChild} from '@angular/core';
|
|
|
|
import {NgFor} from '@angular/common';
|
|
|
|
import {NgIf} from '@angular/common';
|
|
|
|
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
export function main() {
|
2015-11-23 19:02:19 -05:00
|
|
|
describe('ngFor', () => {
|
2015-05-26 17:22:35 -04:00
|
|
|
var TEMPLATE =
|
2016-04-25 22:52:24 -04:00
|
|
|
'<div><copy-me template="ngFor let item of items">{{item.toString()}};</copy-me></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should reflect initial elements',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, TEMPLATE)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('1;2;');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should reflect added elements',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, TEMPLATE)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
(<number[]>fixture.debugElement.componentInstance.items).push(3);
|
|
|
|
fixture.detectChanges();
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('1;2;3;');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should reflect removed elements',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, TEMPLATE)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 1);
|
|
|
|
fixture.detectChanges();
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('1;');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should reflect moved elements',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, TEMPLATE)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 0);
|
|
|
|
(<number[]>fixture.debugElement.componentInstance.items).push(1);
|
|
|
|
fixture.detectChanges();
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('2;1;');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should reflect a mix of all changes (additions/removals/moves)',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, TEMPLATE)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = [0, 1, 2, 3, 4, 5];
|
|
|
|
fixture.detectChanges();
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
fixture.debugElement.componentInstance.items = [6, 2, 7, 0, 4, 8];
|
|
|
|
fixture.detectChanges();
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('6;2;7;0;4;8;');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should iterate over an array of objects',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-04-25 22:52:24 -04:00
|
|
|
var template = '<ul><li template="ngFor let item of items">{{item["name"]}};</li></ul>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
// INIT
|
2015-10-31 12:50:19 -04:00
|
|
|
fixture.debugElement.componentInstance.items =
|
|
|
|
[{'name': 'misko'}, {'name': 'shyam'}];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('misko;shyam;');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
// GROW
|
2015-10-31 12:50:19 -04:00
|
|
|
(<any[]>fixture.debugElement.componentInstance.items).push({'name': 'adam'});
|
|
|
|
fixture.detectChanges();
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('misko;shyam;adam;');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
// SHRINK
|
2015-10-31 12:50:19 -04:00
|
|
|
ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 2);
|
|
|
|
ListWrapper.removeAt(fixture.debugElement.componentInstance.items, 0);
|
|
|
|
fixture.detectChanges();
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('shyam;');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should gracefully handle nulls',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-04-25 22:52:24 -04:00
|
|
|
var template = '<ul><li template="ngFor let item of null">{{item}};</li></ul>';
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should gracefully handle ref changing to null and back',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, TEMPLATE)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('1;2;');
|
2015-06-24 16:07:15 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
fixture.debugElement.componentInstance.items = null;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
2015-06-24 16:07:15 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
fixture.debugElement.componentInstance.items = [1, 2, 3];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('1;2;3;');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-03-09 15:05:15 -05:00
|
|
|
if (!IS_DART) {
|
|
|
|
it('should throw on non-iterable ref and suggest using an array',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, TEMPLATE)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = 'whaaa';
|
|
|
|
try {
|
|
|
|
fixture.detectChanges()
|
|
|
|
} catch (e) {
|
|
|
|
expect(e.message).toContain(
|
|
|
|
`Cannot find a differ supporting object 'whaaa' of type 'string'. NgFor only supports binding to Iterables such as Arrays.`);
|
|
|
|
async.done();
|
|
|
|
}
|
|
|
|
});
|
2016-03-09 15:05:15 -05:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2015-05-26 17:22:35 -04:00
|
|
|
it('should throw on ref changing to string',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, TEMPLATE)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('1;2;');
|
2015-06-24 16:07:15 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
fixture.debugElement.componentInstance.items = 'whaaa';
|
|
|
|
expect(() => fixture.detectChanges()).toThrowError();
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should works with duplicates',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, TEMPLATE)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
2015-05-26 17:22:35 -04:00
|
|
|
var a = new Foo();
|
2015-10-31 12:50:19 -04:00
|
|
|
fixture.debugElement.componentInstance.items = [a, a];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('foo;foo;');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should repeat over nested arrays',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-05-26 17:22:35 -04:00
|
|
|
var template = '<div>' +
|
2016-04-25 22:52:24 -04:00
|
|
|
'<div template="ngFor let item of items">' +
|
|
|
|
'<div template="ngFor let subitem of item">' +
|
2016-04-12 12:40:37 -04:00
|
|
|
'{{subitem}}-{{item.length}};' +
|
|
|
|
'</div>|' +
|
|
|
|
'</div>' +
|
|
|
|
'</div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = [['a', 'b'], ['c']];
|
|
|
|
fixture.detectChanges();
|
|
|
|
fixture.detectChanges();
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('a-2;b-2;|c-1;|');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
fixture.debugElement.componentInstance.items = [['e'], ['f', 'g']];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('e-1;|f-2;g-2;|');
|
2015-05-26 17:22:35 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should repeat over nested arrays with no intermediate element',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-04-25 22:52:24 -04:00
|
|
|
var template = '<div><template ngFor let-item [ngForOf]="items">' +
|
|
|
|
'<div template="ngFor let subitem of item">' +
|
2016-04-12 12:40:37 -04:00
|
|
|
'{{subitem}}-{{item.length}};' +
|
|
|
|
'</div></template></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = [['a', 'b'], ['c']];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('a-2;b-2;c-1;');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.items = [['e'], ['f', 'g']];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('e-1;f-2;g-2;');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-02-03 19:35:38 -05:00
|
|
|
it('should repeat over nested ngIf that are the last node in the ngFor temlate',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-02-03 19:35:38 -05:00
|
|
|
var template =
|
2016-04-25 22:52:24 -04:00
|
|
|
`<div><template ngFor let-item [ngForOf]="items" let-i="index"><div>{{i}}|</div>` +
|
2016-02-03 19:35:38 -05:00
|
|
|
`<div *ngIf="i % 2 == 0">even|</div></template></div>`;
|
|
|
|
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
var el = fixture.debugElement.nativeElement;
|
|
|
|
var items = [1];
|
|
|
|
fixture.debugElement.componentInstance.items = items;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(el).toHaveText('0|even|');
|
|
|
|
|
|
|
|
items.push(1);
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(el).toHaveText('0|even|1|');
|
|
|
|
|
|
|
|
items.push(1);
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(el).toHaveText('0|even|1|2|even|');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-11 21:50:41 -04:00
|
|
|
it('should display indices correctly',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-05-26 17:22:35 -04:00
|
|
|
var template =
|
2016-04-25 22:52:24 -04:00
|
|
|
'<div><copy-me template="ngFor: let item of items; let i=index">{{i.toString()}}</copy-me></div>';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-06-24 16:07:15 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('0123456789');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.items = [1, 2, 6, 7, 4, 3, 5, 8, 9, 0];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('0123456789');
|
2015-05-26 17:22:35 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-01-16 10:37:44 -05:00
|
|
|
it('should display first item correctly',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-01-16 10:37:44 -05:00
|
|
|
var template =
|
2016-04-25 22:52:24 -04:00
|
|
|
'<div><copy-me template="ngFor: let item of items; let isFirst=first">{{isFirst.toString()}}</copy-me></div>';
|
2016-01-16 10:37:44 -05:00
|
|
|
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = [0, 1, 2];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('truefalsefalse');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.items = [2, 1];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('truefalse');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-09-04 10:59:04 -04:00
|
|
|
it('should display last item correctly',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-09-04 10:59:04 -04:00
|
|
|
var template =
|
2016-04-25 22:52:24 -04:00
|
|
|
'<div><copy-me template="ngFor: let item of items; let isLast=last">{{isLast.toString()}}</copy-me></div>';
|
2015-09-04 10:59:04 -04:00
|
|
|
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = [0, 1, 2];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('falsefalsetrue');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.items = [2, 1];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('falsetrue');
|
2015-09-04 10:59:04 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-09-14 17:07:59 -04:00
|
|
|
it('should display even items correctly',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-09-14 17:07:59 -04:00
|
|
|
var template =
|
2016-04-25 22:52:24 -04:00
|
|
|
'<div><copy-me template="ngFor: let item of items; let isEven=even">{{isEven.toString()}}</copy-me></div>';
|
2015-09-14 17:07:59 -04:00
|
|
|
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = [0, 1, 2];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('truefalsetrue');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.items = [2, 1];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('truefalse');
|
2015-09-14 17:07:59 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should display odd items correctly',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-09-14 17:07:59 -04:00
|
|
|
var template =
|
2016-04-25 22:52:24 -04:00
|
|
|
'<div><copy-me template="ngFor: let item of items; let isOdd=odd">{{isOdd.toString()}}</copy-me></div>';
|
2015-09-14 17:07:59 -04:00
|
|
|
|
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = [0, 1, 2, 3];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('falsetruefalsetrue');
|
|
|
|
|
|
|
|
fixture.debugElement.componentInstance.items = [2, 1];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('falsetrue');
|
2015-09-14 17:07:59 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-10-09 15:04:10 -04:00
|
|
|
it('should allow to use a custom template',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2015-10-09 15:04:10 -04:00
|
|
|
tcb.overrideTemplate(
|
|
|
|
TestComponent,
|
2015-11-23 19:02:19 -05:00
|
|
|
'<ul><template ngFor [ngForOf]="items" [ngForTemplate]="contentTpl"></template></ul>')
|
2015-10-09 15:04:10 -04:00
|
|
|
.overrideTemplate(
|
|
|
|
ComponentUsingTestComponent,
|
2016-04-25 22:52:24 -04:00
|
|
|
'<test-cmp><li template="let item; let i=index">{{i}}: {{item}};</li></test-cmp>')
|
2015-10-09 15:04:10 -04:00
|
|
|
.createAsync(ComponentUsingTestComponent)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((fixture) => {
|
2016-01-14 00:35:21 -05:00
|
|
|
var testComponent = fixture.debugElement.children[0];
|
2015-10-09 15:04:10 -04:00
|
|
|
testComponent.componentInstance.items = ['a', 'b', 'c'];
|
2015-10-31 12:50:19 -04:00
|
|
|
fixture.detectChanges();
|
2015-10-09 15:04:10 -04:00
|
|
|
expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-11-06 10:34:41 -05:00
|
|
|
it('should use a default template if a custom one is null',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-04-25 22:52:24 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, `<ul><template ngFor let-item [ngForOf]="items"
|
|
|
|
[ngForTemplate]="contentTpl" let-i="index">{{i}}: {{item}};</template></ul>`)
|
2015-11-06 10:34:41 -05:00
|
|
|
.overrideTemplate(ComponentUsingTestComponent, '<test-cmp></test-cmp>')
|
|
|
|
.createAsync(ComponentUsingTestComponent)
|
|
|
|
.then((fixture) => {
|
2016-01-14 00:35:21 -05:00
|
|
|
var testComponent = fixture.debugElement.children[0];
|
2015-11-06 10:34:41 -05:00
|
|
|
testComponent.componentInstance.items = ['a', 'b', 'c'];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should use a custom template when both default and a custom one are present',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-04-25 22:52:24 -04:00
|
|
|
tcb.overrideTemplate(TestComponent, `<ul><template ngFor let-item [ngForOf]="items"
|
|
|
|
[ngForTemplate]="contentTpl" let-i="index">{{i}}=> {{item}};</template></ul>`)
|
2015-11-06 10:34:41 -05:00
|
|
|
.overrideTemplate(
|
|
|
|
ComponentUsingTestComponent,
|
2016-04-25 22:52:24 -04:00
|
|
|
'<test-cmp><li template="let item; let i=index">{{i}}: {{item}};</li></test-cmp>')
|
2015-11-06 10:34:41 -05:00
|
|
|
.createAsync(ComponentUsingTestComponent)
|
|
|
|
.then((fixture) => {
|
2016-01-14 00:35:21 -05:00
|
|
|
var testComponent = fixture.debugElement.children[0];
|
2015-11-06 10:34:41 -05:00
|
|
|
testComponent.componentInstance.items = ['a', 'b', 'c'];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;');
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-02-01 21:31:26 -05:00
|
|
|
|
2016-02-05 19:32:24 -05:00
|
|
|
describe('track by', function() {
|
|
|
|
it('should not replace tracked items',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-02-05 19:32:24 -05:00
|
|
|
var template =
|
2016-04-25 22:52:24 -04:00
|
|
|
`<template ngFor let-item [ngForOf]="items" [ngForTrackBy]="trackById" let-i="index">
|
2016-02-01 21:31:26 -05:00
|
|
|
<p>{{items[i]}}</p>
|
|
|
|
</template>`;
|
2016-02-05 19:32:24 -05:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
var buildItemList =
|
|
|
|
() => {
|
|
|
|
fixture.debugElement.componentInstance.items = [{'id': 'a'}];
|
|
|
|
fixture.detectChanges();
|
|
|
|
return fixture.debugElement.queryAll(By.css('p'))[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
var firstP = buildItemList();
|
|
|
|
var finalP = buildItemList();
|
|
|
|
expect(finalP.nativeElement).toBe(firstP.nativeElement);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
it('should update implicit local variable on view',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-02-05 19:32:24 -05:00
|
|
|
var template =
|
2016-04-25 22:52:24 -04:00
|
|
|
`<div><template ngFor let-item [ngForOf]="items" [ngForTrackBy]="trackById">{{item['color']}}</template></div>`;
|
2016-02-05 19:32:24 -05:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = [{'id': 'a', 'color': 'blue'}];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('blue');
|
|
|
|
fixture.debugElement.componentInstance.items = [{'id': 'a', 'color': 'red'}];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('red');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
it('should move items around and keep them updated ',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-02-05 19:32:24 -05:00
|
|
|
var template =
|
2016-04-25 22:52:24 -04:00
|
|
|
`<div><template ngFor let-item [ngForOf]="items" [ngForTrackBy]="trackById">{{item['color']}}</template></div>`;
|
2016-02-05 19:32:24 -05:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items =
|
|
|
|
[{'id': 'a', 'color': 'blue'}, {'id': 'b', 'color': 'yellow'}];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('blueyellow');
|
|
|
|
fixture.debugElement.componentInstance.items =
|
|
|
|
[{'id': 'b', 'color': 'orange'}, {'id': 'a', 'color': 'red'}];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('orangered');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-02-24 11:40:02 -05:00
|
|
|
|
|
|
|
it('should handle added and removed items properly when tracking by index',
|
2016-06-08 18:45:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: any /** TODO #9100 */) => {
|
2016-02-24 11:40:02 -05:00
|
|
|
var template =
|
2016-04-25 22:52:24 -04:00
|
|
|
`<div><template ngFor let-item [ngForOf]="items" [ngForTrackBy]="trackByIndex">{{item}}</template></div>`;
|
2016-02-24 11:40:02 -05:00
|
|
|
tcb.overrideTemplate(TestComponent, template)
|
|
|
|
.createAsync(TestComponent)
|
|
|
|
.then((fixture) => {
|
|
|
|
fixture.debugElement.componentInstance.items = ['a', 'b', 'c', 'd'];
|
|
|
|
fixture.detectChanges();
|
|
|
|
fixture.debugElement.componentInstance.items = ['e', 'f', 'g', 'h'];
|
|
|
|
fixture.detectChanges();
|
|
|
|
fixture.debugElement.componentInstance.items = ['e', 'f', 'h'];
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('efh');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2016-02-05 19:32:24 -05:00
|
|
|
});
|
2015-05-26 17:22:35 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
toString() { return 'foo'; }
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({selector: 'test-cmp', directives: [NgFor, NgIf], template: ''})
|
2015-05-26 17:22:35 -04:00
|
|
|
class TestComponent {
|
2016-04-28 17:00:31 -04:00
|
|
|
@ContentChild(TemplateRef) contentTpl: TemplateRef<Object>;
|
2015-10-09 15:04:10 -04:00
|
|
|
items: any;
|
|
|
|
constructor() { this.items = [1, 2]; }
|
2016-02-24 11:40:02 -05:00
|
|
|
trackById(index: number, item: any): string { return item['id']; }
|
|
|
|
trackByIndex(index: number, item: any): number { return index; }
|
2015-10-09 15:04:10 -04:00
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({selector: 'outer-cmp', directives: [TestComponent], template: ''})
|
2015-10-09 15:04:10 -04:00
|
|
|
class ComponentUsingTestComponent {
|
2015-05-26 17:22:35 -04:00
|
|
|
items: any;
|
|
|
|
constructor() { this.items = [1, 2]; }
|
|
|
|
}
|