Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(table): throw error when missing row defs #7751

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/cdk/table/table-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,38 @@
* @docs-private
*/
export function getTableUnknownColumnError(id: string) {
return Error(`cdk-table: Could not find column with id "${id}".`);
return Error(`Could not find column with id "${id}".`);
}

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

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

/**
* Returns an error to be thrown when there are no matching row defs for a particular set of data.
* @docs-private
*/
export function getTableMissingMatchingRowDefError() {
return Error(`cdk-table: Could not find a matching row definition for the provided row data.`);
return Error(`Could not find a matching row definition for the provided row data.`);
}

/**
* Returns an error to be thrown when there is no row definitions present in the content.
* @docs-private
*/
export function getTableMissingRowDefsError() {
return Error('Missing definitions for header and row, ' +
'cannot determine which columns should be rendered.');
}
21 changes: 21 additions & 0 deletions src/cdk/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {map} from 'rxjs/operators';
import {
getTableDuplicateColumnNameError,
getTableMissingMatchingRowDefError,
getTableMissingRowDefsError,
getTableMultipleDefaultRowDefsError,
getTableUnknownColumnError
} from './table-errors';
Expand Down Expand Up @@ -39,6 +40,7 @@ describe('CdkTable', () => {
WhenRowCdkTableApp,
WhenRowWithoutDefaultCdkTableApp,
WhenRowMultipleDefaultsCdkTableApp,
MissingRowDefsCdkTableApp,
BooleanRowCdkTableApp
],
}).compileComponents();
Expand Down Expand Up @@ -158,6 +160,11 @@ describe('CdkTable', () => {
.toThrowError(getTableUnknownColumnError('column_a').message);
});

it('should throw an error if the row definitions are missing', () => {
expect(() => TestBed.createComponent(MissingRowDefsCdkTableApp).detectChanges())
.toThrowError(getTableMissingRowDefsError().message);
});

it('should not throw an error if columns are undefined on initialization', () => {
const undefinedColumnsFixture = TestBed.createComponent(UndefinedColumnsCdkTableApp);
undefinedColumnsFixture.detectChanges();
Expand Down Expand Up @@ -999,6 +1006,20 @@ class MissingColumnDefCdkTableApp {
dataSource: FakeDataSource = new FakeDataSource();
}

@Component({
template: `
<cdk-table [dataSource]="dataSource">
<ng-container cdkColumnDef="column_a">
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
</ng-container>
</cdk-table>
`
})
class MissingRowDefsCdkTableApp {
dataSource: FakeDataSource = new FakeDataSource();
}

@Component({
template: `
<cdk-table [dataSource]="dataSource">
Expand Down
5 changes: 5 additions & 0 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {CdkCellDef, CdkColumnDef, CdkHeaderCellDef} from './cell';
import {
getTableDuplicateColumnNameError,
getTableMissingMatchingRowDefError,
getTableMissingRowDefsError,
getTableMultipleDefaultRowDefsError,
getTableUnknownColumnError
} from './table-errors';
Expand Down Expand Up @@ -182,6 +183,10 @@ export class CdkTable<T> implements CollectionViewer {
}

ngAfterContentInit() {
if (!this._headerDef && !this._rowDefs.length) {
throw getTableMissingRowDefsError();
}

this._cacheColumnDefsByName();
this._columnDefs.changes.subscribe(() => this._cacheColumnDefsByName());
this._renderHeaderRow();
Expand Down