Skip to content

Commit 024f14c

Browse files
committed
testing
1 parent a01e8fa commit 024f14c

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/lib/core/data-table/data-table.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ describe('CdkTable', () => {
9494
});
9595
});
9696

97+
it('should disconnect the data source when table is destroyed', () => {
98+
expect(dataSource.isConnected).toBe(true);
99+
100+
fixture.destroy();
101+
expect(dataSource.isConnected).toBe(false);
102+
});
103+
97104
it('should not clobber an existing table role', () => {
98105
fixture = TestBed.createComponent(CustomRoleCdkTableApp);
99106
fixture.detectChanges();
@@ -301,8 +308,10 @@ describe('CdkTable', () => {
301308
]);
302309

303310
// Add a data source that has initialized data. Expect that the table shows this data.
304-
component.dataSource = new FakeDataSource();
311+
const dynamicDataSource = new FakeDataSource();
312+
component.dataSource = dynamicDataSource;
305313
fixture.detectChanges();
314+
expect(dynamicDataSource.isConnected).toBe(true);
306315

307316
let data = component.dataSource.data;
308317
expect(tableElement).toMatchTableContent([
@@ -315,6 +324,9 @@ describe('CdkTable', () => {
315324
// Remove the data source and check to make sure the table is empty again.
316325
component.dataSource = null;
317326
fixture.detectChanges();
327+
328+
// Expect that the old data source has been disconnected.
329+
expect(dynamicDataSource.isConnected).toBe(false);
318330
expect(tableElement).toMatchTableContent([
319331
['Column A']
320332
]);
@@ -454,6 +466,10 @@ class FakeDataSource extends DataSource<TestData> {
454466
return map.call(combineLatest(streams), ([data]) => data);
455467
}
456468

469+
disconnect() {
470+
this.isConnected = false;
471+
}
472+
457473
addData() {
458474
const nextIndex = this.data.length + 1;
459475

src/lib/core/data-table/data-table.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ export class CdkTable<T> implements CollectionViewer {
179179
ngOnDestroy() {
180180
this._onDestroy.next();
181181
this._onDestroy.complete();
182-
this.dataSource.disconnect();
182+
if (this.dataSource) {
183+
this.dataSource.disconnect();
184+
}
183185
}
184186

185187
ngOnInit() {
@@ -233,7 +235,9 @@ export class CdkTable<T> implements CollectionViewer {
233235
private _switchDataSource(dataSource: DataSource<T>) {
234236
this._data = [];
235237

236-
this.dataSource.disconnect();
238+
if (this._dataSource) {
239+
this.dataSource.disconnect();
240+
}
237241
this._dataSource = dataSource;
238242

239243
if (this._isViewInitialized) {

0 commit comments

Comments
 (0)