Skip to content

Commit

Permalink
Add tests for govuk link button template tag
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincarrogan committed Dec 19, 2024
1 parent 7b149bf commit c1be8f9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions lite_content/lite_internal_frontend/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
queues, # noqa
picklists, # noqa
routing_rules, # noqa
tests, # noqa
) # noqa

# Buttons
Expand Down
2 changes: 2 additions & 0 deletions lite_content/lite_internal_frontend/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class TestPage:
BUTTON = "Test Button"
19 changes: 17 additions & 2 deletions unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2602,12 +2602,27 @@ def _create_files(*files):
return _create_files


@pytest.fixture
@pytest.fixture()
def authorized_client(client):
return client


@pytest.fixture()
def mock_request(rf, authorized_client):
request = rf.get("/")
request.session = authorized_client.session
request.requests_session = requests.Session()
yield request
return request


@pytest.fixture()
def render_template_string(mock_request):
def _render_template_string(template_string):
template = Template(template_string)
context = RequestContext(mock_request, {})
return template.render(context)

return _render_template_string


@pytest.fixture()
Expand Down
31 changes: 31 additions & 0 deletions unit_tests/lite_forms/templatetags/test_custom_tags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest

from pytest_django.asserts import assertHTMLEqual

from lite_forms.templatetags import custom_tags


Expand All @@ -12,3 +14,32 @@
)
def test_file_type(filename, expected):
assert expected == custom_tags.file_type(filename)


@pytest.mark.parametrize(
"input, expected",
[
(
"{% govuk_link_button text='tests.TestPage.BUTTON' url='core:index' %}",
'<a href="/" role="button" draggable="false" class="govuk-button" data-module="govuk-button">Test Button</a>',
),
(
"{% govuk_link_button text='tests.TestPage.BUTTON' url='core:index' id='test-id' classes='govuk-button--secondary' %}",
'<a id="button-test-id" href="/" role="button" draggable="false" class="govuk-button govuk-button--secondary" data-module="govuk-button">Test Button</a>',
),
(
"{% govuk_link_button text='tests.TestPage.BUTTON' url='core:index' show_chevron=True %}",
'<a href="/" role="button" draggable="false" class="govuk-button" data-module="govuk-button">Test Button<svg aria-hidden="true" class="govuk-button__start-icon" focusable="false" height="15" viewbox="0 0 33 43" width="13" xmlns="http://www.w3.org/2000/svg"><path d="M0 0h13l20 20-20 20H0l20-20z" fill="currentColor"></svg></a>',
),
(
"{% govuk_link_button text='tests.TestPage.BUTTON' url='core:index' query_params='?foo=bar' %}",
'<a href="/?foo=bar" role="button" draggable="false" class="govuk-button" data-module="govuk-button">Test Button</a>',
),
(
"{% govuk_link_button text='tests.TestPage.BUTTON' url='core:index' hidden=True%}",
'<a href="/" role="button" draggable="false" class="govuk-button" data-module="govuk-button" style="display: none;">Test Button</a>',
),
],
)
def test_govuk_link_button(render_template_string, input, expected):
assertHTMLEqual(render_template_string(input), expected)

0 comments on commit c1be8f9

Please sign in to comment.