diff --git a/templates/partial/search_result.html b/templates/partial/search_result.html index 3f8354ad..3ff2f91b 100644 --- a/templates/partial/search_result.html +++ b/templates/partial/search_result.html @@ -59,12 +59,14 @@

{% endswitch %} - {% if result.matches %} -
  • - {% translate "Matched fields:" %} - {{ result.matches|lookup:readable_match_reasons|join:", " }} -
  • - {% endif %} + {% with match_reasons=result.matches|lookup:readable_match_reasons|join:", " %} + {% if match_reasons %} +
  • + {% translate "Matched fields:" %} + {{ match_reasons }} +
  • + {% endif %} + {% endwith %}
    diff --git a/tests/integration/test_search_result_metadata.py b/tests/integration/test_search_result_metadata.py new file mode 100644 index 00000000..7c582e3a --- /dev/null +++ b/tests/integration/test_search_result_metadata.py @@ -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