@@ -133,37 +133,71 @@ describe('CdkTable', () => {
133
133
} ) ;
134
134
} ) ;
135
135
136
- it ( 'should use differ to add/remove/move rows' , ( ) => {
137
- // Each row receives an attribute 'initialIndex' the element's original place
138
- getRows ( tableElement ) . forEach ( ( row : Element , index : number ) => {
139
- row . setAttribute ( 'initialIndex' , index . toString ( ) ) ;
140
- } ) ;
136
+ describe ( 'should correctly use the differ to add/remove/move rows' , ( ) => {
137
+ function addInitialIndexAttribute ( ) {
138
+ // Each row receives an attribute 'initialIndex' the element's original place
139
+ getRows ( tableElement ) . forEach ( ( row : Element , index : number ) => {
140
+ row . setAttribute ( 'initialIndex' , index . toString ( ) ) ;
141
+ } ) ;
141
142
142
- // Prove that the attributes match their indicies
143
- const initialRows = getRows ( tableElement ) ;
144
- expect ( initialRows [ 0 ] . getAttribute ( 'initialIndex' ) ) . toBe ( '0' ) ;
145
- expect ( initialRows [ 1 ] . getAttribute ( 'initialIndex' ) ) . toBe ( '1' ) ;
146
- expect ( initialRows [ 2 ] . getAttribute ( 'initialIndex' ) ) . toBe ( '2' ) ;
143
+ // Prove that the attributes match their indicies
144
+ const initialRows = getRows ( tableElement ) ;
145
+ expect ( initialRows [ 0 ] . getAttribute ( 'initialIndex' ) ) . toBe ( '0' ) ;
146
+ expect ( initialRows [ 1 ] . getAttribute ( 'initialIndex' ) ) . toBe ( '1' ) ;
147
+ expect ( initialRows [ 2 ] . getAttribute ( 'initialIndex' ) ) . toBe ( '2' ) ;
148
+ }
147
149
148
- // Swap first and second data in data array
149
- const copiedData = component . dataSource ! . data . slice ( ) ;
150
- const temp = copiedData [ 0 ] ;
151
- copiedData [ 0 ] = copiedData [ 1 ] ;
152
- copiedData [ 1 ] = temp ;
150
+ it ( 'when the data is heterogeneous' , ( ) => {
151
+ addInitialIndexAttribute ( ) ;
153
152
154
- // Remove the third element
155
- copiedData . splice ( 2 , 1 ) ;
153
+ // Swap first and second data in data array
154
+ const copiedData = component . dataSource ! . data . slice ( ) ;
155
+ const temp = copiedData [ 0 ] ;
156
+ copiedData [ 0 ] = copiedData [ 1 ] ;
157
+ copiedData [ 1 ] = temp ;
156
158
157
- // Add new data
158
- component . dataSource ! . data = copiedData ;
159
- component . dataSource ! . addData ( ) ;
159
+ // Remove the third element
160
+ copiedData . splice ( 2 , 1 ) ;
160
161
161
- // Expect that the first and second rows were swapped and that the last row is new
162
- const changedRows = getRows ( tableElement ) ;
163
- expect ( changedRows . length ) . toBe ( 3 ) ;
164
- expect ( changedRows [ 0 ] . getAttribute ( 'initialIndex' ) ) . toBe ( '1' ) ;
165
- expect ( changedRows [ 1 ] . getAttribute ( 'initialIndex' ) ) . toBe ( '0' ) ;
166
- expect ( changedRows [ 2 ] . getAttribute ( 'initialIndex' ) ) . toBe ( null ) ;
162
+ // Add new data
163
+ component . dataSource ! . data = copiedData ;
164
+ component . dataSource ! . addData ( ) ;
165
+
166
+ // Expect that the first and second rows were swapped and that the last row is new
167
+ const changedRows = getRows ( tableElement ) ;
168
+ expect ( changedRows . length ) . toBe ( 3 ) ;
169
+ expect ( changedRows [ 0 ] . getAttribute ( 'initialIndex' ) ) . toBe ( '1' ) ;
170
+ expect ( changedRows [ 1 ] . getAttribute ( 'initialIndex' ) ) . toBe ( '0' ) ;
171
+ expect ( changedRows [ 2 ] . getAttribute ( 'initialIndex' ) ) . toBe ( null ) ;
172
+ } ) ;
173
+
174
+ it ( 'when the data is homogeneous' , ( ) => {
175
+ // Change default data to be one that is a list of homogeneous data.
176
+ const obj = { value : true } ;
177
+ component . dataSource ! . data = [ obj , obj , obj ] ;
178
+ addInitialIndexAttribute ( ) ;
179
+
180
+ const copiedData = component . dataSource ! . data . slice ( ) ;
181
+
182
+ // Remove the third element and add a new different obj in the beginning.
183
+ copiedData . splice ( 2 , 1 ) ;
184
+ copiedData . unshift ( { value : false } ) ;
185
+
186
+ // Add new data
187
+ component . dataSource ! . data = copiedData ;
188
+
189
+ // Expect that two of the three rows still have an initial index. Not as concerned about
190
+ // the order they are in, but more important that there was no unnecessary removes/inserts.
191
+ const changedRows = getRows ( tableElement ) ;
192
+ expect ( changedRows . length ) . toBe ( 3 ) ;
193
+ let numInitialRows = 0 ;
194
+ changedRows . forEach ( row => {
195
+ if ( row . getAttribute ( 'initialIndex' ) !== null ) {
196
+ numInitialRows ++ ;
197
+ }
198
+ } ) ;
199
+ expect ( numInitialRows ) . toBe ( 2 ) ;
200
+ } ) ;
167
201
} ) ;
168
202
169
203
it ( 'should clear the row view containers on destroy' , ( ) => {
@@ -529,18 +563,18 @@ describe('CdkTable', () => {
529
563
} ) ;
530
564
531
565
it ( 'should error if there is row data that does not have a matching row template' ,
532
- fakeAsync ( ( ) => {
533
- const whenRowWithoutDefaultFixture = createComponent ( WhenRowWithoutDefaultCdkTableApp ) ;
534
- const data = whenRowWithoutDefaultFixture . componentInstance . dataSource . data ;
535
- expect ( ( ) => {
536
- try {
537
- whenRowWithoutDefaultFixture . detectChanges ( ) ;
538
- flush ( ) ;
539
- } catch {
540
- flush ( ) ;
541
- }
542
- } ) . toThrowError ( getTableMissingMatchingRowDefError ( data [ 0 ] ) . message ) ;
543
- } ) ) ;
566
+ fakeAsync ( ( ) => {
567
+ const whenRowWithoutDefaultFixture = createComponent ( WhenRowWithoutDefaultCdkTableApp ) ;
568
+ const data = whenRowWithoutDefaultFixture . componentInstance . dataSource . data ;
569
+ expect ( ( ) => {
570
+ try {
571
+ whenRowWithoutDefaultFixture . detectChanges ( ) ;
572
+ flush ( ) ;
573
+ } catch {
574
+ flush ( ) ;
575
+ }
576
+ } ) . toThrowError ( getTableMissingMatchingRowDefError ( data [ 0 ] ) . message ) ;
577
+ } ) ) ;
544
578
545
579
it ( 'should fail when multiple rows match data without multiTemplateRows' , fakeAsync ( ( ) => {
546
580
let whenFixture = createComponent ( WhenRowMultipleDefaultsCdkTableApp ) ;
@@ -552,12 +586,12 @@ describe('CdkTable', () => {
552
586
553
587
describe ( 'with multiTemplateRows' , ( ) => {
554
588
it ( 'should be able to render multiple rows per data object' , ( ) => {
555
- let whenFixture = createComponent ( WhenRowCdkTableApp ) ;
556
- whenFixture . componentInstance . multiTemplateRows = true ;
557
- whenFixture . detectChanges ( ) ;
589
+ setupTableTestApp ( WhenRowCdkTableApp ) ;
590
+ component . multiTemplateRows = true ;
591
+ fixture . detectChanges ( ) ;
558
592
559
- const data = whenFixture . componentInstance . dataSource . data ;
560
- expectTableToMatchContent ( whenFixture . nativeElement . querySelector ( 'cdk-table' ) , [
593
+ const data = component . dataSource . data ;
594
+ expectTableToMatchContent ( tableElement , [
561
595
[ 'Column A' , 'Column B' , 'Column C' ] ,
562
596
[ data [ 0 ] . a , data [ 0 ] . b , data [ 0 ] . c ] ,
563
597
[ data [ 1 ] . a , data [ 1 ] . b , data [ 1 ] . c ] ,
@@ -569,12 +603,12 @@ describe('CdkTable', () => {
569
603
} ) ;
570
604
571
605
it ( 'should have the correct data and row indicies' , ( ) => {
572
- let whenFixture = createComponent ( WhenRowCdkTableApp ) ;
573
- whenFixture . componentInstance . multiTemplateRows = true ;
574
- whenFixture . componentInstance . showIndexColumns ( ) ;
575
- whenFixture . detectChanges ( ) ;
606
+ setupTableTestApp ( WhenRowCdkTableApp ) ;
607
+ component . multiTemplateRows = true ;
608
+ component . showIndexColumns ( ) ;
609
+ fixture . detectChanges ( ) ;
576
610
577
- expectTableToMatchContent ( whenFixture . nativeElement . querySelector ( 'cdk-table' ) , [
611
+ expectTableToMatchContent ( tableElement , [
578
612
[ 'Index' , 'Data Index' , 'Render Index' ] ,
579
613
[ '' , '0' , '0' ] ,
580
614
[ '' , '1' , '1' ] ,
@@ -584,6 +618,41 @@ describe('CdkTable', () => {
584
618
[ '' , '3' , '5' ] ,
585
619
] ) ;
586
620
} ) ;
621
+
622
+ it ( 'should have the correct data and row indicies when data is homogeneous' , ( ) => {
623
+ setupTableTestApp ( WhenRowCdkTableApp ) ;
624
+ component . multiTemplateRows = true ;
625
+ component . showIndexColumns ( ) ;
626
+
627
+ const obj = { value : true } ;
628
+ component . dataSource . data = [ obj , obj , obj , obj ] ;
629
+ fixture . detectChanges ( ) ;
630
+
631
+ expectTableToMatchContent ( tableElement , [
632
+ [ 'Index' , 'Data Index' , 'Render Index' ] ,
633
+ [ '' , '0' , '0' ] ,
634
+ [ '' , '1' , '1' ] ,
635
+ [ '' , '1' , '2' ] ,
636
+ [ '' , '2' , '3' ] ,
637
+ [ '' , '3' , '4' ] ,
638
+ ] ) ;
639
+
640
+ // Push unique data on the front and add another obj to the array
641
+ component . dataSource . data = [ { value : false } , obj , obj , obj , obj , obj ] ;
642
+ fixture . detectChanges ( ) ;
643
+
644
+ expectTableToMatchContent ( tableElement , [
645
+ [ 'Index' , 'Data Index' , 'Render Index' ] ,
646
+ [ '' , '0' , '0' ] ,
647
+ [ '' , '1' , '1' ] ,
648
+ [ '' , '1' , '2' ] ,
649
+ [ '' , '2' , '3' ] ,
650
+ [ '' , '3' , '4' ] ,
651
+ [ '' , '4' , '5' ] ,
652
+ [ '' , '5' , '6' ] ,
653
+ ] ) ;
654
+
655
+ } ) ;
587
656
} ) ;
588
657
} ) ;
589
658
0 commit comments