Skip to content

Commit

Permalink
feat: add exception on elements without type fp-103 (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
what1s1ove authored Dec 10, 2023
1 parent b3f4315 commit 6ec99ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ const getFormControlsPayload = (...controlElements) => {
* @throws {FormPayloadError}
*/
const getFormControlPayload = (controlNode) => {
const hasType = 'type' in controlNode;

if (!hasType) {
throw new FormPayloadError({
message: 'Control element has no type attribute.',
});
}

switch (controlNode.type) {
case ControlType.TEXT:
case ControlType.PASSWORD:
Expand Down Expand Up @@ -129,7 +137,7 @@ const getFormControlPayload = (controlNode) => {
}

throw new FormPayloadError({
message: `Unsupported control type – ${controlNode.type}.`,
message: `Unsupported control element type – ${controlNode.type}.`,
});
};

Expand Down
6 changes: 6 additions & 0 deletions tests/get-form-control-payload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ describe('getFormControlPayload should work correctly', () => {
});

describe('should work correctly with an unexpected control type', () => {
test('should throw FormPayloadError for elements without type', () => {
const control = /** @type {HTMLInputElement} */ ({});

throws(() => getFormControlPayload(control), FormPayloadError);
});

test('should throw FormPayloadError with unknown input type', () => {
const control = /** @type {HTMLInputElement} */ ({
type: 'unknown-type',
Expand Down

0 comments on commit 6ec99ca

Please sign in to comment.