/
text-column.spec.ts
208 lines (174 loc) · 6.18 KB
/
text-column.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {Component} from '@angular/core';
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {
getTableTextColumnMissingParentTableError,
getTableTextColumnMissingNameError,
} from './table-errors';
import {CdkTableModule} from './table-module';
import {expectTableToMatchContent} from './table.spec';
import {TEXT_COLUMN_OPTIONS, TextColumnOptions} from './tokens';
describe('CdkTextColumn', () => {
let fixture: ComponentFixture<BasicTextColumnApp>;
let component: BasicTextColumnApp;
let tableElement: HTMLElement;
beforeEach(async(() => {
TestBed
.configureTestingModule({
imports: [CdkTableModule],
declarations: [
BasicTextColumnApp,
MissingTableApp,
TextColumnWithoutNameApp,
],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BasicTextColumnApp);
component = fixture.componentInstance;
fixture.detectChanges();
tableElement = fixture.nativeElement.querySelector('.cdk-table');
});
it('should render the basic columns', () => {
expectTableToMatchContent(tableElement, [
['PropertyA', 'PropertyB', 'PropertyC'],
['a_1', 'b_1', 'c_1'],
['a_2', 'b_2', 'c_2'],
]);
});
it('should throw an error if the text column is not in the content of a table', () => {
expect(() => TestBed.createComponent(MissingTableApp).detectChanges())
.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();
expectTableToMatchContent(tableElement, [
['PropertyA', 'column-b', 'PropertyC'],
['a_1', 'b_1', 'c_1'],
['a_2', 'b_2', 'c_2'],
]);
});
it('should allow for custom data accessor', () => {
component.dataAccessorA = (data: TestData) => data.propertyA + '!';
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['PropertyA', 'PropertyB', 'PropertyC'],
['a_1!', 'b_1', 'c_1'],
['a_2!', 'b_2', 'c_2'],
]);
});
it('should allow for custom data accessor', () => {
component.dataAccessorA = (data: TestData) => data.propertyA + '!';
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['PropertyA', 'PropertyB', 'PropertyC'],
['a_1!', 'b_1', 'c_1'],
['a_2!', 'b_2', 'c_2'],
]);
});
it('should update values when data changes', () => {
component.data = [
{propertyA: 'changed-a_1', propertyB: 'b_1', propertyC: 'c_1'},
{propertyA: 'changed-a_2', propertyB: 'b_2', propertyC: 'c_2'},
];
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['PropertyA', 'PropertyB', 'PropertyC'],
['changed-a_1', 'b_1', 'c_1'],
['changed-a_2', 'b_2', 'c_2'],
]);
});
describe('with options', () => {
function createTestComponent(options: TextColumnOptions<any>) {
// Reset the previously configured testing module to be able set new providers.
// The testing module has been initialized in the root describe group for the ripples.
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [CdkTableModule],
declarations: [BasicTextColumnApp],
providers: [{provide: TEXT_COLUMN_OPTIONS, useValue: options}]
});
fixture = TestBed.createComponent(BasicTextColumnApp);
fixture.detectChanges();
tableElement = fixture.nativeElement.querySelector('.cdk-table');
}
it('should be able to provide a header text transformation', () => {
const defaultHeaderTextTransform = (name: string) => `${name}!`;
createTestComponent({defaultHeaderTextTransform});
expectTableToMatchContent(tableElement, [
['propertyA!', 'propertyB!', 'propertyC!'],
['a_1', 'b_1', 'c_1'],
['a_2', 'b_2', 'c_2'],
]);
});
it('should be able to provide a general data accessor', () => {
const defaultDataAccessor = (data: TestData, name: string) => {
switch (name) {
case 'propertyA':
return `A: ${data.propertyA}`;
case 'propertyB':
return `B: ${data.propertyB}`;
case 'propertyC':
return `C: ${data.propertyC}`;
default:
return '';
}
};
createTestComponent({defaultDataAccessor});
expectTableToMatchContent(tableElement, [
['PropertyA', 'PropertyB', 'PropertyC'],
['A: a_1', 'B: b_1', 'C: c_1'],
['A: a_2', 'B: b_2', 'C: c_2'],
]);
});
});
});
interface TestData {
propertyA: string;
propertyB: string;
propertyC: string;
}
@Component({
template: `
<cdk-table [dataSource]="data">
<cdk-text-column name="propertyA" [dataAccessor]="dataAccessorA"></cdk-text-column>
<cdk-text-column name="propertyB" [headerText]="headerTextB"></cdk-text-column>
<cdk-text-column name="propertyC" [justify]="justifyC"></cdk-text-column>
<cdk-header-row *cdkHeaderRowDef="displayedColumns"></cdk-header-row>
<cdk-row *cdkRowDef="let row; columns: displayedColumns"></cdk-row>
</cdk-table>
`
})
class BasicTextColumnApp {
displayedColumns = ['propertyA', 'propertyB', 'propertyC'];
data: TestData[] = [
{propertyA: 'a_1', propertyB: 'b_1', propertyC: 'c_1'},
{propertyA: 'a_2', propertyB: 'b_2', propertyC: 'c_2'},
];
headerTextB: string;
dataAccessorA: (data: TestData) => string;
justifyC = 'start';
}
@Component({
template: `
<cdk-text-column name="column-a"></cdk-text-column>
`
})
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 {
}