diff --git a/src/material/chips/chip-list.spec.ts b/src/material/chips/chip-list.spec.ts index 6d72b230b2ff..fdf8d6f1455b 100644 --- a/src/material/chips/chip-list.spec.ts +++ b/src/material/chips/chip-list.spec.ts @@ -1380,7 +1380,7 @@ describe('MatChipList', () => { }); }); - it('should preselected chip as selected inside an OnPush component', fakeAsync(() => { + it('should preselect chip as selected inside an OnPush component', fakeAsync(() => { fixture = createComponent(PreselectedChipInsideOnPush); fixture.detectChanges(); tick(); @@ -1390,6 +1390,21 @@ describe('MatChipList', () => { .withContext('Expected first chip to be selected.').toContain('mat-chip-selected'); })); + it('should not throw when accessing the selected value too early in single selection mode', + fakeAsync(() => { + fixture = createComponent(StandardChipList); + const chipList = fixture.debugElement.query(By.directive(MatChipList)).componentInstance; + expect(() => chipList.selected).not.toThrow(); + })); + + it('should not throw when accessing the selected value too early in multi selection mode', + fakeAsync(() => { + fixture = createComponent(StandardChipList); + const chipList = fixture.debugElement.query(By.directive(MatChipList)).componentInstance; + chipList.multiple = true; + expect(() => chipList.selected).not.toThrow(); + })); + function createComponent(component: Type, providers: Provider[] = [], animationsModule: Type | Type = NoopAnimationsModule): ComponentFixture { diff --git a/src/material/chips/chip-list.ts b/src/material/chips/chip-list.ts index ee007d48fbed..840e700c2b9e 100644 --- a/src/material/chips/chip-list.ts +++ b/src/material/chips/chip-list.ts @@ -154,7 +154,8 @@ export class MatChipList extends _MatChipListBase implements MatFormFieldControl /** The array of selected chips inside chip list. */ get selected(): MatChip[] | MatChip { - return this.multiple ? this._selectionModel.selected : this._selectionModel.selected[0]; + return this.multiple ? (this._selectionModel?.selected || []) : + this._selectionModel?.selected[0]; } /** The ARIA role applied to the chip list. */