Skip to content

Commit 6615c9a

Browse files
committed
Address comments
1 parent ab917a2 commit 6615c9a

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/cdk/collections/selection.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {SelectionModel} from './selection';
1+
import {getMultipleValuesInSingleSelectionError, SelectionModel} from './selection';
22

33

44
describe('SelectionModel', () => {
@@ -23,8 +23,7 @@ describe('SelectionModel', () => {
2323
});
2424

2525
it('should throw an error if multiple values are passed to model', () => {
26-
expect(() => model.select(1, 2))
27-
.toThrowError(/Cannot pass multiple values into SelectionModel with single-value mode/);
26+
expect(() => model.select(1, 2)).toThrow(getMultipleValuesInSingleSelectionError());
2827
});
2928

3029
it('should only preselect one value', () => {

src/cdk/collections/selection.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class SelectionModel<T> {
5757
* Selects a value or an array of values.
5858
*/
5959
select(...values: T[]): void {
60-
this._warnMultipleValuesForSingleSelection(values);
60+
this._verifyValueAssignment(values);
6161
values.forEach(value => this._markSelected(value));
6262
this._emitChangeEvent();
6363
}
@@ -66,7 +66,7 @@ export class SelectionModel<T> {
6666
* Deselects a value or an array of values.
6767
*/
6868
deselect(...values: T[]): void {
69-
this._warnMultipleValuesForSingleSelection(values);
69+
this._verifyValueAssignment(values);
7070
values.forEach(value => this._unmarkSelected(value));
7171
this._emitChangeEvent();
7272
}
@@ -165,10 +165,13 @@ export class SelectionModel<T> {
165165
}
166166
}
167167

168-
/** Throws an error if multiple values are passed into a selection model with a single value. */
169-
private _warnMultipleValuesForSingleSelection(values: T[]) {
168+
/**
169+
* Verifies the value assignment and throws an error if the specified value array is
170+
* including multiple values while the selection model is not supporting multiple values.
171+
*/
172+
private _verifyValueAssignment(values: T[]) {
170173
if (values.length > 1 && !this._isMulti) {
171-
throwMultipleValuesInSingleSelectionError();
174+
throw getMultipleValuesInSingleSelectionError();
172175
}
173176
}
174177
}
@@ -182,6 +185,6 @@ export class SelectionChange<T> {
182185
}
183186

184187
/** Throws an error if multiple values are passed into a selection model with a single value. */
185-
export function throwMultipleValuesInSingleSelectionError() {
186-
throw Error('Cannot pass multiple values into SelectionModel with single-value mode.');
188+
export function getMultipleValuesInSingleSelectionError() {
189+
return Error('Cannot pass multiple values into SelectionModel with single-value mode.');
187190
}

0 commit comments

Comments
 (0)