Skip to content

Commit 55476e2

Browse files
authored
fix(table): throw error when missing row defs (#7751)
* fix(table): throw error when missing row defs * nit: change conditional
1 parent 2347f5b commit 55476e2

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/cdk/table/table-errors.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,38 @@
1212
* @docs-private
1313
*/
1414
export function getTableUnknownColumnError(id: string) {
15-
return Error(`cdk-table: Could not find column with id "${id}".`);
15+
return Error(`Could not find column with id "${id}".`);
1616
}
1717

1818
/**
1919
* Returns an error to be thrown when two column definitions have the same name.
2020
* @docs-private
2121
*/
2222
export function getTableDuplicateColumnNameError(name: string) {
23-
return Error(`cdk-table: Duplicate column definition name provided: "${name}".`);
23+
return Error(`Duplicate column definition name provided: "${name}".`);
2424
}
2525

2626
/**
2727
* Returns an error to be thrown when there are multiple rows that are missing a when function.
2828
* @docs-private
2929
*/
3030
export function getTableMultipleDefaultRowDefsError() {
31-
return Error(`cdk-table: There can only be one default row without a when predicate function.`);
31+
return Error(`There can only be one default row without a when predicate function.`);
3232
}
3333

3434
/**
3535
* Returns an error to be thrown when there are no matching row defs for a particular set of data.
3636
* @docs-private
3737
*/
3838
export function getTableMissingMatchingRowDefError() {
39-
return Error(`cdk-table: Could not find a matching row definition for the provided row data.`);
39+
return Error(`Could not find a matching row definition for the provided row data.`);
40+
}
41+
42+
/**
43+
* Returns an error to be thrown when there is no row definitions present in the content.
44+
* @docs-private
45+
*/
46+
export function getTableMissingRowDefsError() {
47+
return Error('Missing definitions for header and row, ' +
48+
'cannot determine which columns should be rendered.');
4049
}

src/cdk/table/table.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {map} from 'rxjs/operators/map';
1010
import {
1111
getTableDuplicateColumnNameError,
1212
getTableMissingMatchingRowDefError,
13+
getTableMissingRowDefsError,
1314
getTableMultipleDefaultRowDefsError,
1415
getTableUnknownColumnError
1516
} from './table-errors';
@@ -39,6 +40,7 @@ describe('CdkTable', () => {
3940
WhenRowCdkTableApp,
4041
WhenRowWithoutDefaultCdkTableApp,
4142
WhenRowMultipleDefaultsCdkTableApp,
43+
MissingRowDefsCdkTableApp,
4244
BooleanRowCdkTableApp
4345
],
4446
}).compileComponents();
@@ -158,6 +160,11 @@ describe('CdkTable', () => {
158160
.toThrowError(getTableUnknownColumnError('column_a').message);
159161
});
160162

163+
it('should throw an error if the row definitions are missing', () => {
164+
expect(() => TestBed.createComponent(MissingRowDefsCdkTableApp).detectChanges())
165+
.toThrowError(getTableMissingRowDefsError().message);
166+
});
167+
161168
it('should not throw an error if columns are undefined on initialization', () => {
162169
const undefinedColumnsFixture = TestBed.createComponent(UndefinedColumnsCdkTableApp);
163170
undefinedColumnsFixture.detectChanges();
@@ -998,6 +1005,20 @@ class MissingColumnDefCdkTableApp {
9981005
dataSource: FakeDataSource = new FakeDataSource();
9991006
}
10001007

1008+
@Component({
1009+
template: `
1010+
<cdk-table [dataSource]="dataSource">
1011+
<ng-container cdkColumnDef="column_a">
1012+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
1013+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
1014+
</ng-container>
1015+
</cdk-table>
1016+
`
1017+
})
1018+
class MissingRowDefsCdkTableApp {
1019+
dataSource: FakeDataSource = new FakeDataSource();
1020+
}
1021+
10011022
@Component({
10021023
template: `
10031024
<cdk-table [dataSource]="dataSource">

src/cdk/table/table.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {CdkCellDef, CdkColumnDef, CdkHeaderCellDef} from './cell';
3939
import {
4040
getTableDuplicateColumnNameError,
4141
getTableMissingMatchingRowDefError,
42+
getTableMissingRowDefsError,
4243
getTableMultipleDefaultRowDefsError,
4344
getTableUnknownColumnError
4445
} from './table-errors';
@@ -182,6 +183,10 @@ export class CdkTable<T> implements CollectionViewer {
182183
}
183184

184185
ngAfterContentInit() {
186+
if (!this._headerDef && !this._rowDefs.length) {
187+
throw getTableMissingRowDefsError();
188+
}
189+
185190
this._cacheColumnDefsByName();
186191
this._columnDefs.changes.subscribe(() => this._cacheColumnDefsByName());
187192
this._renderHeaderRow();

0 commit comments

Comments
 (0)