Skip to content

Commit 74a11bb

Browse files
committed
✅ FIX broken test.
1 parent 0ef0f21 commit 74a11bb

File tree

6 files changed

+132
-27
lines changed

6 files changed

+132
-27
lines changed

src/open_inwoner/components/templates/components/Filter/Filter.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% load i18n icon_tags button_tags form_tags %}
22

33
<div class="filter__container">
4-
<details class="filter" aria-label="{% trans "Filter" %}">
4+
<details class="filter" aria-label="{% trans "Filter" %}" {% if initial_open %}open{% endif %}>
55
<summary class="filter__title">
66
<span class="filter__summary-label">
77
{{ field.label }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{% load i18n icon_tags button_tags form_tags filter_tags utils %}
2+
3+
<div>
4+
<div class='filter__reset'>
5+
{% button transparent=True type='button' inline=True text=_('Wis alle?') %}
6+
</div>
7+
8+
{% if search_filter_categories and search_form.categories.field.choices|length != 0 %}
9+
{% filter field=search_form.categories form_id=form_id open_filter_index=open_filter_index index=0 %}
10+
{% endif %}
11+
12+
{% if search_filter_tags and search_form.tags.field.choices|length != 0 %}
13+
{% filter field=search_form.tags form_id=form_id open_filter_index=open_filter_index index=1 %}
14+
{% endif %}
15+
16+
{% if search_filter_organizations and search_form.organizations.field.choices|length != 0 %}
17+
{% filter field=search_form.organizations form_id=form_id open_filter_index=open_filter_index index=2 %}
18+
{% endif %}
19+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from django import template
2+
3+
register = template.Library()
4+
5+
6+
@register.inclusion_tag("components/Filter/SearchFilters.html")
7+
def search_filters(
8+
search_form,
9+
search_filter_categories,
10+
search_filter_tags,
11+
search_filter_organizations,
12+
**kwargs
13+
):
14+
"""
15+
Renders the actions in a filterable table.
16+
17+
Usage:
18+
{% search_filters search_form form_id='form_id' search_filter_categories search_filter_tags search_filter_organizations %}
19+
20+
Available options:
21+
+ search_form: BaseForm | The form that contains the fields with choices.
22+
+ form_id: str | The id of the form.
23+
+ search_filter_categories: bool | The config bool that defines if the categorie choices are rendered.
24+
+ search_filter_tags: bool | The config bool that defines if the tag choices are rendered.
25+
+ search_filter_organizations: bool | The config bool that defines if the organization choices are rendered.
26+
"""
27+
28+
open_filter_index = 0
29+
30+
# Determine the initial open state based on active filters and their choices
31+
filters = [
32+
(search_filter_categories, len(search_form["categories"].field.choices), 0),
33+
(search_filter_tags, len(search_form["tags"].field.choices), 1),
34+
(
35+
search_filter_organizations,
36+
len(search_form["organizations"].field.choices),
37+
2,
38+
),
39+
]
40+
41+
# Set initial_open based on which filter is active and has choices
42+
for filter_active, choices_len, index in filters:
43+
if filter_active and choices_len != 0:
44+
open_filter_index = index
45+
break # Stop once we find the first active filter with choices
46+
47+
kwargs["search_form"] = search_form
48+
kwargs["search_filter_categories"] = search_filter_categories
49+
kwargs["search_filter_tags"] = search_filter_tags
50+
kwargs["search_filter_organizations"] = search_filter_organizations
51+
kwargs["open_filter_index"] = open_filter_index
52+
53+
return {**kwargs}
54+
55+
56+
@register.inclusion_tag("components/Filter/Filter.html")
57+
def filter(field, **kwargs):
58+
"""
59+
Renders the actions in a filterable table.
60+
61+
Usage:
62+
{% filter field=search_form.tags form_id=form_id open_filter_index=1 index=1 %}
63+
64+
Available options:
65+
+ field: Field | The field that needs to be rendered.
66+
+ form_id: str | The id of the form.
67+
+ open_filter_index: int | The index of the filter that should render open.
68+
+ index: int | The index of the current filter.
69+
"""
70+
71+
kwargs["initial_open"] = kwargs["open_filter_index"] is kwargs["index"]
72+
73+
return {"field": field, **kwargs}

src/open_inwoner/scss/components/SearchResults/SearchResults.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
gap: var(--spacing-large);
66

77
@media (min-width: 768px) {
8-
margin-top: 3.375rem; // 54px
8+
margin-top: calc(2 * var(--spacing-giant) - var(--spacing-small)); // 54px
99
}
1010

1111
.grid {

src/open_inwoner/search/tests/test_page.py

+35-12
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,12 @@ def test_search_filter_configuration(self):
137137
config = SiteConfiguration.get_solo()
138138

139139
def _assert_facet_checkbox_count(response, facet, count):
140-
controls = response.pyquery(f"input[form='search-form'][name='{facet}']")
141-
self.assertEqual(len(controls), count)
140+
details_selector = f"details.filter div.filter__list input[name='{facet}']"
141+
checkboxes = response.pyquery(details_selector)
142+
143+
self.assertEqual(
144+
len(checkboxes), count, f"Incorrect number of {facet} checkboxes"
145+
)
142146

143147
with self.subTest("tags"):
144148
config.search_filter_tags = False
@@ -301,16 +305,35 @@ def test_search_with_filters(self):
301305
expect(checkbox).to_be_enabled()
302306
expect(checkbox).not_to_be_checked()
303307

304-
def _click_checkbox_for_name(page=page, name=""):
305-
# first open de <details> through the summary
306-
page.locator(".filter").filter(
307-
has=page.get_by_role("checkbox", name=name)
308-
).locator(".filter__title").click()
309-
# our checkbox widget hides the <input> element and styles the <label> and a pseudo-element
310-
# this a problem for playwright accessibility, so we find the label for the checkbox and click on the label like a user would
311-
page.locator(".checkbox").filter(
312-
has=page.get_by_role("checkbox", name=name)
313-
).locator("label").click()
308+
def _click_checkbox_for_name(page, name):
309+
# Find the details element containing the label
310+
details_with_label = page.locator("details.filter").filter(
311+
has=page.get_by_text(name, exact=True)
312+
)
313+
314+
# If exact match fails, try partial match
315+
if details_with_label.count() == 0:
316+
details_with_label = page.locator("details.filter").filter(
317+
has=page.get_by_text(name)
318+
)
319+
320+
# Ensure the details element is open and scrolled into view
321+
summary = details_with_label.locator("summary")
322+
summary.scroll_into_view_if_needed()
323+
summary.click()
324+
325+
# Find the checkbox input
326+
checkbox_input = (
327+
details_with_label.locator(
328+
f"input[type='checkbox'] + label:has-text('{name}')"
329+
)
330+
.locator("..")
331+
.locator("input[type='checkbox']")
332+
)
333+
334+
# Scroll the checkbox into view and click
335+
checkbox_input.scroll_into_view_if_needed()
336+
checkbox_input.click()
314337

315338
def _test_search(checkbox_name, expected_text):
316339
page.goto(self.live_reverse("search:search", params={"query": "summary"}))

src/open_inwoner/templates/pages/search.html

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends 'master.html' %}
2-
{% load i18n form_tags utils icon_tags grid_tags pagination_tags button_tags %}
2+
{% load i18n form_tags utils icon_tags grid_tags pagination_tags button_tags filter_tags %}
33

44
{% block main_inner %}
55
{# search term section #}
@@ -13,20 +13,10 @@ <h1 class="utrecht-heading-1">{% trans "Zoekresultaten voor " %} "{% firstof sea
1313
{# facets and results section #}
1414
<div class="grid">
1515
{% if paginator.count %}
16+
{# start search filters #}
1617
{% if search_filter_categories or search_filter_tags or search_filter_organizations %}
1718
<aside class="grid__sidebar grid__filters" aria-label="{% trans "Zoekfilters" %}">
18-
<div class='filter__reset'>
19-
{% button transparent=True type='button' inline=True text=_('Wis alle?') %}
20-
</div>
21-
{% if search_filter_categories and search_form.categories.field.choices|length != 0 %}
22-
{% include "components/Filter/Filter.html" with field=search_form.categories form_id="search-form" only %}
23-
{% endif %}
24-
{% if search_filter_tags and search_form.tags.field.choices|length != 0 %}
25-
{% include "components/Filter/Filter.html" with field=search_form.tags form_id="search-form" only %}
26-
{% endif %}
27-
{% if search_filter_organizations and search_form.organizations.field.choices|length != 0 %}
28-
{% include "components/Filter/Filter.html" with field=search_form.organizations form_id="search-form" only %}
29-
{% endif %}
19+
{% search_filters search_form search_filter_categories search_filter_tags search_filter_organizations form_id="search-form" %}
3020
</aside>
3121
{% endif %}
3222
{# end search filters #}

0 commit comments

Comments
 (0)