From 11c17cf5d38fbeeed41383b40b80370acf62b3ad Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Thu, 12 Oct 2017 14:46:23 -0700 Subject: [PATCH 1/2] fix(table): throw error when missing row defs --- src/cdk/table/table-errors.ts | 17 +++++++++++++---- src/cdk/table/table.spec.ts | 21 +++++++++++++++++++++ src/cdk/table/table.ts | 5 +++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/cdk/table/table-errors.ts b/src/cdk/table/table-errors.ts index 7ec1b8c531bc..080250c69beb 100644 --- a/src/cdk/table/table-errors.ts +++ b/src/cdk/table/table-errors.ts @@ -12,7 +12,7 @@ * @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}".`); } /** @@ -20,7 +20,7 @@ export function getTableUnknownColumnError(id: string) { * @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}".`); } /** @@ -28,7 +28,7 @@ export function getTableDuplicateColumnNameError(name: string) { * @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.`); } /** @@ -36,5 +36,14 @@ export function getTableMultipleDefaultRowDefsError() { * @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.'); } diff --git a/src/cdk/table/table.spec.ts b/src/cdk/table/table.spec.ts index da27d0300ca6..e17d6d8be7a5 100644 --- a/src/cdk/table/table.spec.ts +++ b/src/cdk/table/table.spec.ts @@ -10,6 +10,7 @@ import {map} from 'rxjs/operators'; import { getTableDuplicateColumnNameError, getTableMissingMatchingRowDefError, + getTableMissingRowDefsError, getTableMultipleDefaultRowDefsError, getTableUnknownColumnError } from './table-errors'; @@ -39,6 +40,7 @@ describe('CdkTable', () => { WhenRowCdkTableApp, WhenRowWithoutDefaultCdkTableApp, WhenRowMultipleDefaultsCdkTableApp, + MissingRowDefsCdkTableApp, BooleanRowCdkTableApp ], }).compileComponents(); @@ -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(); @@ -999,6 +1006,20 @@ class MissingColumnDefCdkTableApp { dataSource: FakeDataSource = new FakeDataSource(); } +@Component({ + template: ` + + + Column A + {{row.a}} + + + ` +}) +class MissingRowDefsCdkTableApp { + dataSource: FakeDataSource = new FakeDataSource(); +} + @Component({ template: ` diff --git a/src/cdk/table/table.ts b/src/cdk/table/table.ts index b2df1ea5ccaf..12dec2b89bf0 100644 --- a/src/cdk/table/table.ts +++ b/src/cdk/table/table.ts @@ -39,6 +39,7 @@ import {CdkCellDef, CdkColumnDef, CdkHeaderCellDef} from './cell'; import { getTableDuplicateColumnNameError, getTableMissingMatchingRowDefError, + getTableMissingRowDefsError, getTableMultipleDefaultRowDefsError, getTableUnknownColumnError } from './table-errors'; @@ -182,6 +183,10 @@ export class CdkTable implements CollectionViewer { } ngAfterContentInit() { + if (!this._headerDef && this._rowDefs.length == 0) { + throw getTableMissingRowDefsError(); + } + this._cacheColumnDefsByName(); this._columnDefs.changes.subscribe(() => this._cacheColumnDefsByName()); this._renderHeaderRow(); From cd4d34db02f707ccbd20f39d7719f22f835ccc71 Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Fri, 13 Oct 2017 10:53:24 -0700 Subject: [PATCH 2/2] nit: change conditional --- src/cdk/table/table.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cdk/table/table.ts b/src/cdk/table/table.ts index 12dec2b89bf0..f424fb316366 100644 --- a/src/cdk/table/table.ts +++ b/src/cdk/table/table.ts @@ -183,7 +183,7 @@ export class CdkTable implements CollectionViewer { } ngAfterContentInit() { - if (!this._headerDef && this._rowDefs.length == 0) { + if (!this._headerDef && !this._rowDefs.length) { throw getTableMissingRowDefsError(); }