Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(material/select): error if selected value is accessed too early #23378

Merged
merged 1 commit into from
Sep 20, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/material-experimental/mdc-select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2709,11 +2709,34 @@ expect(panel.scrollTop)
describe(`when the select's value is accessed on initialization`, () => {
beforeEach(waitForAsync(() => configureMatSelectTestingModule([SelectEarlyAccessSibling])));

it('should not throw when trying to access the selected value on init', fakeAsync(() => {
expect(() => {
TestBed.createComponent(SelectEarlyAccessSibling).detectChanges();
}).not.toThrow();
}));
it('should not throw when trying to access the selected value on init in the view',
fakeAsync(() => {
expect(() => {
TestBed.createComponent(SelectEarlyAccessSibling).detectChanges();
}).not.toThrow();
}));

it('should not throw when reading selected value programmatically in single selection mode',
fakeAsync(() => {
expect(() => {
const fixture = TestBed.createComponent(SelectEarlyAccessSibling);
const select = fixture.debugElement.query(By.directive(MatSelect)).componentInstance;
// We're checking that accessing the getter won't throw.
select.multiple = false;
return select.selected;
}).not.toThrow();
}));

it('should not throw when reading selected value programmatically in multi selection mode',
fakeAsync(() => {
expect(() => {
const fixture = TestBed.createComponent(SelectEarlyAccessSibling);
const select = fixture.debugElement.query(By.directive(MatSelect)).componentInstance;
// We're checking that accessing the getter won't throw.
select.multiple = true;
return select.selected;
}).not.toThrow();
}));
});

describe('with ngIf and mat-label', () => {
Expand Down
33 changes: 28 additions & 5 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2776,11 +2776,34 @@ describe('MatSelect', () => {
describe(`when the select's value is accessed on initialization`, () => {
beforeEach(waitForAsync(() => configureMatSelectTestingModule([SelectEarlyAccessSibling])));

it('should not throw when trying to access the selected value on init', fakeAsync(() => {
expect(() => {
TestBed.createComponent(SelectEarlyAccessSibling).detectChanges();
}).not.toThrow();
}));
it('should not throw when trying to access the selected value on init in the view',
fakeAsync(() => {
expect(() => {
TestBed.createComponent(SelectEarlyAccessSibling).detectChanges();
}).not.toThrow();
}));

it('should not throw when reading selected value programmatically in single selection mode',
fakeAsync(() => {
expect(() => {
const fixture = TestBed.createComponent(SelectEarlyAccessSibling);
const select = fixture.debugElement.query(By.directive(MatSelect)).componentInstance;
// We're checking that accessing the getter won't throw.
select.multiple = false;
return select.selected;
}).not.toThrow();
}));

it('should not throw when reading selected value programmatically in multi selection mode',
fakeAsync(() => {
expect(() => {
const fixture = TestBed.createComponent(SelectEarlyAccessSibling);
const select = fixture.debugElement.query(By.directive(MatSelect)).componentInstance;
// We're checking that accessing the getter won't throw.
select.multiple = true;
return select.selected;
}).not.toThrow();
}));
});

describe('with ngIf and mat-label', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,8 @@ export abstract class _MatSelectBase<C> extends _MatSelectMixinBase implements A

/** The currently selected option. */
get selected(): MatOption | MatOption[] {
return this.multiple ? this._selectionModel.selected : this._selectionModel.selected[0];
return this.multiple ? (this._selectionModel?.selected || []) :
this._selectionModel?.selected[0];
}

/** The value displayed in the trigger. */
Expand Down