Skip to content

Commit 781953e

Browse files
committed
tests
1 parent da2daec commit 781953e

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/cdk/table/table.spec.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fdescribe('CdkTable', () => {
2424
DynamicDataSourceCdkTableApp,
2525
CustomRoleCdkTableApp,
2626
TrackByCdkTableApp,
27+
DynamicColumnDefinitionsCdkTableApp,
2728
RowContextCdkTableApp,
2829
],
2930
}).compileComponents();
@@ -108,6 +109,16 @@ fdescribe('CdkTable', () => {
108109
expect(fixture.nativeElement.querySelector('cdk-table').getAttribute('role')).toBe('treegrid');
109110
});
110111

112+
fit('should be able to dynamically add/remove column definitions', () => {
113+
const dynamicColumnDefFixture = TestBed.createComponent(DynamicColumnDefinitionsCdkTableApp);
114+
dynamicColumnDefFixture.detectChanges();
115+
116+
const dynamicColumnDefComp = dynamicColumnDefFixture.componentInstance;
117+
expect(dynamicColumnDefComp.dynamicColumns.push('columnA'));
118+
119+
dynamicColumnDefFixture.detectChanges();
120+
});
121+
111122
it('should re-render the rows when the data changes', () => {
112123
dataSource.addData();
113124
fixture.detectChanges();
@@ -587,34 +598,25 @@ class TrackByCdkTableApp {
587598
@Component({
588599
template: `
589600
<cdk-table [dataSource]="dataSource">
590-
<ng-container [cdkColumnDef]="column.id" *ngFor="let column of dynamicColumnDefs">
591-
<cdk-header-cell *cdkHeaderCellDef> {{column.headerText}} </cdk-header-cell>
592-
<cdk-cell *cdkCellDef="let row"> {{row[column.property]}} </cdk-cell>
601+
<ng-container [cdkColumnDef]="column" *ngFor="let column of dynamicColumns">
602+
<cdk-header-cell *cdkHeaderCellDef> {{column}}</cdk-header-cell>
603+
<cdk-cell *cdkCellDef="let row"> {{column}}</cdk-cell>
593604
</ng-container>
594605
595-
<cdk-header-row *cdkHeaderRowDef="dynamicColumnIds"></cdk-header-row>
596-
<cdk-row *cdkRowDef="let row; columns: dynamicColumnIds;"></cdk-row>
606+
<cdk-header-row *cdkHeaderRowDef="dynamicColumns"></cdk-header-row>
607+
<cdk-row *cdkRowDef="let row; columns: dynamicColumns;"></cdk-row>
597608
</cdk-table>
598609
`
599610
})
600611
class DynamicColumnDefinitionsCdkTableApp {
601-
dynamicColumnDefs: any[] = [];
602-
dynamicColumnIds: string[] = [];
612+
dynamicColumns: any[] = [];
603613

604614
dataSource: FakeDataSource = new FakeDataSource();
605-
columnsToRender = ['column_a', 'column_b'];
606615

607616
@ViewChild(CdkTable) table: CdkTable<TestData>;
608617

609618
addDynamicColumnDef() {
610-
const nextProperty = this.dynamicColumnDefs.length;
611-
this.dynamicColumnDefs.push({
612-
id: nextProperty,
613-
property: nextProperty,
614-
headerText: nextProperty
615-
});
616-
617-
this.dynamicColumnIds = this.dynamicColumnDefs.map(columnDef => columnDef.id);
619+
this.dynamicColumns.push(this.dynamicColumns.length);
618620
}
619621
}
620622

src/cdk/table/table.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export class CdkTable<T> implements CollectionViewer {
195195
}
196196

197197
ngAfterContentChecked() {
198+
console.log('Content checked');
198199
this._checkColumnsChange();
199200
}
200201

@@ -221,6 +222,7 @@ export class CdkTable<T> implements CollectionViewer {
221222

222223
/** Update the map containing the content's column definitions. */
223224
private _updateColumnDefinitions() {
225+
console.log('Updating columns');
224226
this._columnDefinitionsByName.clear();
225227
this._columnDefinitions.forEach(columnDef => {
226228
if (this._columnDefinitionsByName.has(columnDef.name)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<h3>CdkTable With Dynamic Column Def</h3>
3737

3838
<cdk-table [dataSource]="dataSource">
39-
<ng-container [cdkColumnDef]="column.id" *ngFor="let column of dynamicColumnDefs">
39+
<ng-container [cdkColumnDef]="column.id" *ngFor="let column of dynamicColumns">
4040
<cdk-header-cell *cdkHeaderCellDef> {{column.headerText}} </cdk-header-cell>
4141
<cdk-cell *cdkCellDef="let row"> {{row[column.property]}} </cdk-cell>
4242
</ng-container>

0 commit comments

Comments
 (0)