@@ -148,21 +148,21 @@ describe('CdkTable', () => {
148
148
149
149
describe ( 'with trackBy' , ( ) => {
150
150
151
- afterEach ( ( ) => {
152
- // Resetting the static value of the trackby function for TrackByCdkTableApp
153
- TrackByCdkTableApp . trackBy = 'reference' ;
154
- } ) ;
151
+ let trackByComponent : TrackByCdkTableApp ;
152
+ let trackByFixture : ComponentFixture < TrackByCdkTableApp > ;
153
+
154
+ function createTestComponentWithTrackyByTable ( trackByStrategy ) {
155
+ trackByFixture = TestBed . createComponent ( TrackByCdkTableApp ) ;
155
156
156
- function createTestComponentWithTrackyByTable ( ) {
157
- fixture = TestBed . createComponent ( TrackByCdkTableApp ) ;
157
+ trackByComponent = trackByFixture . componentInstance ;
158
+ trackByComponent . trackByStrategy = trackByStrategy ;
158
159
159
- component = fixture . componentInstance ;
160
- dataSource = component . dataSource as FakeDataSource ;
161
- table = component . table ;
162
- tableElement = fixture . nativeElement . querySelector ( 'cdk-table' ) ;
160
+ dataSource = trackByComponent . dataSource as FakeDataSource ;
161
+ table = trackByComponent . table ;
162
+ tableElement = trackByFixture . nativeElement . querySelector ( 'cdk-table' ) ;
163
163
164
- fixture . detectChanges ( ) ; // Let the component and table create embedded views
165
- fixture . detectChanges ( ) ; // Let the cells render
164
+ trackByFixture . detectChanges ( ) ; // Let the component and table create embedded views
165
+ trackByFixture . detectChanges ( ) ; // Let the cells render
166
166
167
167
// Each row receives an attribute 'initialIndex' the element's original place
168
168
getRows ( tableElement ) . forEach ( ( row : Element , index : number ) => {
@@ -179,7 +179,7 @@ describe('CdkTable', () => {
179
179
// Swap first two elements, remove the third, add new data
180
180
function mutateData ( ) {
181
181
// Swap first and second data in data array
182
- const copiedData = component . dataSource . data . slice ( ) ;
182
+ const copiedData = trackByComponent . dataSource . data . slice ( ) ;
183
183
const temp = copiedData [ 0 ] ;
184
184
copiedData [ 0 ] = copiedData [ 1 ] ;
185
185
copiedData [ 1 ] = temp ;
@@ -188,12 +188,12 @@ describe('CdkTable', () => {
188
188
copiedData . splice ( 2 , 1 ) ;
189
189
190
190
// Add new data
191
- component . dataSource . data = copiedData ;
192
- component . dataSource . addData ( ) ;
191
+ trackByComponent . dataSource . data = copiedData ;
192
+ trackByComponent . dataSource . addData ( ) ;
193
193
}
194
194
195
195
it ( 'should add/remove/move rows with reference-based trackBy' , ( ) => {
196
- createTestComponentWithTrackyByTable ( ) ;
196
+ createTestComponentWithTrackyByTable ( 'reference' ) ;
197
197
mutateData ( ) ;
198
198
199
199
// Expect that the first and second rows were swapped and that the last row is new
@@ -205,11 +205,11 @@ describe('CdkTable', () => {
205
205
} ) ;
206
206
207
207
it ( 'should add/remove/move rows with changed references without property-based trackBy' , ( ) => {
208
- createTestComponentWithTrackyByTable ( ) ;
208
+ createTestComponentWithTrackyByTable ( 'reference' ) ;
209
209
mutateData ( ) ;
210
210
211
211
// Change each item reference to show that the trackby is not checking the item properties.
212
- component . dataSource . data = component . dataSource . data
212
+ trackByComponent . dataSource . data = trackByComponent . dataSource . data
213
213
. map ( item => { return { a : item . a , b : item . b , c : item . c } ; } ) ;
214
214
215
215
// Expect that all the rows are considered new since their references are all different
@@ -221,13 +221,12 @@ describe('CdkTable', () => {
221
221
} ) ;
222
222
223
223
it ( 'should add/remove/move rows with changed references with property-based trackBy' , ( ) => {
224
- TrackByCdkTableApp . trackBy = 'propertyA' ;
225
- createTestComponentWithTrackyByTable ( ) ;
224
+ createTestComponentWithTrackyByTable ( 'propertyA' ) ;
226
225
mutateData ( ) ;
227
226
228
227
// Change each item reference to show that the trackby is checking the item properties.
229
228
// Otherwise this would cause them all to be removed/added.
230
- component . dataSource . data = component . dataSource . data
229
+ trackByComponent . dataSource . data = trackByComponent . dataSource . data
231
230
. map ( item => { return { a : item . a , b : item . b , c : item . c } ; } ) ;
232
231
233
232
// Expect that the first and second rows were swapped and that the last row is new
@@ -239,13 +238,12 @@ describe('CdkTable', () => {
239
238
} ) ;
240
239
241
240
it ( 'should add/remove/move rows with changed references with index-based trackBy' , ( ) => {
242
- TrackByCdkTableApp . trackBy = 'index' ;
243
- createTestComponentWithTrackyByTable ( ) ;
241
+ createTestComponentWithTrackyByTable ( 'index' ) ;
244
242
mutateData ( ) ;
245
243
246
244
// Change each item reference to show that the trackby is checking the index.
247
245
// Otherwise this would cause them all to be removed/added.
248
- component . dataSource . data = component . dataSource . data
246
+ trackByComponent . dataSource . data = trackByComponent . dataSource . data
249
247
. map ( item => { return { a : item . a , b : item . b , c : item . c } ; } ) ;
250
248
251
249
// Expect first two to be the same since they were swapped but indicies are consistent.
@@ -539,15 +537,15 @@ class DynamicDataSourceCdkTableApp {
539
537
`
540
538
} )
541
539
class TrackByCdkTableApp {
542
- static trackBy : 'reference' | 'propertyA' | 'index' = 'reference' ;
540
+ trackByStrategy : 'reference' | 'propertyA' | 'index' = 'reference' ;
543
541
544
542
dataSource : FakeDataSource = new FakeDataSource ( ) ;
545
543
columnsToRender = [ 'column_a' , 'column_b' ] ;
546
544
547
545
@ViewChild ( CdkTable ) table : CdkTable < TestData > ;
548
546
549
- trackBy ( index : number , item : TestData ) {
550
- switch ( TrackByCdkTableApp . trackBy ) {
547
+ trackBy = ( index : number , item : TestData ) => {
548
+ switch ( this . trackByStrategy ) {
551
549
case 'reference' : return item ;
552
550
case 'propertyA' : return item . a ;
553
551
case 'index' : return index ;
0 commit comments