2015-05-26 17:22:35 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
2015-06-24 16:07:15 -04:00
|
|
|
TestComponentBuilder,
|
2015-05-26 17:22:35 -04:00
|
|
|
beforeEach,
|
2015-11-11 08:28:23 -05:00
|
|
|
beforeEachProviders,
|
2015-05-26 17:22:35 -04:00
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
2015-10-13 03:29:13 -04:00
|
|
|
} from 'angular2/testing_internal';
|
2015-05-26 17:22:35 -04:00
|
|
|
|
2015-11-06 20:34:07 -05:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
2016-03-09 15:05:15 -05:00
|
|
|
import {IS_DART} from 'angular2/src/facade/lang';
|
2016-03-08 16:36:48 -05:00
|
|
|
import {Component, TemplateRef, ContentChild} from 'angular2/core';
|
2015-11-05 17:58:24 -05:00
|
|
|
import {NgFor} from 'angular2/src/common/directives/ng_for';
|
2016-02-03 19:35:38 -05:00
|
|
|
import {NgIf} from 'angular2/src/common/directives/ng_if';
|
2016-02-01 21:31:26 -05:00
|
|
|
import {By} from 'angular2/platform/common_dom';
|
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 =
|
2015-11-23 19:02:19 -05:00
|
|
|
'<div><copy-me template="ngFor #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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
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)',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var template = '<ul><li template="ngFor #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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var template = '<ul><li template="ngFor #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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2015-05-26 17:22:35 -04:00
|
|
|
it('should throw on ref changing to string',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-26 17:22:35 -04:00
|
|
|
var template = '<div>' +
|
2015-11-23 19:02:19 -05:00
|
|
|
'<div template="ngFor #item of items">' +
|
|
|
|
'<div template="ngFor #subitem of item">' +
|
2015-05-26 17:22:35 -04:00
|
|
|
'{{subitem}}-{{item.length}};' +
|
|
|
|
'</div>|' +
|
|
|
|
'</div>' +
|
|
|
|
'</div>';
|
|
|
|
|
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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
var template = '<div><template ngFor #item [ngForOf]="items">' +
|
|
|
|
'<div template="ngFor #subitem of item">' +
|
2015-05-26 17:22:35 -04:00
|
|
|
'{{subitem}}-{{item.length}};' +
|
|
|
|
'</div></template></div>';
|
|
|
|
|
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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var template =
|
|
|
|
`<div><template ngFor #item [ngForOf]="items" #i="index"><div>{{i}}|</div>` +
|
|
|
|
`<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',
|
2015-06-24 16:07:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-05-26 17:22:35 -04:00
|
|
|
var template =
|
2015-11-23 19:02:19 -05:00
|
|
|
'<div><copy-me template="ngFor: var item of items; var 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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var template =
|
|
|
|
'<div><copy-me template="ngFor: var item of items; var isFirst=first">{{isFirst.toString()}}</copy-me></div>';
|
|
|
|
|
|
|
|
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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var template =
|
2015-11-23 19:02:19 -05:00
|
|
|
'<div><copy-me template="ngFor: var item of items; var 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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var template =
|
2015-11-23 19:02:19 -05:00
|
|
|
'<div><copy-me template="ngFor: var item of items; var 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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var template =
|
2015-11-23 19:02:19 -05:00
|
|
|
'<div><copy-me template="ngFor: var item of items; var 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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
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,
|
|
|
|
'<test-cmp><li template="#item #i=index">{{i}}: {{item}};</li></test-cmp>')
|
|
|
|
.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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
tcb.overrideTemplate(TestComponent, `<ul><template ngFor #item [ngForOf]="items"
|
|
|
|
[ngForTemplate]="contentTpl" #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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
2015-11-23 19:02:19 -05:00
|
|
|
tcb.overrideTemplate(TestComponent, `<ul><template ngFor #item [ngForOf]="items"
|
|
|
|
[ngForTemplate]="contentTpl" #i="index">{{i}}=> {{item}};</template></ul>`)
|
2015-11-06 10:34:41 -05:00
|
|
|
.overrideTemplate(
|
|
|
|
ComponentUsingTestComponent,
|
|
|
|
'<test-cmp><li template="#item #i=index">{{i}}: {{item}};</li></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();
|
|
|
|
});
|
|
|
|
}));
|
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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var template =
|
2016-02-24 11:40:02 -05:00
|
|
|
`<template ngFor #item [ngForOf]="items" [ngForTrackBy]="trackById" #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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var template =
|
2016-02-24 11:40:02 -05:00
|
|
|
`<div><template ngFor #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 ',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var template =
|
2016-02-24 11:40:02 -05:00
|
|
|
`<div><template ngFor #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',
|
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
|
|
|
var template =
|
|
|
|
`<div><template ngFor #item [ngForOf]="items" [ngForTrackBy]="trackByIndex">{{item}}</template></div>`;
|
|
|
|
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 {
|
2015-10-09 15:04:10 -04:00
|
|
|
@ContentChild(TemplateRef) contentTpl: TemplateRef;
|
|
|
|
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]; }
|
|
|
|
}
|