Skip to content

Commit

Permalink
Merge pull request #4118 from open-formulieren/issue/missing-radiofie…
Browse files Browse the repository at this point in the history
…ld-tests

✅ Add missing validation tests for radio input
  • Loading branch information
sergei-maertens authored Apr 4, 2024
2 parents 908cbe1 + babc2d4 commit f079be7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
53 changes: 52 additions & 1 deletion src/openforms/formio/tests/validation/test_radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
35 changes: 34 additions & 1 deletion src/openforms/tests/e2e/test_input_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit f079be7

Please sign in to comment.