1- import { CommonModule } from '@angular/common ' ;
1+ import { Component } from '@angular/core ' ;
22import { createHostComponentFactory , SpectatorWithHost } from '@netbasal/spectator' ;
33import { NgForTrackByKeyDirective } from '../../lib/directives/track-by-key.directive' ;
44
5+ @Component ( {
6+ selector : 'lib-animals' ,
7+ template : ''
8+ } )
9+ class AnimalsComponent {
10+ animals : any [ ] ;
11+ }
12+
513describe ( 'NgForTrackByKeyDirective' , ( ) => {
6- let host : SpectatorWithHost < NgForTrackByKeyDirective < any > > ;
14+ let host : SpectatorWithHost < NgForTrackByKeyDirective < any > , AnimalsComponent > ;
715 const create = createHostComponentFactory ( {
816 component : NgForTrackByKeyDirective ,
917 declarations : [ NgForTrackByKeyDirective ] ,
10- imports : [ CommonModule ]
18+ host : AnimalsComponent
1119 } ) ;
1220
1321 it ( 'should add generate trackBy fn by key' , ( ) => {
@@ -21,7 +29,28 @@ describe('NgForTrackByKeyDirective', () => {
2129 </ng-container>
2230 ` ) ;
2331
24- expect ( host . element ) . toHaveText ( 'π¦ π¦' ) ;
32+ expect ( host . hostElement ) . toHaveText ( 'π¦ π¦' ) ;
33+ } ) ;
34+
35+ it ( 'should correct recreate differs when array are changed' , ( ) => {
36+ host = create ( `
37+ <ng-container *ngFor="let item of animals trackByKey 'animal'">
38+ {{ item.animal }}
39+ </ng-container>
40+ ` ) ;
41+
42+ host . setHostInput ( 'animals' , [
43+ { animal : 'π¦' } ,
44+ { animal : 'π¦' }
45+ ] ) ;
46+ expect ( host . hostElement . textContent ) . toEqual ( ' π¦ π¦ ' ) ;
47+
48+ host . setHostInput ( 'animals' , [
49+ { animal : 'π¦' } ,
50+ { animal : 'π' } ,
51+ { animal : 'π¦' }
52+ ] ) ;
53+ expect ( host . hostElement . textContent ) . toEqual ( ' π¦ π π¦ ' ) ;
2554 } ) ;
2655
2756 it ( 'should throw exception' , ( ) => {
0 commit comments