| 
									
										
										
										
											2016-06-23 09:47:54 -07: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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 17:23:27 -07:00
										 |  |  | import {CommonModule} from '@angular/common'; | 
					
						
							|  |  |  | import {Component} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  | import {ComponentFixture, TestBed, async} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {By} from '@angular/platform-browser/src/dom/debug/by'; | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {expect} from '@angular/platform-browser/testing/src/matchers'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-04 10:14:44 -07:00
										 |  |  | let thisArg: any; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |   describe('ngFor', () => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |     let fixture: ComponentFixture<any>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getComponent(): TestComponent { return fixture.componentInstance; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function detectChangesAndExpectText(text: string): void { | 
					
						
							|  |  |  |       fixture.detectChanges(); | 
					
						
							|  |  |  |       expect(fixture.nativeElement).toHaveText(text); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 09:54:02 -07:00
										 |  |  |     afterEach(() => { fixture = null as any; }); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |       TestBed.configureTestingModule({ | 
					
						
							| 
									
										
										
										
											2017-08-17 11:40:46 +02:00
										 |  |  |         declarations: [TestComponent], | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |         imports: [CommonModule], | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should reflect initial elements', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          detectChangesAndExpectText('1;2;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should reflect added elements', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items.push(3); | 
					
						
							|  |  |  |          detectChangesAndExpectText('1;2;3;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should reflect removed elements', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items.splice(1, 1); | 
					
						
							|  |  |  |          detectChangesAndExpectText('1;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should reflect moved elements', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items.splice(0, 1); | 
					
						
							|  |  |  |          getComponent().items.push(1); | 
					
						
							|  |  |  |          detectChangesAndExpectText('2;1;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should reflect a mix of all changes (additions/removals/moves)', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [0, 1, 2, 3, 4, 5]; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [6, 2, 7, 0, 4, 8]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          detectChangesAndExpectText('6;2;7;0;4;8;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should iterate over an array of objects', async(() => { | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          const template = '<ul><li *ngFor="let item of items">{{item["name"]}};</li></ul>'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          // INIT
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [{'name': 'misko'}, {'name': 'shyam'}]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('misko;shyam;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          // GROW
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items.push({'name': 'adam'}); | 
					
						
							|  |  |  |          detectChangesAndExpectText('misko;shyam;adam;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          // SHRINK
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items.splice(2, 1); | 
					
						
							|  |  |  |          getComponent().items.splice(0, 1); | 
					
						
							|  |  |  |          detectChangesAndExpectText('shyam;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should gracefully handle nulls', async(() => { | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          const template = '<ul><li *ngFor="let item of null">{{item}};</li></ul>'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          detectChangesAndExpectText(''); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should gracefully handle ref changing to null and back', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          detectChangesAndExpectText('1;2;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-24 09:54:02 -07:00
										 |  |  |          getComponent().items = null !; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          detectChangesAndExpectText(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().items = [1, 2, 3]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('1;2;3;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw on non-iterable ref and suggest using an array', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().items = <any>'whaaa'; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          expect(() => fixture.detectChanges()) | 
					
						
							|  |  |  |              .toThrowError( | 
					
						
							|  |  |  |                  /Cannot find a differ supporting object 'whaaa' of type 'string'. NgFor only supports binding to Iterables such as Arrays/); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw on ref changing to string', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          detectChangesAndExpectText('1;2;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = <any>'whaaa'; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          expect(() => fixture.detectChanges()).toThrowError(); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should works with duplicates', async(() => { | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          const a = new Foo(); | 
					
						
							|  |  |  |          getComponent().items = [a, a]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('foo;foo;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should repeat over nested arrays', async(() => { | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          const template = '<div *ngFor="let item of items">' + | 
					
						
							|  |  |  |              '<div *ngFor="let subitem of item">{{subitem}}-{{item.length}};</div>|' + | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |              '</div>'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [['a', 'b'], ['c']]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('a-2;b-2;|c-1;|'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [['e'], ['f', 'g']]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('e-1;|f-2;g-2;|'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should repeat over nested arrays with no intermediate element', async(() => { | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          const template = '<div *ngFor="let item of items">' + | 
					
						
							|  |  |  |              '<div *ngFor="let subitem of item">{{subitem}}-{{item.length}};</div>' + | 
					
						
							|  |  |  |              '</div>'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [['a', 'b'], ['c']]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('a-2;b-2;c-1;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [['e'], ['f', 'g']]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('e-1;f-2;g-2;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |     it('should repeat over nested ngIf that are the last node in the ngFor template', async(() => { | 
					
						
							|  |  |  |          const template = `<div *ngFor="let item of items; let i=index">` + | 
					
						
							|  |  |  |              `<div>{{i}}|</div>` + | 
					
						
							|  |  |  |              `<div *ngIf="i % 2 == 0">even|</div>` + | 
					
						
							|  |  |  |              `</div>`; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          const items = [1]; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = items; | 
					
						
							|  |  |  |          detectChangesAndExpectText('0|even|'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          items.push(1); | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          detectChangesAndExpectText('0|even|1|'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          items.push(1); | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          detectChangesAndExpectText('0|even|1|2|even|'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-14 20:46:29 -07:00
										 |  |  |     it('should allow of saving the collection', async(() => { | 
					
						
							|  |  |  |          const template = | 
					
						
							| 
									
										
										
										
											2018-06-27 13:05:56 +08:00
										 |  |  |              '<ul><li *ngFor="let item of items as collection; index as i">{{i}}/{{collection.length}} - {{item}};</li></ul>'; | 
					
						
							| 
									
										
										
										
											2017-03-14 20:46:29 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-27 13:05:56 +08:00
										 |  |  |          detectChangesAndExpectText('0/2 - 1;1/2 - 2;'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          getComponent().items = [1, 2, 3]; | 
					
						
							| 
									
										
										
										
											2017-03-14 20:46:29 -07:00
										 |  |  |          detectChangesAndExpectText('0/3 - 1;1/3 - 2;2/3 - 3;'); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |     it('should display indices correctly', async(() => { | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          const template = '<span *ngFor ="let item of items; let i=index">{{i.toString()}}</span>'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('0123456789'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [1, 2, 6, 7, 4, 3, 5, 8, 9, 0]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('0123456789'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should display first item correctly', async(() => { | 
					
						
							|  |  |  |          const template = | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |              '<span *ngFor="let item of items; let isFirst=first">{{isFirst.toString()}}</span>'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [0, 1, 2]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('truefalsefalse'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [2, 1]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('truefalse'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should display last item correctly', async(() => { | 
					
						
							|  |  |  |          const template = | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |              '<span *ngFor="let item of items; let isLast=last">{{isLast.toString()}}</span>'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [0, 1, 2]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('falsefalsetrue'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [2, 1]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('falsetrue'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should display even items correctly', async(() => { | 
					
						
							|  |  |  |          const template = | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |              '<span *ngFor="let item of items; let isEven=even">{{isEven.toString()}}</span>'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [0, 1, 2]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('truefalsetrue'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [2, 1]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('truefalse'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should display odd items correctly', async(() => { | 
					
						
							|  |  |  |          const template = | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |              '<span *ngFor="let item of items; let isOdd=odd">{{isOdd.toString()}}</span>'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [0, 1, 2, 3]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('falsetruefalsetrue'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |          getComponent().items = [2, 1]; | 
					
						
							|  |  |  |          detectChangesAndExpectText('falsetrue'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should allow to use a custom template', async(() => { | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          const template = | 
					
						
							|  |  |  |              '<ng-container *ngFor="let item of items; template: tpl"></ng-container>' + | 
					
						
							| 
									
										
										
										
											2017-01-09 13:16:46 -08:00
										 |  |  |              '<ng-template let-item let-i="index" #tpl><p>{{i}}: {{item}};</p></ng-template>'; | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  |          getComponent().items = ['a', 'b', 'c']; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          detectChangesAndExpectText('0: a;1: b;2: c;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should use a default template if a custom one is null', async(() => { | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          const template = | 
					
						
							|  |  |  |              `<ul><ng-container *ngFor="let item of items; template: null; let i=index">{{i}}: {{item}};</ng-container></ul>`; | 
					
						
							|  |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  |          getComponent().items = ['a', 'b', 'c']; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          detectChangesAndExpectText('0: a;1: b;2: c;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should use a custom template when both default and a custom one are present', async(() => { | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          const template = | 
					
						
							|  |  |  |              '<ng-container *ngFor="let item of items; template: tpl">{{i}};</ng-container>' + | 
					
						
							| 
									
										
										
										
											2017-01-09 13:16:46 -08:00
										 |  |  |              '<ng-template let-item let-i="index" #tpl>{{i}}: {{item}};</ng-template>'; | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          fixture = createTestComponent(template); | 
					
						
							|  |  |  |          getComponent().items = ['a', 'b', 'c']; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |          detectChangesAndExpectText('0: a;1: b;2: c;'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2016-02-01 18:31:26 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-04 10:11:40 -07:00
										 |  |  |     describe('track by', () => { | 
					
						
							| 
									
										
										
										
											2017-01-03 15:14:30 -08:00
										 |  |  |       it('should console.warn if trackBy is not a function', async(() => { | 
					
						
							|  |  |  |            // TODO(vicb): expect a warning message when we have a proper log service
 | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |            const template = `<p *ngFor="let item of items; trackBy: value"></p>`; | 
					
						
							| 
									
										
										
										
											2016-12-21 03:18:24 +03:00
										 |  |  |            fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2017-01-03 15:14:30 -08:00
										 |  |  |            fixture.componentInstance.value = 0; | 
					
						
							|  |  |  |            fixture.detectChanges(); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2016-12-21 03:18:24 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 15:14:30 -08:00
										 |  |  |       it('should track by identity when trackBy is to `null` or `undefined`', async(() => { | 
					
						
							|  |  |  |            // TODO(vicb): expect no warning message when we have a proper log service
 | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |            const template = `<p *ngFor="let item of items; trackBy: value">{{ item }}</p>`; | 
					
						
							| 
									
										
										
										
											2017-01-03 15:14:30 -08:00
										 |  |  |            fixture = createTestComponent(template); | 
					
						
							|  |  |  |            fixture.componentInstance.items = ['a', 'b', 'c']; | 
					
						
							|  |  |  |            fixture.componentInstance.value = null; | 
					
						
							|  |  |  |            detectChangesAndExpectText('abc'); | 
					
						
							|  |  |  |            fixture.componentInstance.value = undefined; | 
					
						
							|  |  |  |            detectChangesAndExpectText('abc'); | 
					
						
							| 
									
										
										
										
											2016-12-21 03:18:24 +03:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should set the context to the component instance', async(() => { | 
					
						
							|  |  |  |            const template = | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |                `<p *ngFor="let item of items; trackBy: trackByContext.bind(this)"></p>`; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            thisArg = null; | 
					
						
							|  |  |  |            fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            expect(thisArg).toBe(getComponent()); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not replace tracked items', async(() => { | 
					
						
							|  |  |  |            const template = | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |                `<p *ngFor="let item of items; trackBy: trackById; let i=index">{{items[i]}}</p>`; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            const buildItemList = () => { | 
					
						
							|  |  |  |              getComponent().items = [{'id': 'a'}]; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |              fixture.detectChanges(); | 
					
						
							|  |  |  |              return fixture.debugElement.queryAll(By.css('p'))[0]; | 
					
						
							|  |  |  |            }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            const firstP = buildItemList(); | 
					
						
							|  |  |  |            const finalP = buildItemList(); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |            expect(finalP.nativeElement).toBe(firstP.nativeElement); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should update implicit local variable on view', async(() => { | 
					
						
							|  |  |  |            const template = | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |                `<div *ngFor="let item of items; trackBy: trackById">{{item['color']}}</div>`; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            getComponent().items = [{'id': 'a', 'color': 'blue'}]; | 
					
						
							|  |  |  |            detectChangesAndExpectText('blue'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            getComponent().items = [{'id': 'a', 'color': 'red'}]; | 
					
						
							|  |  |  |            detectChangesAndExpectText('red'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2017-01-03 15:14:30 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |       it('should move items around and keep them updated ', async(() => { | 
					
						
							|  |  |  |            const template = | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |                `<div *ngFor="let item of items; trackBy: trackById">{{item['color']}}</div>`; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            getComponent().items = [{'id': 'a', 'color': 'blue'}, {'id': 'b', 'color': 'yellow'}]; | 
					
						
							|  |  |  |            detectChangesAndExpectText('blueyellow'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            getComponent().items = [{'id': 'b', 'color': 'orange'}, {'id': 'a', 'color': 'red'}]; | 
					
						
							|  |  |  |            detectChangesAndExpectText('orangered'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should handle added and removed items properly when tracking by index', async(() => { | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  |            const template = `<div *ngFor="let item of items; trackBy: trackByIndex">{{item}}</div>`; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            fixture = createTestComponent(template); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            getComponent().items = ['a', 'b', 'c', 'd']; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |            fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            getComponent().items = ['e', 'f', 'g', 'h']; | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |            fixture.detectChanges(); | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |            getComponent().items = ['e', 'f', 'h']; | 
					
						
							|  |  |  |            detectChangesAndExpectText('efh'); | 
					
						
							| 
									
										
										
										
											2016-08-15 13:52:57 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2016-02-05 16:32:24 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Foo { | 
					
						
							|  |  |  |   toString() { return 'foo'; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 |  |  | @Component({selector: 'test-cmp', template: ''}) | 
					
						
							| 
									
										
										
										
											2015-05-26 14:22:35 -07:00
										 |  |  | class TestComponent { | 
					
						
							| 
									
										
										
										
											2017-01-03 15:14:30 -08:00
										 |  |  |   value: any; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  |   items: any[] = [1, 2]; | 
					
						
							| 
									
										
										
										
											2016-02-24 08:40:02 -08:00
										 |  |  |   trackById(index: number, item: any): string { return item['id']; } | 
					
						
							|  |  |  |   trackByIndex(index: number, item: any): number { return index; } | 
					
						
							| 
									
										
										
										
											2016-08-04 10:14:44 -07:00
										 |  |  |   trackByContext(): void { thisArg = this; } | 
					
						
							| 
									
										
										
										
											2015-10-09 12:04:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 14:18:17 -08:00
										 |  |  | const TEMPLATE = '<div><span *ngFor="let item of items">{{item.toString()}};</span></div>'; | 
					
						
							| 
									
										
										
										
											2016-09-08 20:37:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | function createTestComponent(template: string = TEMPLATE): ComponentFixture<TestComponent> { | 
					
						
							|  |  |  |   return TestBed.overrideComponent(TestComponent, {set: {template: template}}) | 
					
						
							|  |  |  |       .createComponent(TestComponent); | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | } |