Skip to content

Commit bb90e12

Browse files
committed
add tests for errors
1 parent f8be051 commit bb90e12

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();
@@ -666,6 +679,45 @@ class CustomRoleCdkTableApp {
666679
@ViewChild(CdkTable) table: CdkTable<TestData>;
667680
}
668681

682+
@Component({
683+
template: `
684+
<cdk-table [dataSource]="dataSource">
685+
<ng-container cdkColumnDef="column_a">
686+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
687+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
688+
</ng-container>
689+
690+
<ng-container cdkColumnDef="column_a">
691+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
692+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
693+
</ng-container>
694+
695+
<cdk-header-row *cdkHeaderRowDef="['column_a']"></cdk-header-row>
696+
<cdk-row *cdkRowDef="let row; columns: ['column_a']"></cdk-row>
697+
</cdk-table>
698+
`
699+
})
700+
class DuplicateColumnDefNameCdkTableApp {
701+
dataSource: FakeDataSource = new FakeDataSource();
702+
}
703+
704+
@Component({
705+
template: `
706+
<cdk-table [dataSource]="dataSource">
707+
<ng-container cdkColumnDef="column_b">
708+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
709+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
710+
</ng-container>
711+
712+
<cdk-header-row *cdkHeaderRowDef="['column_a']"></cdk-header-row>
713+
<cdk-row *cdkRowDef="let row; columns: ['column_a']"></cdk-row>
714+
</cdk-table>
715+
`
716+
})
717+
class MissingColumnDefCdkTableApp {
718+
dataSource: FakeDataSource = new FakeDataSource();
719+
}
720+
669721
@Component({
670722
template: `
671723
<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)