Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/material-experimental/mdc-table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,45 @@ describe('MDC-based MatTable', () => {
['Footer A', 'Footer B', 'Footer C'],
]);
});

it('should fall back to empty table if invalid data is passed in', () => {
component.underlyingDataSource.addData();
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
['a_1', 'b_1', 'c_1'],
['a_2', 'b_2', 'c_2'],
['a_3', 'b_3', 'c_3'],
['a_4', 'b_4', 'c_4'],
['Footer A', 'Footer B', 'Footer C'],
]);

dataSource.data = null!;
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
['Footer A', 'Footer B', 'Footer C'],
]);

component.underlyingDataSource.addData();
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
['a_1', 'b_1', 'c_1'],
['a_2', 'b_2', 'c_2'],
['a_3', 'b_3', 'c_3'],
['a_4', 'b_4', 'c_4'],
['a_5', 'b_5', 'c_5'],
['Footer A', 'Footer B', 'Footer C'],
]);

dataSource.data = {} as any;
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
['Footer A', 'Footer B', 'Footer C'],
]);
});
});
});

Expand Down
1 change: 1 addition & 0 deletions src/material/table/table-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class _MatTableDataSource<
return this._data.value;
}
set data(data: T[]) {
data = Array.isArray(data) ? data : [];
this._data.next(data);
// Normally the `filteredData` is updated by the re-render
// subscription, but that won't happen if it's inactive.
Expand Down
39 changes: 39 additions & 0 deletions src/material/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,45 @@ describe('MatTable', () => {
['Footer A', 'Footer B', 'Footer C'],
]);
});

it('should fall back to empty table if invalid data is passed in', () => {
component.underlyingDataSource.addData();
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
['a_1', 'b_1', 'c_1'],
['a_2', 'b_2', 'c_2'],
['a_3', 'b_3', 'c_3'],
['a_4', 'b_4', 'c_4'],
['Footer A', 'Footer B', 'Footer C'],
]);

dataSource.data = null!;
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
['Footer A', 'Footer B', 'Footer C'],
]);

component.underlyingDataSource.addData();
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
['a_1', 'b_1', 'c_1'],
['a_2', 'b_2', 'c_2'],
['a_3', 'b_3', 'c_3'],
['a_4', 'b_4', 'c_4'],
['a_5', 'b_5', 'c_5'],
['Footer A', 'Footer B', 'Footer C'],
]);

dataSource.data = {} as any;
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['Column A', 'Column B', 'Column C'],
['Footer A', 'Footer B', 'Footer C'],
]);
});
});
});

Expand Down