Skip to content

Commit 1a1975a

Browse files
committed
add tests for errors
1 parent cd8c2da commit 1a1975a

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

src/cdk/table/table-errors.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Returns an error to be thrown when attempting to find an unexisting column.
3+
* @param id Id whose lookup failed.
4+
* @docs-private
5+
*/
6+
export function getTableUnknownColumnError(id: string) {
7+
return Error(`cdk-table: Could not find column with id "${id}".`);
8+
}
9+
10+
/**
11+
* Returns an error to be thrown when two column definitions have the same name.
12+
* @docs-private
13+
*/
14+
export function getTableDuplicateColumnNameError(name: string) {
15+
return Error(`cdk-table: Duplicate column definition name provided: "${name}".`);
16+
}

src/cdk/table/table.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {Observable} from 'rxjs/Observable';
77
import {combineLatest} from 'rxjs/observable/combineLatest';
88
import {CdkTableModule} from './index';
99
import {map} from 'rxjs/operator/map';
10+
import {getTableDuplicateColumnNameError, getTableUnknownColumnError} from './table-errors';
1011

1112
describe('CdkTable', () => {
1213
let fixture: ComponentFixture<SimpleCdkTableApp>;
@@ -26,6 +27,8 @@ describe('CdkTable', () => {
2627
TrackByCdkTableApp,
2728
DynamicColumnDefinitionsCdkTableApp,
2829
RowContextCdkTableApp,
30+
DuplicateColumnDefNameCdkTableApp,
31+
MissingColumnDefCdkTableApp,
2932
],
3033
}).compileComponents();
3134
}));
@@ -109,6 +112,16 @@ describe('CdkTable', () => {
109112
expect(fixture.nativeElement.querySelector('cdk-table').getAttribute('role')).toBe('treegrid');
110113
});
111114

115+
it('should throw an error if two column definitions have the same name', () => {
116+
expect(() => TestBed.createComponent(DuplicateColumnDefNameCdkTableApp).detectChanges())
117+
.toThrowError(getTableDuplicateColumnNameError('column_a').message);
118+
});
119+
120+
it('should throw an error if a column definition is requested but not defined', () => {
121+
expect(() => TestBed.createComponent(MissingColumnDefCdkTableApp).detectChanges())
122+
.toThrowError(getTableUnknownColumnError('column_a').message);
123+
});
124+
112125
it('should be able to dynamically add/remove column definitions', () => {
113126
const dynamicColumnDefFixture = TestBed.createComponent(DynamicColumnDefinitionsCdkTableApp);
114127
dynamicColumnDefFixture.detectChanges();
@@ -653,6 +666,45 @@ class CustomRoleCdkTableApp {
653666
@ViewChild(CdkTable) table: CdkTable<TestData>;
654667
}
655668

669+
@Component({
670+
template: `
671+
<cdk-table [dataSource]="dataSource">
672+
<ng-container cdkColumnDef="column_a">
673+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
674+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
675+
</ng-container>
676+
677+
<ng-container cdkColumnDef="column_a">
678+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
679+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
680+
</ng-container>
681+
682+
<cdk-header-row *cdkHeaderRowDef="['column_a']"></cdk-header-row>
683+
<cdk-row *cdkRowDef="let row; columns: ['column_a']"></cdk-row>
684+
</cdk-table>
685+
`
686+
})
687+
class DuplicateColumnDefNameCdkTableApp {
688+
dataSource: FakeDataSource = new FakeDataSource();
689+
}
690+
691+
@Component({
692+
template: `
693+
<cdk-table [dataSource]="dataSource">
694+
<ng-container cdkColumnDef="column_b">
695+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
696+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
697+
</ng-container>
698+
699+
<cdk-header-row *cdkHeaderRowDef="['column_a']"></cdk-header-row>
700+
<cdk-row *cdkRowDef="let row; columns: ['column_a']"></cdk-row>
701+
</cdk-table>
702+
`
703+
})
704+
class MissingColumnDefCdkTableApp {
705+
dataSource: FakeDataSource = new FakeDataSource();
706+
}
707+
656708
@Component({
657709
template: `
658710
<cdk-table [dataSource]="dataSource">

src/cdk/table/table.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,7 @@ import {BehaviorSubject} from 'rxjs/BehaviorSubject';
3636
import {Subscription} from 'rxjs/Subscription';
3737
import {Subject} from 'rxjs/Subject';
3838
import {CdkCellDef, CdkColumnDef, CdkHeaderCellDef} from './cell';
39-
40-
/**
41-
* Returns an error to be thrown when attempting to find an unexisting column.
42-
* @param id Id whose lookup failed.
43-
* @docs-private
44-
*/
45-
export function getTableUnknownColumnError(id: string) {
46-
return new Error(`cdk-table: Could not find column with id "${id}".`);
47-
}
48-
49-
/**
50-
* Returns an error to be thrown when attempting to find an unexisting column.
51-
* @docs-private
52-
*/
53-
export function getTableDuplicateColumnNameError(name: string) {
54-
return new Error(`cdk-table: Duplicate column definition name provided: "${name}".`);
55-
}
39+
import {getTableDuplicateColumnNameError, getTableUnknownColumnError} from './table-errors';
5640

5741
/**
5842
* Provides a handle for the table to grab the view container's ng-container to insert data rows.

0 commit comments

Comments
 (0)