Skip to content

Commit

Permalink
fix(table): better error message if text column is missing a name
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
crisbeto authored and mmalerba committed May 23, 2019
1 parent 92448bc commit 254fb49
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/cdk/table/table-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
}
26 changes: 25 additions & 1 deletion src/cdk/table/text-column.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -19,6 +22,7 @@ describe('CdkTextColumn', () => {
declarations: [
BasicTextColumnApp,
MissingTableApp,
TextColumnWithoutNameApp,
],
})
.compileComponents();
Expand All @@ -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();
Expand Down Expand Up @@ -182,3 +191,18 @@ class BasicTextColumnApp {
})
class MissingTableApp {
}


@Component({
template: `
<cdk-table [dataSource]="data">
<cdk-text-column [dataAccessor]="dataAccessorA"></cdk-text-column>
<cdk-header-row *cdkHeaderRowDef="displayedColumns"></cdk-header-row>
<cdk-row *cdkRowDef="let row; columns: displayedColumns"></cdk-row>
</cdk-table>
`
})
class TextColumnWithoutNameApp extends BasicTextColumnApp {
}

18 changes: 14 additions & 4 deletions src/cdk/table/text-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`. */
Expand Down Expand Up @@ -164,11 +168,17 @@ export class CdkTextColumn<T> implements OnDestroy, OnInit {
* has been provided. Otherwise simply capitalize the column name.
*/
_createDefaultHeaderText() {
const name = this.name;

if (isDevMode() && !name) {
throw getTableTextColumnMissingNameError();
}

if (this._options && this._options.defaultHeaderTextTransform) {
return this._options.defaultHeaderTextTransform(this.name);
return this._options.defaultHeaderTextTransform(name);
}

return this.name[0].toUpperCase() + this.name.slice(1);
return name[0].toUpperCase() + name.slice(1);
}

/** Synchronizes the column definition name with the text column name. */
Expand Down

0 comments on commit 254fb49

Please sign in to comment.