Skip to content

Commit

Permalink
Bug: Fix answer labels on summary pages when there is only one relate…
Browse files Browse the repository at this point in the history
…d answer (#1197)

* Initial changes

* Remove test schema

* Restore summary row title

---------

Co-authored-by: Mebin Abraham <35296336+MebinAbraham@users.noreply.github.com>
Co-authored-by: katie-gardner <katharine.gardner@and.digital>
Co-authored-by: Katie Gardner_ONS <114991656+katie-gardner@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 12, 2023
1 parent 645503a commit 7a74d3c
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 28 deletions.
27 changes: 10 additions & 17 deletions app/jinja_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,27 +470,19 @@ def __init__( # noqa: C901, R0912 pylint: disable=too-complex, too-many-branche
self,
question: SelectFieldBase._Option,
answer: SelectFieldBase._Option,
multiple_answers: bool,
answers_are_editable: bool,
no_answer_provided: str,
edit_link_text: str,
edit_link_aria_label: str,
summary_type: str,
use_answer_label: bool = False,
) -> None:
if "type" in answer:
answer_type = answer["type"]
else:
answer_type = "calculated"

answer_type = answer.get("type", "calculated")
if (
(
multiple_answers
or answer_type == "relationship"
or is_summary_with_calculation(summary_type)
)
and "label" in answer
and answer["label"]
):
answer_type == "relationship"
or is_summary_with_calculation(summary_type)
or use_answer_label
) and answer.get("label"):
self.rowTitle = answer["label"]
self.rowTitleAttributes = {"data-qa": answer["id"] + "-label"}
else:
Expand Down Expand Up @@ -563,12 +555,12 @@ def __init__(
no_answer_provided: str,
edit_link_text: str,
edit_link_aria_label: str,
use_answer_label: bool = False,
) -> None:
self.rowTitle = strip_tags(question["title"])
self.id = question["id"]
self.rowItems = []

multiple_answers = len(question["answers"]) > 1
use_answer_label = use_answer_label or len(question["answers"]) > 1

if is_summary_with_calculation(summary_type) and not answers_are_editable:
self.total = True
Expand All @@ -578,12 +570,12 @@ def __init__(
SummaryRowItem(
question,
answer,
multiple_answers,
answers_are_editable,
no_answer_provided,
edit_link_text,
edit_link_aria_label,
summary_type,
use_answer_label,
)
)

Expand Down Expand Up @@ -742,6 +734,7 @@ def map_list_collector_config(
no_answer_provided=flask_babel.lazy_gettext("No answer provided"),
edit_link_text=edit_link_text,
edit_link_aria_label=edit_link_aria_label,
use_answer_label=True,
)
row_items.extend(summary_row.rowItems)

Expand Down
22 changes: 11 additions & 11 deletions schemas/test/en/test_list_collector_section_summary.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
"type": "string"
}
],
"questionnaire_flow": {
"type": "Linear",
"options": {
"summary": {
"collapsible": false
}
}
},
"post_submission": {
"view_response": true
},
"answer_codes": [
{
"answer_id": "any-companies-or-branches-answer",
Expand Down Expand Up @@ -63,17 +74,6 @@
"code": "7"
}
],
"questionnaire_flow": {
"type": "Linear",
"options": {
"summary": {
"collapsible": false
}
}
},
"post_submission": {
"view_response": true
},
"sections": [
{
"id": "section-companies",
Expand Down
119 changes: 119 additions & 0 deletions tests/app/test_jinja_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,5 +1006,124 @@ def test_summary_item_config_with_list_collector():
assert to_dict(expected) == to_dict(result)


@pytest.mark.usefixtures("gb_locale")
def test_summary_item_config_with_list_collector_and_one_related_answer():
expected = [
{
"rowItems": [
{
"actions": [
{
"visuallyHiddenText": "Change your answer for:",
"attributes": {"data-qa": "list-item-change-1-link"},
"text": "Change",
"url": "change_link_url",
},
{
"visuallyHiddenText": "Remove Company A",
"attributes": {"data-qa": "list-item-remove-1-link"},
"text": "Remove",
"url": "remove_link_url",
},
],
"iconType": None,
"id": "vmmPmD",
"rowTitle": "Company A",
"rowTitleAttributes": {
"data-list-item-id": "vmmPmD",
"data-qa": "list-item-1-label",
},
},
{
"actions": [
{
"visuallyHiddenText": "Change your answer for: "
"Registration number",
"attributes": {
"data-ga": "click",
"data-ga-action": "Edit click",
"data-ga-category": "Summary",
"data-qa": "registration-number-edit",
},
"text": "Change",
"url": "edit_link_url",
}
],
"attributes": {"data-qa": "registration-number"},
"id": "registration-number",
"rowTitle": "Registration number",
"rowTitleAttributes": {"data-qa": "registration-number-label"},
"valueList": [{"text": "123"}],
},
]
}
]

result = map_summary_item_config(
group={
"blocks": [
{
"title": "Companies or UK branches",
"type": "List",
"add_link": "/questionnaire/companies/add-company/?return_to=section-summary",
"add_link_text": "Add another UK company or branch",
"empty_list_text": "No UK company or branch added",
"list_name": "companies",
"related_answers": {
"vmmPmD": [
{
"id": "edit-company",
"title": None,
"number": None,
"question": {
"id": "add-question",
"type": "General",
"title": "Give details about the company or branch that undertakes general insurance business",
"number": None,
"answers": [
{
"id": "registration-number",
"label": "Registration number",
"value": 123,
"type": "number",
"unit": None,
"unit_length": None,
"currency": None,
"link": "edit_link_url",
}
],
},
}
]
},
"answer_title": "Name of UK company or branch",
"list": {
"list_items": [
{
"item_title": "Company A",
"primary_person": False,
"list_item_id": "vmmPmD",
"edit_link": "change_link_url",
"remove_link": "remove_link_url",
}
],
"editable": True,
},
}
],
},
summary_type="SectionSummary",
answers_are_editable=True,
no_answer_provided="No answer Provided",
remove_link_aria_label="Remove Company A",
remove_link_text="Remove",
edit_link_text="Change",
edit_link_aria_label="Change your answer for:",
calculated_question={},
)

assert to_dict(expected) == to_dict(result)


def to_dict(obj):
return json.loads(json.dumps(obj, default=lambda o: o.__dict__))

0 comments on commit 7a74d3c

Please sign in to comment.