Skip to content

Commit e7d8706

Browse files
committed
tests
1 parent bf8d820 commit e7d8706

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();
@@ -574,34 +585,25 @@ class TrackByCdkTableApp {
574585
@Component({
575586
template: `
576587
<cdk-table [dataSource]="dataSource">
577-
<ng-container [cdkColumnDef]="column.id" *ngFor="let column of dynamicColumnDefs">
578-
<cdk-header-cell *cdkHeaderCellDef> {{column.headerText}} </cdk-header-cell>
579-
<cdk-cell *cdkCellDef="let row"> {{row[column.property]}} </cdk-cell>
588+
<ng-container [cdkColumnDef]="column" *ngFor="let column of dynamicColumns">
589+
<cdk-header-cell *cdkHeaderCellDef> {{column}}</cdk-header-cell>
590+
<cdk-cell *cdkCellDef="let row"> {{column}}</cdk-cell>
580591
</ng-container>
581592
582-
<cdk-header-row *cdkHeaderRowDef="dynamicColumnIds"></cdk-header-row>
583-
<cdk-row *cdkRowDef="let row; columns: dynamicColumnIds;"></cdk-row>
593+
<cdk-header-row *cdkHeaderRowDef="dynamicColumns"></cdk-header-row>
594+
<cdk-row *cdkRowDef="let row; columns: dynamicColumns;"></cdk-row>
584595
</cdk-table>
585596
`
586597
})
587598
class DynamicColumnDefinitionsCdkTableApp {
588-
dynamicColumnDefs: any[] = [];
589-
dynamicColumnIds: string[] = [];
599+
dynamicColumns: any[] = [];
590600

591601
dataSource: FakeDataSource = new FakeDataSource();
592-
columnsToRender = ['column_a', 'column_b'];
593602

594603
@ViewChild(CdkTable) table: CdkTable<TestData>;
595604

596605
addDynamicColumnDef() {
597-
const nextProperty = this.dynamicColumnDefs.length;
598-
this.dynamicColumnDefs.push({
599-
id: nextProperty,
600-
property: nextProperty,
601-
headerText: nextProperty
602-
});
603-
604-
this.dynamicColumnIds = this.dynamicColumnDefs.map(columnDef => columnDef.id);
606+
this.dynamicColumns.push(this.dynamicColumns.length);
605607
}
606608
}
607609

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

@@ -218,6 +219,7 @@ export class CdkTable<T> implements CollectionViewer {
218219

219220
/** Update the map containing the content's column definitions. */
220221
private _updateColumnDefinitions() {
222+
console.log('Updating columns');
221223
this._columnDefinitionsByName.clear();
222224
this._columnDefinitions.forEach(columnDef => {
223225
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)