Skip to content

Commit f2ceec9

Browse files
committed
tests
1 parent a47b961 commit f2ceec9

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();
@@ -107,6 +108,16 @@ fdescribe('CdkTable', () => {
107108
expect(fixture.nativeElement.querySelector('cdk-table').getAttribute('role')).toBe('treegrid');
108109
});
109110

111+
fit('should be able to dynamically add/remove column definitions', () => {
112+
const dynamicColumnDefFixture = TestBed.createComponent(DynamicColumnDefinitionsCdkTableApp);
113+
dynamicColumnDefFixture.detectChanges();
114+
115+
const dynamicColumnDefComp = dynamicColumnDefFixture.componentInstance;
116+
expect(dynamicColumnDefComp.dynamicColumns.push('columnA'));
117+
118+
dynamicColumnDefFixture.detectChanges();
119+
});
120+
110121
it('should re-render the rows when the data changes', () => {
111122
dataSource.addData();
112123
fixture.detectChanges();
@@ -590,34 +601,25 @@ class TrackByCdkTableApp {
590601
@Component({
591602
template: `
592603
<cdk-table [dataSource]="dataSource">
593-
<ng-container [cdkColumnDef]="column.id" *ngFor="let column of dynamicColumnDefs">
594-
<cdk-header-cell *cdkHeaderCellDef> {{column.headerText}} </cdk-header-cell>
595-
<cdk-cell *cdkCellDef="let row"> {{row[column.property]}} </cdk-cell>
604+
<ng-container [cdkColumnDef]="column" *ngFor="let column of dynamicColumns">
605+
<cdk-header-cell *cdkHeaderCellDef> {{column}}</cdk-header-cell>
606+
<cdk-cell *cdkCellDef="let row"> {{column}}</cdk-cell>
596607
</ng-container>
597608
598-
<cdk-header-row *cdkHeaderRowDef="dynamicColumnIds"></cdk-header-row>
599-
<cdk-row *cdkRowDef="let row; columns: dynamicColumnIds;"></cdk-row>
609+
<cdk-header-row *cdkHeaderRowDef="dynamicColumns"></cdk-header-row>
610+
<cdk-row *cdkRowDef="let row; columns: dynamicColumns;"></cdk-row>
600611
</cdk-table>
601612
`
602613
})
603614
class DynamicColumnDefinitionsCdkTableApp {
604-
dynamicColumnDefs: any[] = [];
605-
dynamicColumnIds: string[] = [];
615+
dynamicColumns: any[] = [];
606616

607617
dataSource: FakeDataSource = new FakeDataSource();
608-
columnsToRender = ['column_a', 'column_b'];
609618

610619
@ViewChild(CdkTable) table: CdkTable<TestData>;
611620

612621
addDynamicColumnDef() {
613-
const nextProperty = this.dynamicColumnDefs.length;
614-
this.dynamicColumnDefs.push({
615-
id: nextProperty,
616-
property: nextProperty,
617-
headerText: nextProperty
618-
});
619-
620-
this.dynamicColumnIds = this.dynamicColumnDefs.map(columnDef => columnDef.id);
622+
this.dynamicColumns.push(this.dynamicColumns.length);
621623
}
622624
}
623625

src/cdk/table/table.ts

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

192192
ngAfterContentChecked() {
193+
console.log('Content checked');
193194
this._updateColumnDefinitions();
194195
}
195196

@@ -224,6 +225,7 @@ export class CdkTable<T> implements CollectionViewer {
224225

225226
/** Update the map containing the content's column definitions. */
226227
private _updateColumnDefinitions() {
228+
console.log('Updating columns');
227229
this._columnDefinitionsByName.clear();
228230
this._columnDefinitions.forEach(columnDef => {
229231
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)