Skip to content

Commit

Permalink
[Main Nav] Handle routes with no page or menu context (#12282)
Browse files Browse the repository at this point in the history
* Handle routes with no page or menu context

* Fix type checking
  • Loading branch information
jhonatan-lopes authored Apr 30, 2024
1 parent 60d5282 commit 2739576
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
16 changes: 12 additions & 4 deletions network-api/networkapi/nav/templatetags/nav_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ def get_dropdown_id(**kwargs):
index = int(kwargs["idx"])
except ValueError:
return None
menu = kwargs["menu"]
menu = kwargs.get("menu", None)
if not menu:
return None
return menu.dropdowns.get_prep_value()[index]["id"]


@register.simple_tag(takes_context=True)
def check_if_dropdown_is_active(context, dropdown_id):
# The page that user is currently visiting/requesting:
page = context["page"]
menu = context["menu"]
page = context.get("page", None)
if not page:
return None
menu = context.get("menu", None)
if not menu:
return None
dropdowns_page_links = menu.page_references_per_dropdown

# Don't highlight the link if the page is the homepage
Expand Down Expand Up @@ -69,7 +75,9 @@ def check_if_link_is_active(context, link):
return False

# Page that the user is currently visiting/requesting:
page = context["page"]
page = context.get("page", None)
if not page:
return None

# Don't highlight the link if the page is the homepage
if isinstance(page, Homepage):
Expand Down
57 changes: 57 additions & 0 deletions network-api/networkapi/nav/tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

import wagtail_factories

from networkapi.nav import factories as nav_factories
Expand Down Expand Up @@ -32,6 +34,13 @@ def test_get_dropdown_id_invalid_index(self) -> None:
# Check if the ID is None:
self.assertIsNone(dropdown_id)

def test_get_dropdown_id_no_menu(self) -> None:
# Get the dropdown IDs without a menu:
dropdown_id = nav_tags.get_dropdown_id(menu=None, idx=0)

# Check if the ID is None:
self.assertIsNone(dropdown_id)


class TestCheckIfDropdownIsActive(test_base.WagtailpagesTestCase):
def test_active_dropdown_check(self) -> None:
Expand Down Expand Up @@ -148,6 +157,38 @@ def test_homepage_should_never_be_marked_as_active(self):
# Even though the homepage is part of the dropdown, it should not be marked as active:
self.assertFalse(nav_tags.check_if_dropdown_is_active(context, dropdown_1_id))

def test_returns_false_if_no_page(self) -> None:
"""If no page is passed, the function should return False."""
# Create a menu with the first dropdown linking to a page:
page = wagtail_factories.PageFactory(parent=self.homepage, title="Test Page")
menu = nav_factories.NavMenuFactory(
dropdowns__0__dropdown__button__link_to="page",
dropdowns__0__dropdown__button__page=page,
)

dropdown_1_id = nav_tags.get_dropdown_id(menu=menu, idx=0)

# No page is passed
context = {"menu": menu}
# Should return False
self.assertFalse(nav_tags.check_if_dropdown_is_active(context, dropdown_1_id))

def test_returns_false_if_no_menu(self) -> None:
"""If no page is passed, the function should return False."""
# Create a menu with the first dropdown linking to a page:
page = wagtail_factories.PageFactory(parent=self.homepage, title="Test Page")
menu = nav_factories.NavMenuFactory(
dropdowns__0__dropdown__button__link_to="page",
dropdowns__0__dropdown__button__page=page,
)

dropdown_1_id = nav_tags.get_dropdown_id(menu=menu, idx=0)

# No page is passed
context = {"page": page}
# Should return False
self.assertFalse(nav_tags.check_if_dropdown_is_active(context, dropdown_1_id))


class TestCheckIfLinkIsActive(test_base.WagtailpagesTestCase):
def test_active_link_check(self) -> None:
Expand Down Expand Up @@ -216,3 +257,19 @@ def test_homepage_should_never_be_marked_as_active(self):
context = {"page": self.homepage, "menu": menu}
# Even so, the homepage should not be marked as active:
self.assertFalse(nav_tags.check_if_link_is_active(context, homepage_link))

def test_no_page_returns_false(self) -> None:
"""If no page is passed, the function should return False."""
# Create a menu with the first dropdown linking to a page:
page = wagtail_factories.PageFactory(parent=self.homepage, title="Test Page")
menu = nav_factories.NavMenuFactory(
dropdowns__0__dropdown__button__link_to="page",
dropdowns__0__dropdown__button__page=page,
)

homepage_link = menu.dropdowns[0].value["button"]

# No page is passed
context: dict[Any, Any] = {}
# Should return False
self.assertFalse(nav_tags.check_if_link_is_active(context, homepage_link))

0 comments on commit 2739576

Please sign in to comment.