From 6ec99ca6b7ab57cbb7a88562cd6e91effa698541 Mon Sep 17 00:00:00 2001 From: Vladyslav Zubko <42296182+what1s1ove@users.noreply.github.com> Date: Sun, 10 Dec 2023 15:08:26 +0200 Subject: [PATCH] feat: add exception on elements without type fp-103 (#113) --- .../get-form-control-payload.js | 10 +++++++++- tests/get-form-control-payload.test.js | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/packages/get-form-control-payload/get-form-control-payload.js b/src/packages/get-form-control-payload/get-form-control-payload.js index a694ca5..a694030 100644 --- a/src/packages/get-form-control-payload/get-form-control-payload.js +++ b/src/packages/get-form-control-payload/get-form-control-payload.js @@ -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: @@ -129,7 +137,7 @@ const getFormControlPayload = (controlNode) => { } throw new FormPayloadError({ - message: `Unsupported control type – ${controlNode.type}.`, + message: `Unsupported control element type – ${controlNode.type}.`, }); }; diff --git a/tests/get-form-control-payload.test.js b/tests/get-form-control-payload.test.js index 3f70e3d..7f33289 100644 --- a/tests/get-form-control-payload.test.js +++ b/tests/get-form-control-payload.test.js @@ -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',