Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement optional default for override_tag #125

Merged
merged 5 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ The override process has two parts:
1. Override your template tag with a mock implementation
2. Define fake result for your tag in a `yaml` file


### Providing a default value for template tags
To provide a default for a template tag, you need to provide a keyword argument default when overriding your tag.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/default/default_html/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


```
from pattern_library.monkey_utils import override_tag

override_tag(register, 'a_tag_name', default="https://potato.com")
William-Blackie marked this conversation as resolved.
Show resolved Hide resolved
```

This default is used for any tag that's not passed its own context, allowing specificity for those elements that need it while preventing the tag from breaking when it's not structural to the component.

#### limitation
William-Blackie marked this conversation as resolved.
Show resolved Hide resolved
Currently the default override is limited to direct references only i.e. {% a_tag_name page.url %} and not {% a_tag_name page.url as variable_name %}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the wording here. What's actually not supported is modifying context, so it might be better to say something along the lines of "Only providing a default for the output of the tag is supporting, it's not possible to modify context like <example of simpletag as>".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, thanks.


### When do I need to override a template tag?

Ideally your pattern library should be independent, so it doesn't fail when
Expand Down
9 changes: 7 additions & 2 deletions pattern_library/monkey_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
)

logger = logging.getLogger(__name__)
UNSPECIFIED = object()


def override_tag(register, name):
def override_tag(register, name, default_html=None):
"""
An utility that helps you override original tags for use in your pattern library.

Expand Down Expand Up @@ -75,9 +76,13 @@ def node_render(context):

# Render result instead of the tag
return result
elif default_html is not UNSPECIFIED:
# Render provided default;
# if no stub data supplied.
return default_html
else:
logger.warning(
'No stub data defined for the "%s" tag in the "%s" template',
'No default or stub data defined for the "%s" tag in the "%s" template',
tag_name, current_template_name
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
SAND{% error_tag empty_string %}WICH
SAND{% error_tag none %}WICH
SAND{% error_tag zero %}WICH

POTA{% default_html_tag page.url %}TO
POTA{% default_html_tag page.child.url %}TO
POTA{% default_html_tag None %}TO

POTA{% default_html_tag_falsey empty_string %}TO1
POTA{% default_html_tag_falsey none %}TO2
POTA{% default_html_tag_falsey zero %}TO3
14 changes: 14 additions & 0 deletions tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ tags:
raw: null
zero:
raw: 0
default_html_tag:
page.url:
raw: "example"
page.child.url:
raw: "another_example"
# None handled by default html value.
default_html_tag_falsey:
empty_string:
raw: ''
none:
raw: null
zero:
raw: 0

17 changes: 17 additions & 0 deletions tests/templatetags/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,27 @@
register = template.Library()


# Basic template tag
@register.simple_tag
def error_tag(arg=None):
"Just raise an exception, never do anything"
raise Exception("error_tag raised an exception")


# Template tag to to test setting a default html value.
@register.simple_tag()
def default_html_tag(arg=None):
"Just raise an exception, never do anything"
raise Exception("default_tag raised an exception")


# Template tag to to test setting a default html value that is falsey.
@register.simple_tag()
def default_html_tag_falsey(arg=None):
"Just raise an exception, never do anything"
raise Exception("default_tag raised an exception")


override_tag(register, 'error_tag')
override_tag(register, 'default_html_tag', default_html="https://potato.com")
override_tag(register, 'default_html_tag_falsey', default_html=None)
21 changes: 20 additions & 1 deletion tests/tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .utils import reverse


class TagsTesCase(SimpleTestCase):
class TagsTestCase(SimpleTestCase):
def test_falsey_raw_values_for_tag_output(self):
response = self.client.get(reverse(
'pattern_library:render_pattern',
Expand All @@ -12,3 +12,22 @@ def test_falsey_raw_values_for_tag_output(self):
self.assertContains(response, "SANDWICH")
self.assertContains(response, "SANDNoneWICH")
self.assertContains(response, "SAND0WICH")

def test_default_html_override(self):
response = self.client.get(reverse(
'pattern_library:render_pattern',
kwargs={'pattern_template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'},
))

self.assertContains(response, "POTAexampleTO")
self.assertContains(response, "POTAanother_exampleTO")
self.assertContains(response, "POTAhttps://potato.comTO")

def test_falsey_default_html_overide(self):
response = self.client.get(reverse(
'pattern_library:render_pattern',
kwargs={'pattern_template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'},
))
self.assertContains(response, "POTATO1")
self.assertContains(response, "POTANoneTO2")
self.assertContains(response, "POTA0TO3")