Skip to content

Commit

Permalink
feat: throw when selecting invalid index, value or label
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
`TestSelect`'s `selectIndex`, `selectValue` and `selectLabel` methods now throw if an invalid index, value or label is passed, instead of ignoring it.
  • Loading branch information
jnizet committed May 26, 2024
1 parent 6a5225a commit 093cb5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
15 changes: 14 additions & 1 deletion projects/ngx-speculoos/src/lib/test-select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TestComponentTester extends ComponentTester<TestComponent> {
}
}

describe('TestButton', () => {
describe('TestSelect', () => {
let tester: TestComponentTester;

beforeEach(() => {
Expand All @@ -59,6 +59,11 @@ describe('TestButton', () => {
expect(tester.detectChanges).toHaveBeenCalled();
});

it('should throw if index is out of bounds', () => {
expect(() => tester.selectBox.selectIndex(-2)).toThrowError();
expect(() => tester.selectBox.selectIndex(3)).toThrowError();
});

it('should select by value', () => {
spyOn(tester.componentInstance, 'onChange');
spyOn(tester, 'detectChanges').and.callThrough();
Expand All @@ -69,6 +74,10 @@ describe('TestButton', () => {
expect(tester.detectChanges).toHaveBeenCalled();
});

it('should throw if value does not exist', () => {
expect(() => tester.selectBox.selectValue('oops')).toThrowError();
});

it('should select by label', () => {
spyOn(tester.componentInstance, 'onChange');
spyOn(tester, 'detectChanges').and.callThrough();
Expand All @@ -79,6 +88,10 @@ describe('TestButton', () => {
expect(tester.detectChanges).toHaveBeenCalled();
});

it('should throw if label does not exist', () => {
expect(() => tester.selectBox.selectLabel('oops')).toThrowError();
});

it('should expose the selected value', () => {
expect(tester.selectBox.selectedValue).toBe('');

Expand Down
16 changes: 11 additions & 5 deletions projects/ngx-speculoos/src/lib/test-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,40 @@ export class TestSelect extends TestHtmlElement<HTMLSelectElement> {
}

/**
* Selects the option at the given index, then dispatches an event of type change and triggers a change detection
* Selects the option at the given index, then dispatches an event of type change and triggers a change detection.
* If the index is out of bounds and is not -1, then throws an error.
*/
selectIndex(index: number): void {
if (index < -1 || index >= this.nativeElement.options.length) {
throw new Error(`The index ${index} is out of bounds`);
}
this.nativeElement.selectedIndex = index;
this.dispatchEventOfType('change');
}

/**
* Selects the first option with the given value, then dispatches an event of type change and triggers a change detection.
* If there is no option with the given value, then does nothing
* TODO should it throw instead?
* If there is no option with the given value, then throws an error.
*/
selectValue(value: string): void {
const index = this.optionValues.indexOf(value);
if (index >= 0) {
this.selectIndex(index);
} else {
throw new Error(`The value ${value} is not part of the option values (${this.optionValues.join(', ')}`);
}
}

/**
* Selects the first option with the given label (or text), then dispatches an event of type change and triggers a change detection.
* If there is no option with the given label, then does nothing
* TODO should it throw instead?
* If there is no option with the given label, then throws an error.
*/
selectLabel(label: string): void {
const index = this.optionLabels.indexOf(label);
if (index >= 0) {
this.selectIndex(index);
} else {
throw new Error(`The label ${label} is not part of the option labels (${this.optionLabels.join(', ')}`);
}
}

Expand Down

0 comments on commit 093cb5a

Please sign in to comment.