From babc2d4c12bb1bd25d7978db05bbf82cbd45a162 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Thu, 4 Apr 2024 12:18:19 +0200 Subject: [PATCH] :white_check_mark: Add missing validation tests for radio input --- .../formio/tests/validation/test_radio.py | 53 ++++++++++++++++++- .../tests/e2e/test_input_validation.py | 35 +++++++++++- 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/openforms/formio/tests/validation/test_radio.py b/src/openforms/formio/tests/validation/test_radio.py index d21d62e9b1..16f47aa5ed 100644 --- a/src/openforms/formio/tests/validation/test_radio.py +++ b/src/openforms/formio/tests/validation/test_radio.py @@ -3,11 +3,62 @@ from openforms.typing import JSONObject from ...typing import RadioComponent -from .helpers import validate_formio_data +from .helpers import extract_error, validate_formio_data class RadioValidationTests(SimpleTestCase): + def test_radio_required_validation(self): + component: RadioComponent = { + "type": "radio", + "key": "foo", + "label": "Test", + "validate": {"required": True}, + "openForms": {"dataSrc": "manual"}, # type: ignore + "values": [ + {"value": "a", "label": "A"}, + {"value": "b", "label": "B"}, + ], + } + + invalid_values = [ + ({}, "required"), + ({"foo": ""}, "invalid_choice"), + ({"foo": None}, "null"), + ] + + for data, error_code in invalid_values: + with self.subTest(data=data): + is_valid, errors = validate_formio_data(component, data) + + self.assertFalse(is_valid) + self.assertIn(component["key"], errors) + error = extract_error(errors, component["key"]) + self.assertEqual(error.code, error_code) + + def test_invalid_option_provided(self): + component: RadioComponent = { + "type": "radio", + "key": "foo", + "label": "Test", + "validate": {"required": False}, + "openForms": {"dataSrc": "manual"}, # type: ignore + "values": [ + {"value": "a", "label": "A"}, + {"value": "b", "label": "B"}, + ], + } + + with self.subTest("valid option"): + is_valid, _ = validate_formio_data(component, {"foo": "b"}) + + self.assertTrue(is_valid) + + with self.subTest("invalid option"): + is_valid, _ = validate_formio_data(component, {"foo": "c"}) + + self.assertFalse(is_valid) + @tag("gh-4096") def test_radio_hidden_required(self): component: RadioComponent = { diff --git a/src/openforms/tests/e2e/test_input_validation.py b/src/openforms/tests/e2e/test_input_validation.py index 04e6f9d136..e43cedb501 100644 --- a/src/openforms/tests/e2e/test_input_validation.py +++ b/src/openforms/tests/e2e/test_input_validation.py @@ -11,7 +11,7 @@ from playwright.async_api import Page, expect -from openforms.formio.typing import Component, DateComponent +from openforms.formio.typing import Component, DateComponent, RadioComponent from .input_validation_base import ValidationsTestCase @@ -172,6 +172,39 @@ def test_max_value(self): ) +class RadioTests(ValidationsTestCase): + async def apply_ui_input(self, page: Page, label: str, ui_input: str | int | float): + """ + Check the radio input labeled by ``ui_input``. + """ + assert isinstance(ui_input, str) + + # no option to select -> do nothing + if not ui_input: + return + + await page.get_by_label(ui_input).click() + + def test_required_field(self): + component: RadioComponent = { + "type": "radio", + "key": "requiredRadio", + "label": "Required radio", + "validate": {"required": True}, + "openForms": {"dataSrc": "manual"}, # type: ignore + "values": [ + {"value": "a", "label": "A"}, + {"value": "b", "label": "B"}, + ], + } + + self.assertValidationIsAligned( + component, + ui_input="", + expected_ui_error="Het verplichte veld Required radio is niet ingevuld.", + ) + + class SingleBSNTests(ValidationsTestCase): def test_required_field(self): component: Component = {