From 55476e2cb96e0a48e4faba07436999caf0d52654 Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Wed, 1 Nov 2017 20:51:44 -0700 Subject: [PATCH] fix(table): throw error when missing row defs (#7751) * fix(table): throw error when missing row defs * nit: change conditional --- 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 7ec1b8c531b..080250c69be 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 b659fefaac1..002b25c19eb 100644 --- a/src/cdk/table/table.spec.ts +++ b/src/cdk/table/table.spec.ts @@ -10,6 +10,7 @@ import {map} from 'rxjs/operators/map'; 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(); @@ -998,6 +1005,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 cb0aa60128b..cbc944fa058 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) { + throw getTableMissingRowDefsError(); + } + this._cacheColumnDefsByName(); this._columnDefs.changes.subscribe(() => this._cacheColumnDefsByName()); this._renderHeaderRow();