Skip to content

Commit

Permalink
fix(angular/table): better handling of invalid data (#1030)
Browse files Browse the repository at this point in the history
The table data source is set up to expect an array and will throw a cryptic error
 down the line if the value is anything different. While typings should be enough to enforce this,
 if the value comes from somewhere in the view it may not get caught.
Since the effort for handling it on our end is minimal,
these changes add some logic that fall back to an empty array if the value is invalid.
angular/components#18953
  • Loading branch information
jeripeierSBB authored Jan 17, 2022
1 parent 657cdb3 commit 5e978c6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/angular/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,45 @@ describe('SbbTable', () => {
['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'],
]);
});
});

it('should set css classes to grouped columns', () => {
Expand Down
1 change: 1 addition & 0 deletions src/angular/table/table/table-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class _SbbTableDataSource<
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

0 comments on commit 5e978c6

Please sign in to comment.