@@ -21,7 +21,7 @@ describe('CdkTable', () => {
21
21
22
22
TestBed . configureTestingModule ( {
23
23
imports : [ CdkDataTableModule ] ,
24
- declarations : [ SimpleCdkTableApp , CustomRoleCdkTableApp ] ,
24
+ declarations : [ SimpleCdkTableApp , DynamicDataSourceCdkTableApp , CustomRoleCdkTableApp ] ,
25
25
} ) . compileComponents ( ) ;
26
26
27
27
fixture = TestBed . createComponent ( SimpleCdkTableApp ) ;
@@ -164,6 +164,40 @@ describe('CdkTable', () => {
164
164
] ) ;
165
165
} ) ;
166
166
167
+ it ( 'should match the right table content with dynamic data source' , ( ) => {
168
+ fixture = TestBed . createComponent ( DynamicDataSourceCdkTableApp ) ;
169
+ component = fixture . componentInstance ;
170
+ tableElement = fixture . nativeElement . querySelector ( 'cdk-table' ) ;
171
+
172
+ fixture . detectChanges ( ) ; // Let the table render the rows
173
+ fixture . detectChanges ( ) ; // Let the rows render their cells
174
+
175
+ // Expect that the component has no data source and the table element reflects empty data.
176
+ expect ( component . dataSource ) . toBe ( undefined ) ;
177
+ expect ( tableElement ) . toMatchTableContent ( [
178
+ [ 'Column A' ]
179
+ ] ) ;
180
+
181
+ // Add a data source that has initialized data. Expect that the table shows this data.
182
+ component . dataSource = new FakeDataSource ( ) ;
183
+ fixture . detectChanges ( ) ;
184
+
185
+ let data = component . dataSource . data ;
186
+ expect ( tableElement ) . toMatchTableContent ( [
187
+ [ 'Column A' ] ,
188
+ [ data [ 0 ] . a ] ,
189
+ [ data [ 1 ] . a ] ,
190
+ [ data [ 2 ] . a ] ,
191
+ ] ) ;
192
+
193
+ // Remove the data source and check to make sure the table is empty again.
194
+ component . dataSource = null ;
195
+ fixture . detectChanges ( ) ;
196
+ expect ( tableElement ) . toMatchTableContent ( [
197
+ [ 'Column A' ]
198
+ ] ) ;
199
+ } ) ;
200
+
167
201
it ( 'should be able to dynamically change the columns for header and rows' , ( ) => {
168
202
expect ( dataSource . data . length ) . toBe ( 3 ) ;
169
203
@@ -262,6 +296,26 @@ class SimpleCdkTableApp {
262
296
@ViewChild ( CdkTable ) table : CdkTable < TestData > ;
263
297
}
264
298
299
+ @Component ( {
300
+ template : `
301
+ <cdk-table [dataSource]="dataSource">
302
+ <ng-container cdkColumnDef="column_a">
303
+ <cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
304
+ <cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
305
+ </ng-container>
306
+
307
+ <cdk-header-row *cdkHeaderRowDef="columnsToRender"></cdk-header-row>
308
+ <cdk-row *cdkRowDef="let row; columns: columnsToRender"></cdk-row>
309
+ </cdk-table>
310
+ `
311
+ } )
312
+ class DynamicDataSourceCdkTableApp {
313
+ dataSource : FakeDataSource ;
314
+ columnsToRender = [ 'column_a' ] ;
315
+
316
+ @ViewChild ( CdkTable ) table : CdkTable < TestData > ;
317
+ }
318
+
265
319
@Component ( {
266
320
template : `
267
321
<cdk-table [dataSource]="dataSource" role="treegrid">
@@ -317,13 +371,19 @@ const tableCustomMatchers: jasmine.CustomMatcherFactories = {
317
371
// Check header cells
318
372
const expectedHeaderContent = expectedTableContent . shift ( ) ;
319
373
getHeaderCells ( tableElement ) . forEach ( ( cell , index ) => {
320
- return checkCellContent ( cell , expectedHeaderContent [ index ] ) ;
374
+ const expected = expectedHeaderContent ?
375
+ expectedHeaderContent [ index ] :
376
+ null ;
377
+ checkCellContent ( cell , expected ) ;
321
378
} ) ;
322
379
323
380
// Check data row cells
324
381
getRows ( tableElement ) . forEach ( ( row , rowIndex ) => {
325
382
getCells ( row ) . forEach ( ( cell , cellIndex ) => {
326
- checkCellContent ( cell , expectedTableContent [ rowIndex ] [ cellIndex ] ) ;
383
+ const expected = expectedHeaderContent ?
384
+ expectedTableContent [ rowIndex ] [ cellIndex ] :
385
+ null ;
386
+ checkCellContent ( cell , expected ) ;
327
387
} ) ;
328
388
} ) ;
329
389
0 commit comments