From 254fb49b1fc78af04678e6750ee5a2d11a5a42ee Mon Sep 17 00:00:00 2001 From: crisbeto Date: Thu, 23 May 2019 19:28:03 +0200 Subject: [PATCH] fix(table): better error message if text column is missing a name Currently if the consumer forgot to set a name on a text column, we throw a cryptic error somewhere down the line. These changes add a proper error message. --- src/cdk/table/table-errors.ts | 8 ++++++++ src/cdk/table/text-column.spec.ts | 26 +++++++++++++++++++++++++- src/cdk/table/text-column.ts | 20 +++++++++++++++----- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/cdk/table/table-errors.ts b/src/cdk/table/table-errors.ts index 4351e7a6bfd..779cc9a0a7a 100644 --- a/src/cdk/table/table-errors.ts +++ b/src/cdk/table/table-errors.ts @@ -64,3 +64,11 @@ export function getTableUnknownDataSourceError() { export function getTableTextColumnMissingParentTableError() { return Error(`Text column could not find a parent table for registration.`); } + +/** + * Returns an error to be thrown when a table text column doesn't have a name. + * @docs-private + */ +export function getTableTextColumnMissingNameError() { + return Error(`Table text column must have a name.`); +} diff --git a/src/cdk/table/text-column.spec.ts b/src/cdk/table/text-column.spec.ts index 31f7597ff6d..13422e02cf1 100644 --- a/src/cdk/table/text-column.spec.ts +++ b/src/cdk/table/text-column.spec.ts @@ -1,7 +1,10 @@ import {Component} from '@angular/core'; import {async, ComponentFixture, TestBed} from '@angular/core/testing'; -import {getTableTextColumnMissingParentTableError} from './table-errors'; +import { + getTableTextColumnMissingParentTableError, + getTableTextColumnMissingNameError, +} from './table-errors'; import {CdkTableModule} from './table-module'; import {expectTableToMatchContent} from './table.spec'; import {TEXT_COLUMN_OPTIONS, TextColumnOptions} from './text-column'; @@ -19,6 +22,7 @@ describe('CdkTextColumn', () => { declarations: [ BasicTextColumnApp, MissingTableApp, + TextColumnWithoutNameApp, ], }) .compileComponents(); @@ -45,6 +49,11 @@ describe('CdkTextColumn', () => { .toThrowError(getTableTextColumnMissingParentTableError().message); }); + it('should throw an error if the text column does not have a name', () => { + expect(() => TestBed.createComponent(TextColumnWithoutNameApp).detectChanges()) + .toThrowError(getTableTextColumnMissingNameError().message); + }); + it('should allow for alternate header text', () => { component.headerTextB = 'column-b'; fixture.detectChanges(); @@ -182,3 +191,18 @@ class BasicTextColumnApp { }) class MissingTableApp { } + + +@Component({ + template: ` + + + + + + + ` +}) +class TextColumnWithoutNameApp extends BasicTextColumnApp { +} + diff --git a/src/cdk/table/text-column.ts b/src/cdk/table/text-column.ts index aa37a38997c..461ea765a85 100644 --- a/src/cdk/table/text-column.ts +++ b/src/cdk/table/text-column.ts @@ -16,11 +16,15 @@ import { OnInit, Optional, ViewChild, - ViewEncapsulation + ViewEncapsulation, + isDevMode, } from '@angular/core'; import {CdkCellDef, CdkColumnDef, CdkHeaderCellDef} from './cell'; import {CdkTable} from './table'; -import {getTableTextColumnMissingParentTableError} from './table-errors'; +import { + getTableTextColumnMissingParentTableError, + getTableTextColumnMissingNameError, +} from './table-errors'; /** Configurable options for `CdkTextColumn`. */ @@ -164,11 +168,17 @@ export class CdkTextColumn implements OnDestroy, OnInit { * has been provided. Otherwise simply capitalize the column name. */ _createDefaultHeaderText() { - if (this._options && this._options.defaultHeaderTextTransform) { - return this._options.defaultHeaderTextTransform(this.name); + const name = this.name; + + if (isDevMode() && !name) { + throw getTableTextColumnMissingNameError(); } - return this.name[0].toUpperCase() + this.name.slice(1); + if (this._options && this._options.defaultHeaderTextTransform) { + return this._options.defaultHeaderTextTransform(name); + } + + return name[0].toUpperCase() + name.slice(1); } /** Synchronizes the column definition name with the text column name. */