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