Skip to content

Commit

Permalink
fix(table): throw error when missing row defs (#7751)
Browse files Browse the repository at this point in the history
* fix(table): throw error when missing row defs

* nit: change conditional
  • Loading branch information
andrewseguin committed Nov 2, 2017
1 parent 2347f5b commit 55476e2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/cdk/table/table-errors.ts
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
Expand Up @@ -10,6 +10,7 @@ import {map} from 'rxjs/operators/map';
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 @@ -998,6 +1005,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
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

0 comments on commit 55476e2

Please sign in to comment.