Highlight selected item in proposition navigation

It is standard practice when using GOV.UK template to highlight the
selected navigation item in the propositional navigation (black bar) by
colouring it blue.

This commit adds a new subclass of `Navigation` with the mapping needed
to decide which pages belong to which item in the navigation (or none
at all).
This commit is contained in:
Chris Hill-Scott
2018-04-25 11:11:37 +01:00
parent e1fd63e184
commit 8a7525a809
4 changed files with 328 additions and 34 deletions

View File

@@ -1,38 +1,91 @@
import pytest
from tests.conftest import SERVICE_ONE_ID, app_
from app.navigation import MainNavigation
from app.navigation import HeaderNavigation, MainNavigation
all_endpoints = [
rule.endpoint for rule in next(app_(None)).url_map.iter_rules()
]
def test_navigation_items_are_properly_defined():
for endpoint in MainNavigation().endpoints_with_navigation:
assert endpoint in all_endpoints
assert endpoint not in MainNavigation().endpoints_without_navigation
assert MainNavigation().endpoints_with_navigation.count(endpoint) == 1
def test_excluded_navigation_items_are_properly_defined():
for endpoint in MainNavigation().endpoints_without_navigation:
assert endpoint in all_endpoints
assert endpoint not in MainNavigation().endpoints_with_navigation
assert MainNavigation().endpoints_without_navigation.count(endpoint) == 1
def test_all_endpoints_are_covered():
for endpoint in all_endpoints:
assert endpoint in (
MainNavigation().endpoints_with_navigation +
MainNavigation().endpoints_without_navigation
@pytest.mark.parametrize('navigation_instance', [
MainNavigation(),
HeaderNavigation(),
])
def test_navigation_items_are_properly_defined(navigation_instance):
for endpoint in navigation_instance.endpoints_with_navigation:
assert (
endpoint in all_endpoints
), '{} is not a real endpoint (in {}.mapping)'.format(
endpoint,
type(navigation_instance).__name__
)
assert (
endpoint not in navigation_instance.endpoints_without_navigation
), '{} is listed in {}.mapping and {}.exclude'.format(
endpoint,
type(navigation_instance).__name__,
type(navigation_instance).__name__,
)
assert (
navigation_instance.endpoints_with_navigation.count(endpoint) == 1
), '{} found more than once in {}.mapping'.format(
endpoint,
type(navigation_instance).__name__
)
@pytest.mark.parametrize('navigation_instance', [
MainNavigation(),
HeaderNavigation(),
])
def test_excluded_navigation_items_are_properly_defined(navigation_instance):
for endpoint in navigation_instance.endpoints_without_navigation:
assert (
endpoint in all_endpoints
), '{} is not a real endpoint (in {}.exclude)'.format(
endpoint,
type(navigation_instance).__name__
)
assert (
endpoint not in navigation_instance.endpoints_with_navigation
), '{} is listed in {}.exclude and {}.mapping'.format(
endpoint,
type(navigation_instance).__name__,
type(navigation_instance).__name__,
)
assert (
navigation_instance.endpoints_without_navigation.count(endpoint) == 1
), '{} found more than once in {}.exclude'.format(
endpoint,
type(navigation_instance).__name__
)
@pytest.mark.parametrize('navigation_instance', [
MainNavigation(),
HeaderNavigation(),
])
def test_all_endpoints_are_covered(navigation_instance):
for endpoint in all_endpoints:
assert endpoint in (
navigation_instance.endpoints_with_navigation +
navigation_instance.endpoints_without_navigation
), '{} is not listed or excluded in {}'.format(
endpoint,
type(navigation_instance).__name__
)
@pytest.mark.parametrize('navigation_instance', [
MainNavigation(),
HeaderNavigation(),
])
@pytest.mark.xfail(raises=KeyError)
def test_raises_on_invalid_navigation_item(client_request):
MainNavigation().is_selected('foo')
def test_raises_on_invalid_navigation_item(
client_request, navigation_instance
):
navigation_instance.is_selected('foo')
@pytest.mark.parametrize('endpoint, selected_nav_item', [
@@ -51,3 +104,18 @@ def test_a_page_should_nave_selected_navigation_item(
selected_nav_items = page.select('.navigation a.selected')
assert len(selected_nav_items) == 1
assert selected_nav_items[0].text.strip() == selected_nav_item
@pytest.mark.parametrize('endpoint, selected_nav_item', [
('main.documentation', 'Documentation'),
('main.support', 'Support'),
])
def test_a_page_should_nave_selected_header_navigation_item(
client_request,
endpoint,
selected_nav_item,
):
page = client_request.get(endpoint, service_id=SERVICE_ONE_ID)
selected_nav_items = page.select('#proposition-links a.active')
assert len(selected_nav_items) == 1
assert selected_nav_items[0].text.strip() == selected_nav_item