2015-03-13 06:10:11 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
2015-03-13 10:30:10 -04:00
|
|
|
beforeEachBindings,
|
2015-03-13 06:10:11 -04:00
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
|
|
|
} from 'angular2/test_lib';
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-02-27 17:50:06 -05:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2015-02-24 09:32:44 -05:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-04-28 21:17:00 -04:00
|
|
|
import {Component} from 'angular2/src/core/annotations_impl/annotations';
|
|
|
|
import {View} from 'angular2/src/core/annotations_impl/view';
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-05-13 04:11:33 -04:00
|
|
|
import {NgFor} from 'angular2/src/directives/ng_for';
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
import {TestBed} from 'angular2/src/test_lib/test_bed';
|
|
|
|
|
2015-03-13 10:30:10 -04:00
|
|
|
|
2014-12-05 20:44:00 -05:00
|
|
|
export function main() {
|
2015-05-11 20:04:55 -04:00
|
|
|
describe('ng-for', () => {
|
|
|
|
var TEMPLATE = '<div><copy-me template="ng-for #item of items">{{item.toString()}};</copy-me></div>';
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should reflect initial elements', inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
tb.createView(TestComponent, {html: TEMPLATE}).then((view) => {
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('1;2;');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should reflect added elements', inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
tb.createView(TestComponent, {html: TEMPLATE}).then((view) => {
|
|
|
|
view.detectChanges();
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
ListWrapper.push(view.context.items, 3);
|
|
|
|
view.detectChanges();
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('1;2;3;');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should reflect removed elements', inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
tb.createView(TestComponent, {html: TEMPLATE}).then((view) => {
|
|
|
|
view.detectChanges();
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
ListWrapper.removeAt(view.context.items, 1);
|
|
|
|
view.detectChanges();
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('1;');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should reflect moved elements', inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
tb.createView(TestComponent, {html: TEMPLATE}).then((view) => {
|
|
|
|
view.detectChanges();
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
ListWrapper.removeAt(view.context.items, 0);
|
|
|
|
ListWrapper.push(view.context.items, 1);
|
|
|
|
view.detectChanges();
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('2;1;');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should reflect a mix of all changes (additions/removals/moves)',
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
tb.createView(TestComponent, {html: TEMPLATE}).then((view) => {
|
|
|
|
view.context.items = [0, 1, 2, 3, 4, 5];
|
|
|
|
view.detectChanges();
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.items = [6, 2, 7, 0, 4, 8];
|
|
|
|
view.detectChanges();
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('6;2;7;0;4;8;');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should iterate over an array of objects', inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
2015-05-11 20:04:55 -04:00
|
|
|
var template = '<ul><li template="ng-for #item of items">{{item["name"]}};</li></ul>';
|
2015-03-30 11:11:18 -04:00
|
|
|
|
|
|
|
tb.createView(TestComponent, {html: template}).then((view) => {
|
2014-12-05 20:44:00 -05:00
|
|
|
|
|
|
|
// INIT
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.items = [{'name': 'misko'}, {'name':'shyam'}];
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('misko;shyam;');
|
2014-12-05 20:44:00 -05:00
|
|
|
|
|
|
|
// GROW
|
2015-03-30 11:11:18 -04:00
|
|
|
ListWrapper.push(view.context.items, {'name': 'adam'});
|
|
|
|
view.detectChanges();
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('misko;shyam;adam;');
|
2014-12-05 20:44:00 -05:00
|
|
|
|
|
|
|
// SHRINK
|
2015-03-30 11:11:18 -04:00
|
|
|
ListWrapper.removeAt(view.context.items, 2);
|
|
|
|
ListWrapper.removeAt(view.context.items, 0);
|
|
|
|
view.detectChanges();
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('shyam;');
|
2015-03-13 19:22:01 -04:00
|
|
|
async.done();
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
2015-03-13 19:22:01 -04:00
|
|
|
}));
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should gracefully handle nulls', inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
2015-05-11 20:04:55 -04:00
|
|
|
var template = '<ul><li template="ng-for #item of null">{{item}};</li></ul>';
|
2015-03-30 11:11:18 -04:00
|
|
|
tb.createView(TestComponent, {html: template}).then((view) => {
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should gracefully handle ref changing to null and back',
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
tb.createView(TestComponent, {html: TEMPLATE}).then((view) => {
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('1;2;');
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.items = null;
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('');
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.items = [1, 2, 3];
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('1;2;3;');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should throw on ref changing to string', inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
tb.createView(TestComponent, {html: TEMPLATE}).then((view) => {
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('1;2;');
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.items = 'whaaa';
|
|
|
|
expect(() => view.detectChanges()).toThrowError();
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should works with duplicates', inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
tb.createView(TestComponent, {html: TEMPLATE}).then((view) => {
|
2014-12-05 20:44:00 -05:00
|
|
|
var a = new Foo();
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.items = [a, a];
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('foo;foo;');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should repeat over nested arrays', inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var template =
|
2015-03-13 19:22:01 -04:00
|
|
|
'<div>'+
|
2015-05-11 20:04:55 -04:00
|
|
|
'<div template="ng-for #item of items">' +
|
|
|
|
'<div template="ng-for #subitem of item">' +
|
2015-03-13 19:22:01 -04:00
|
|
|
'{{subitem}}-{{item.length}};' +
|
|
|
|
'</div>|'+
|
|
|
|
'</div>'+
|
2015-03-30 11:11:18 -04:00
|
|
|
'</div>';
|
|
|
|
|
|
|
|
tb.createView(TestComponent, {html: template}).then((view) => {
|
|
|
|
view.context.items = [['a', 'b'], ['c']];
|
|
|
|
view.detectChanges();
|
|
|
|
view.detectChanges();
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('a-2;b-2;|c-1;|');
|
2015-03-19 05:04:42 -04:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.items = [['e'], ['f', 'g']];
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('e-1;|f-2;g-2;|');
|
2015-03-19 05:04:42 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should repeat over nested arrays with no intermediate element',
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var template =
|
2015-05-11 20:04:55 -04:00
|
|
|
'<div><template [ng-for] #item [ng-for-of]="items">' +
|
|
|
|
'<div template="ng-for #subitem of item">' +
|
2015-03-19 05:04:42 -04:00
|
|
|
'{{subitem}}-{{item.length}};' +
|
2015-03-30 11:11:18 -04:00
|
|
|
'</div></template></div>';
|
2015-03-19 05:04:42 -04:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
tb.createView(TestComponent, {html: template}).then((view) => {
|
|
|
|
view.context.items = [['a', 'b'], ['c']];
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('a-2;b-2;c-1;');
|
2015-03-19 05:04:42 -04:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.items = [['e'], ['f', 'g']];
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('e-1;f-2;g-2;');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
it('should display indices correctly',
|
|
|
|
inject([TestBed, AsyncTestCompleter], (tb, async) => {
|
|
|
|
var template =
|
2015-05-11 20:04:55 -04:00
|
|
|
'<div><copy-me template="ng-for: var item of items; var i=index">{{i.toString()}}</copy-me></div>';
|
2015-03-30 11:11:18 -04:00
|
|
|
|
|
|
|
tb.createView(TestComponent, {html: template}).then((view) => {
|
|
|
|
view.context.items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('0123456789');
|
2014-12-05 20:44:00 -05:00
|
|
|
|
2015-03-30 11:11:18 -04:00
|
|
|
view.context.items = [1, 2, 6, 7, 4, 3, 5, 8, 9, 0];
|
|
|
|
view.detectChanges();
|
2015-04-07 23:54:20 -04:00
|
|
|
expect(DOM.getText(view.rootNodes[0])).toEqual('0123456789');
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-01-27 18:42:08 -05:00
|
|
|
|
2014-12-05 20:44:00 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
toString() {
|
|
|
|
return 'foo';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 08:44:59 -05:00
|
|
|
@Component({selector: 'test-cmp'})
|
2015-05-11 20:04:55 -04:00
|
|
|
@View({directives: [NgFor]})
|
2014-12-05 20:44:00 -05:00
|
|
|
class TestComponent {
|
|
|
|
items: any;
|
|
|
|
constructor() {
|
|
|
|
this.items = [1, 2];
|
|
|
|
}
|
|
|
|
}
|