@@ -145,21 +145,21 @@ describe('CdkTable', () => {
145
145
146
146
describe ( 'with trackBy' , ( ) => {
147
147
148
- afterEach ( ( ) => {
149
- // Resetting the static value of the trackby function for TrackByCdkTableApp
150
- TrackByCdkTableApp . trackBy = 'reference' ;
151
- } ) ;
148
+ let trackByComponent : TrackByCdkTableApp ;
149
+ let trackByFixture : ComponentFixture < TrackByCdkTableApp > ;
150
+
151
+ function createTestComponentWithTrackyByTable ( trackByStrategy ) {
152
+ trackByFixture = TestBed . createComponent ( TrackByCdkTableApp ) ;
152
153
153
- function createTestComponentWithTrackyByTable ( ) {
154
- fixture = TestBed . createComponent ( TrackByCdkTableApp ) ;
154
+ trackByComponent = trackByFixture . componentInstance ;
155
+ trackByComponent . trackByStrategy = trackByStrategy ;
155
156
156
- component = fixture . componentInstance ;
157
- dataSource = component . dataSource as FakeDataSource ;
158
- table = component . table ;
159
- tableElement = fixture . nativeElement . querySelector ( 'cdk-table' ) ;
157
+ dataSource = trackByComponent . dataSource as FakeDataSource ;
158
+ table = trackByComponent . table ;
159
+ tableElement = trackByFixture . nativeElement . querySelector ( 'cdk-table' ) ;
160
160
161
- fixture . detectChanges ( ) ; // Let the component and table create embedded views
162
- fixture . detectChanges ( ) ; // Let the cells render
161
+ trackByFixture . detectChanges ( ) ; // Let the component and table create embedded views
162
+ trackByFixture . detectChanges ( ) ; // Let the cells render
163
163
164
164
// Each row receives an attribute 'initialIndex' the element's original place
165
165
getRows ( tableElement ) . forEach ( ( row : Element , index : number ) => {
@@ -176,7 +176,7 @@ describe('CdkTable', () => {
176
176
// Swap first two elements, remove the third, add new data
177
177
function mutateData ( ) {
178
178
// Swap first and second data in data array
179
- const copiedData = component . dataSource . data . slice ( ) ;
179
+ const copiedData = trackByComponent . dataSource . data . slice ( ) ;
180
180
const temp = copiedData [ 0 ] ;
181
181
copiedData [ 0 ] = copiedData [ 1 ] ;
182
182
copiedData [ 1 ] = temp ;
@@ -185,12 +185,12 @@ describe('CdkTable', () => {
185
185
copiedData . splice ( 2 , 1 ) ;
186
186
187
187
// Add new data
188
- component . dataSource . data = copiedData ;
189
- component . dataSource . addData ( ) ;
188
+ trackByComponent . dataSource . data = copiedData ;
189
+ trackByComponent . dataSource . addData ( ) ;
190
190
}
191
191
192
192
it ( 'should add/remove/move rows with reference-based trackBy' , ( ) => {
193
- createTestComponentWithTrackyByTable ( ) ;
193
+ createTestComponentWithTrackyByTable ( 'reference' ) ;
194
194
mutateData ( ) ;
195
195
196
196
// Expect that the first and second rows were swapped and that the last row is new
@@ -202,11 +202,11 @@ describe('CdkTable', () => {
202
202
} ) ;
203
203
204
204
it ( 'should add/remove/move rows with changed references without property-based trackBy' , ( ) => {
205
- createTestComponentWithTrackyByTable ( ) ;
205
+ createTestComponentWithTrackyByTable ( 'reference' ) ;
206
206
mutateData ( ) ;
207
207
208
208
// Change each item reference to show that the trackby is not checking the item properties.
209
- component . dataSource . data = component . dataSource . data
209
+ trackByComponent . dataSource . data = trackByComponent . dataSource . data
210
210
. map ( item => { return { a : item . a , b : item . b , c : item . c } ; } ) ;
211
211
212
212
// Expect that all the rows are considered new since their references are all different
@@ -218,13 +218,12 @@ describe('CdkTable', () => {
218
218
} ) ;
219
219
220
220
it ( 'should add/remove/move rows with changed references with property-based trackBy' , ( ) => {
221
- TrackByCdkTableApp . trackBy = 'propertyA' ;
222
- createTestComponentWithTrackyByTable ( ) ;
221
+ createTestComponentWithTrackyByTable ( 'propertyA' ) ;
223
222
mutateData ( ) ;
224
223
225
224
// Change each item reference to show that the trackby is checking the item properties.
226
225
// Otherwise this would cause them all to be removed/added.
227
- component . dataSource . data = component . dataSource . data
226
+ trackByComponent . dataSource . data = trackByComponent . dataSource . data
228
227
. map ( item => { return { a : item . a , b : item . b , c : item . c } ; } ) ;
229
228
230
229
// Expect that the first and second rows were swapped and that the last row is new
@@ -236,13 +235,12 @@ describe('CdkTable', () => {
236
235
} ) ;
237
236
238
237
it ( 'should add/remove/move rows with changed references with index-based trackBy' , ( ) => {
239
- TrackByCdkTableApp . trackBy = 'index' ;
240
- createTestComponentWithTrackyByTable ( ) ;
238
+ createTestComponentWithTrackyByTable ( 'index' ) ;
241
239
mutateData ( ) ;
242
240
243
241
// Change each item reference to show that the trackby is checking the index.
244
242
// Otherwise this would cause them all to be removed/added.
245
- component . dataSource . data = component . dataSource . data
243
+ trackByComponent . dataSource . data = trackByComponent . dataSource . data
246
244
. map ( item => { return { a : item . a , b : item . b , c : item . c } ; } ) ;
247
245
248
246
// Expect first two to be the same since they were swapped but indicies are consistent.
@@ -453,15 +451,15 @@ class DynamicDataSourceCdkTableApp {
453
451
`
454
452
} )
455
453
class TrackByCdkTableApp {
456
- static trackBy : 'reference' | 'propertyA' | 'index' = 'reference' ;
454
+ trackByStrategy : 'reference' | 'propertyA' | 'index' = 'reference' ;
457
455
458
456
dataSource : FakeDataSource = new FakeDataSource ( ) ;
459
457
columnsToRender = [ 'column_a' , 'column_b' ] ;
460
458
461
459
@ViewChild ( CdkTable ) table : CdkTable < TestData > ;
462
460
463
- trackBy ( index : number , item : TestData ) {
464
- switch ( TrackByCdkTableApp . trackBy ) {
461
+ trackBy = ( index : number , item : TestData ) => {
462
+ switch ( this . trackByStrategy ) {
465
463
case 'reference' : return item ;
466
464
case 'propertyA' : return item . a ;
467
465
case 'index' : return index ;
0 commit comments