Skip to content

Commit 86e2a91

Browse files
committed
comments
1 parent 9ab17f8 commit 86e2a91

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

src/demo-app/data-table/data-table-demo.html

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
<div class="demo-database-actions">
1+
<div class="demo-data-source-actions">
22
<button md-raised-button (click)="connect()">Connect New Data Source</button>
33
<button md-raised-button (click)="disconnect()" [disabled]="!dataSource">Disconnect Data Source</button>
44

55
<button md-raised-button (click)="_peopleDatabase.shuffle(changeReferences)">Randomize Data</button>
66
<md-checkbox [(ngModel)]="changeReferences">Change references</md-checkbox>
7-
</div>
87

9-
<span>Track By</span>
10-
<md-radio-group [(ngModel)]="trackBy">
11-
<md-radio-button [value]="'id'">ID</md-radio-button>
12-
<md-radio-button [value]="'reference'">Reference</md-radio-button>
13-
<md-radio-button [value]="'index'">Index</md-radio-button>
14-
</md-radio-group>
15-
16-
<div class="demo-data-source-actions">
8+
<span>Track By</span>
9+
<md-radio-group [(ngModel)]="trackBy">
10+
<md-radio-button [value]="'id'">ID</md-radio-button>
11+
<md-radio-button [value]="'reference'">Reference</md-radio-button>
12+
<md-radio-button [value]="'index'">Index</md-radio-button>
13+
</md-radio-group>
1714
</div>
1815

1916
<div class="demo-table-container mat-elevation-z4">

src/demo-app/data-table/people-database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class PeopleDatabase {
2828
for (let i = 0; i < 100; i++) { this.addPerson(); }
2929
}
3030

31-
randomize(changeReferences: boolean) {
31+
shuffle(changeReferences: boolean) {
3232
let copiedData = this.data.slice();
3333
for (let i = copiedData.length; i; i--) {
3434
let j = Math.floor(Math.random() * i);

src/lib/core/data-table/data-table.spec.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ describe('CdkTable', () => {
143143
expect(changedRows[2].getAttribute('initialIndex')).toBe(null);
144144
});
145145

146-
describe('should properly use trackBy when diffing to add/remove/move rows', () => {
146+
describe('with trackBy', () => {
147147

148148
afterEach(() => {
149149
// Resetting the static value of the trackby function for TrackByCdkTableApp
150-
TrackByCdkTableApp.TRACK_BY = 'reference';
150+
TrackByCdkTableApp.trackBy = 'reference';
151151
});
152152

153-
function createComponent() {
153+
function createTestComponentWithTrackyByTable() {
154154
fixture = TestBed.createComponent(TrackByCdkTableApp);
155155

156156
component = fixture.componentInstance;
@@ -174,7 +174,7 @@ describe('CdkTable', () => {
174174
}
175175

176176
// Swap first two elements, remove the third, add new data
177-
function changeData() {
177+
function mutateData() {
178178
// Swap first and second data in data array
179179
const copiedData = component.dataSource.data.slice();
180180
const temp = copiedData[0];
@@ -189,9 +189,9 @@ describe('CdkTable', () => {
189189
component.dataSource.addData();
190190
}
191191

192-
it('with reference-based trackBy', () => {
193-
createComponent();
194-
changeData();
192+
it('should add/remove/move rows with reference-based trackBy', () => {
193+
createTestComponentWithTrackyByTable();
194+
mutateData();
195195

196196
// Expect that the first and second rows were swapped and that the last row is new
197197
const changedRows = getRows(tableElement);
@@ -201,9 +201,9 @@ describe('CdkTable', () => {
201201
expect(changedRows[2].getAttribute('initialIndex')).toBe(null);
202202
});
203203

204-
it('with changed references without property-based trackBy', () => {
205-
createComponent();
206-
changeData();
204+
it('should add/remove/move rows with changed references without property-based trackBy', () => {
205+
createTestComponentWithTrackyByTable();
206+
mutateData();
207207

208208
// Change each item reference to show that the trackby is not checking the item properties.
209209
component.dataSource.data = component.dataSource.data
@@ -217,10 +217,10 @@ describe('CdkTable', () => {
217217
expect(changedRows[2].getAttribute('initialIndex')).toBe(null);
218218
});
219219

220-
it('with changed references with property-based trackBy', () => {
221-
TrackByCdkTableApp.TRACK_BY = 'propertyA';
222-
createComponent();
223-
changeData();
220+
it('should add/remove/move rows with changed references with property-based trackBy', () => {
221+
TrackByCdkTableApp.trackBy = 'propertyA';
222+
createTestComponentWithTrackyByTable();
223+
mutateData();
224224

225225
// Change each item reference to show that the trackby is checking the item properties.
226226
// Otherwise this would cause them all to be removed/added.
@@ -235,10 +235,10 @@ describe('CdkTable', () => {
235235
expect(changedRows[2].getAttribute('initialIndex')).toBe(null);
236236
});
237237

238-
it('with changed references with index-based trackBy', () => {
239-
TrackByCdkTableApp.TRACK_BY = 'index';
240-
createComponent();
241-
changeData();
238+
it('should add/remove/move rows with changed references with index-based trackBy', () => {
239+
TrackByCdkTableApp.trackBy = 'index';
240+
createTestComponentWithTrackyByTable();
241+
mutateData();
242242

243243
// Change each item reference to show that the trackby is checking the index.
244244
// Otherwise this would cause them all to be removed/added.
@@ -453,15 +453,15 @@ class DynamicDataSourceCdkTableApp {
453453
`
454454
})
455455
class TrackByCdkTableApp {
456-
static TRACK_BY: 'reference' | 'propertyA' | 'index' = 'reference';
456+
static trackBy: 'reference' | 'propertyA' | 'index' = 'reference';
457457

458458
dataSource: FakeDataSource = new FakeDataSource();
459459
columnsToRender = ['column_a', 'column_b'];
460460

461461
@ViewChild(CdkTable) table: CdkTable<TestData>;
462462

463463
trackBy(index: number, item: TestData) {
464-
switch (TrackByCdkTableApp.TRACK_BY) {
464+
switch (TrackByCdkTableApp.trackBy) {
465465
case 'reference': return item;
466466
case 'propertyA': return item.a;
467467
case 'index': return index;

0 commit comments

Comments
 (0)