Skip to content

Commit 5267502

Browse files
committed
comments
1 parent 379824a commit 5267502

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

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

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

6-
<button md-raised-button (click)="_peopleDatabase.shuffle(changeReferences)">Randomize Data</button>
7-
<md-checkbox [(ngModel)]="changeReferences">Change references</md-checkbox>
8-
</div>
9-
10-
<span>Track By</span>
11-
<md-radio-group [(ngModel)]="trackBy">
12-
<md-radio-button [value]="'id'">ID</md-radio-button>
13-
<md-radio-button [value]="'reference'">Reference</md-radio-button>
14-
<md-radio-button [value]="'index'">Index</md-radio-button>
15-
</md-radio-group>
6+
<button md-raised-button (click)="_peopleDatabase.shuffle(changeReferences)">Randomize Data</button>
7+
<md-checkbox [(ngModel)]="changeReferences">Change references</md-checkbox>
168

17-
<div class="demo-data-source-actions">
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>
1815
</div>
1916

2017
<div class="demo-highlighter">

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
@@ -146,14 +146,14 @@ describe('CdkTable', () => {
146146
expect(changedRows[2].getAttribute('initialIndex')).toBe(null);
147147
});
148148

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

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

156-
function createComponent() {
156+
function createTestComponentWithTrackyByTable() {
157157
fixture = TestBed.createComponent(TrackByCdkTableApp);
158158

159159
component = fixture.componentInstance;
@@ -177,7 +177,7 @@ describe('CdkTable', () => {
177177
}
178178

179179
// Swap first two elements, remove the third, add new data
180-
function changeData() {
180+
function mutateData() {
181181
// Swap first and second data in data array
182182
const copiedData = component.dataSource.data.slice();
183183
const temp = copiedData[0];
@@ -192,9 +192,9 @@ describe('CdkTable', () => {
192192
component.dataSource.addData();
193193
}
194194

195-
it('with reference-based trackBy', () => {
196-
createComponent();
197-
changeData();
195+
it('should add/remove/move rows with reference-based trackBy', () => {
196+
createTestComponentWithTrackyByTable();
197+
mutateData();
198198

199199
// Expect that the first and second rows were swapped and that the last row is new
200200
const changedRows = getRows(tableElement);
@@ -204,9 +204,9 @@ describe('CdkTable', () => {
204204
expect(changedRows[2].getAttribute('initialIndex')).toBe(null);
205205
});
206206

207-
it('with changed references without property-based trackBy', () => {
208-
createComponent();
209-
changeData();
207+
it('should add/remove/move rows with changed references without property-based trackBy', () => {
208+
createTestComponentWithTrackyByTable();
209+
mutateData();
210210

211211
// Change each item reference to show that the trackby is not checking the item properties.
212212
component.dataSource.data = component.dataSource.data
@@ -220,10 +220,10 @@ describe('CdkTable', () => {
220220
expect(changedRows[2].getAttribute('initialIndex')).toBe(null);
221221
});
222222

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

228228
// Change each item reference to show that the trackby is checking the item properties.
229229
// Otherwise this would cause them all to be removed/added.
@@ -238,10 +238,10 @@ describe('CdkTable', () => {
238238
expect(changedRows[2].getAttribute('initialIndex')).toBe(null);
239239
});
240240

241-
it('with changed references with index-based trackBy', () => {
242-
TrackByCdkTableApp.TRACK_BY = 'index';
243-
createComponent();
244-
changeData();
241+
it('should add/remove/move rows with changed references with index-based trackBy', () => {
242+
TrackByCdkTableApp.trackBy = 'index';
243+
createTestComponentWithTrackyByTable();
244+
mutateData();
245245

246246
// Change each item reference to show that the trackby is checking the index.
247247
// Otherwise this would cause them all to be removed/added.
@@ -539,15 +539,15 @@ class DynamicDataSourceCdkTableApp {
539539
`
540540
})
541541
class TrackByCdkTableApp {
542-
static TRACK_BY: 'reference' | 'propertyA' | 'index' = 'reference';
542+
static trackBy: 'reference' | 'propertyA' | 'index' = 'reference';
543543

544544
dataSource: FakeDataSource = new FakeDataSource();
545545
columnsToRender = ['column_a', 'column_b'];
546546

547547
@ViewChild(CdkTable) table: CdkTable<TestData>;
548548

549549
trackBy(index: number, item: TestData) {
550-
switch (TrackByCdkTableApp.TRACK_BY) {
550+
switch (TrackByCdkTableApp.trackBy) {
551551
case 'reference': return item;
552552
case 'propertyA': return item.a;
553553
case 'index': return index;

0 commit comments

Comments
 (0)