Skip to content

Commit

Permalink
fix: don't show matched fields if there is nothing to show
Browse files Browse the repository at this point in the history
Datahub was returning a spurious 'tags: []' value, which
doesn't require rendering anything. This meant we were showing
an empty matched fields even when no search query was entered.
  • Loading branch information
MatMoore committed Aug 7, 2024
1 parent b4c2bf6 commit 1efbbbd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
14 changes: 8 additions & 6 deletions templates/partial/search_result.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ <h3 class="govuk-heading-m govuk-!-margin-bottom-2">
</li>
{% endswitch %}

{% if result.matches %}
<li>
<span class="govuk-!-font-weight-bold">{% translate "Matched fields:" %}</span>
<span>{{ result.matches|lookup:readable_match_reasons|join:", " }}</span>
</li>
{% endif %}
{% with match_reasons=result.matches|lookup:readable_match_reasons|join:", " %}
{% if match_reasons %}
<li>
<span class="govuk-!-font-weight-bold">{% translate "Matched fields:" %}</span>
<span>{{ match_reasons }}</span>
</li>
{% endif %}
{% endwith %}
</ul>
<hr class="govuk-section-break govuk-section-break--m govuk-section-break--visible">
</div>
Expand Down
48 changes: 48 additions & 0 deletions tests/integration/test_search_result_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pytest
from data_platform_catalogue.search_types import ResultType, SearchResult

from tests.conftest import mock_search_response


@pytest.mark.slow
class TestSearchResultMetadata:
"""
Given I am on a search page
When I look at the metadata underneath a search result
Then "matched fields" should only be shown if I entered a search term
"""

@pytest.fixture(autouse=True)
def setup(self, live_server, selenium):
self.selenium = selenium
self.live_server_url = live_server.url

def test_matched_fields_hidden(self, mock_catalogue):
result = SearchResult(
urn="fake-urn",
result_type=ResultType.DATABASE,
name="abc",
fully_qualified_name="abc",
description="bla bla bla",
matches={"tags": []},
)
mock_search_response(mock_catalogue, total_results=1, page_results=[result])

self.selenium.get(f"{self.live_server_url}/search")

assert "Matched fields:" not in self.selenium.page_source

def test_matched_fields_shown(self, mock_catalogue):
result = SearchResult(
urn="fake-urn",
result_type=ResultType.DATABASE,
name="abc",
fully_qualified_name="abc",
description="bla bla bla",
matches={"tags": [], "description": "abc"},
)
mock_search_response(mock_catalogue, total_results=1, page_results=[result])

self.selenium.get(f"{self.live_server_url}/search?query=bla")

assert "Matched fields:" in self.selenium.page_source

0 comments on commit 1efbbbd

Please sign in to comment.