Skip to content

Commit 857ff1f

Browse files
committed
Return early and throw on unsupported element
1 parent bd05418 commit 857ff1f

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/LiveComponent/assets/src/update_array_data.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,24 @@ export function updateArrayDataFromChangedElement(
2828
if (index === -1) {
2929
currentValues.push(value);
3030
}
31-
} else {
32-
// Remove value from an array
33-
if (index > -1) {
34-
currentValues.splice(index, 1);
35-
}
31+
32+
return currentValues;
33+
}
34+
35+
// Remove value from an array
36+
if (index > -1) {
37+
currentValues.splice(index, 1);
3638
}
37-
} else if (element instanceof HTMLSelectElement) {
39+
40+
return currentValues;
41+
}
42+
43+
if (element instanceof HTMLSelectElement) {
3844
// Select elements with `multiple` option require mapping chosen options to their values
3945
currentValues = Array.from(element.selectedOptions).map(el => el.value);
46+
47+
return currentValues;
4048
}
4149

42-
// When no values are selected for collection no data should be sent over the wire
43-
return currentValues;
50+
throw new Error(`The element used to determine array data from is unsupported (${element.tagName} provided)`);
4451
}

src/LiveComponent/assets/test/update_array_data.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,11 @@ describe('getArrayValue', () => {
5151
expect(updateArrayDataFromChangedElement(select, '', null))
5252
.toEqual(['foo', 'bar']);
5353
})
54+
55+
it('Throws on unsupported elements', () => {
56+
const div = document.createElement('div');
57+
58+
expect(() => updateArrayDataFromChangedElement(div, '', null))
59+
.toThrowError('The element used to determine array data from is unsupported (DIV provided)')
60+
});
5461
});

0 commit comments

Comments
 (0)