Update tests to include component_block versions too.

This commit is contained in:
Emil Stenström 2022-07-14 13:29:35 +02:00
parent 84fcaf7ba7
commit 812e455dec

View file

@ -1,3 +1,4 @@
import re
from textwrap import dedent
from django.template import Context, Template, TemplateSyntaxError
@ -62,42 +63,63 @@ class ComponentTemplateTagTest(SimpleTestCase):
# NOTE: component.registry is global, so need to clear before each test
component.registry.clear()
def inline_to_block(self, tag):
return re.sub(
r"({% component (.*) %})",
r"{% component_block \2 %}{% endcomponent_block %}",
tag,
)
def test_single_component(self):
component.registry.register(name="test", component=SimpleComponent)
template = Template(
'{% load component_tags %}{% component name="test" variable="variable" %}'
)
simple_tag_tempate = '{% load component_tags %}{% component name="test" variable="variable" %}'
block_tag_template = self.inline_to_block(simple_tag_tempate)
for tag in [simple_tag_tempate, block_tag_template]:
template = Template(tag)
rendered = template.render(Context({}))
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
self.assertHTMLEqual(
rendered, "Variable: <strong>variable</strong>\n"
)
def test_call_with_invalid_name(self):
# Note: No component registered
# Note: No tag registered
template = Template(
'{% load component_tags %}{% component name="test" variable="variable" %}'
)
simple_tag_tempate = '{% load component_tags %}{% component name="test" variable="variable" %}'
block_tag_template = self.inline_to_block(simple_tag_tempate)
for tag in [simple_tag_tempate, block_tag_template]:
template = Template(tag)
with self.assertRaises(
django_components.component_registry.NotRegistered
):
template.render(Context({}))
def test_single_component_positional_name(self):
def test_component_called_with_positional_name(self):
component.registry.register(name="test", component=SimpleComponent)
template = Template(
'{% load component_tags %}{% component "test" variable="variable" %}'
)
simple_tag_tempate = '{% load component_tags %}{% component "test" variable="variable" %}'
block_tag_template = self.inline_to_block(simple_tag_tempate)
for tag in [simple_tag_tempate, block_tag_template]:
template = Template(tag)
rendered = template.render(Context({}))
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
self.assertHTMLEqual(
rendered, "Variable: <strong>variable</strong>\n"
)
def test_call_component_with_two_variables(self):
component.registry.register(name="test", component=IffedComponent)
template = Template(
"{% load component_tags %}"
'{% component name="test" variable="variable" variable2="hej" %}'
)
simple_tag_tempate = """
{% load component_tags %}
{% component name="test" variable="variable" variable2="hej" %}
"""
block_tag_template = self.inline_to_block(simple_tag_tempate)
for tag in [simple_tag_tempate, block_tag_template]:
template = Template(tag)
rendered = template.render(Context({}))
expected_outcome = (
"""Variable: <strong>variable</strong>\n"""
@ -105,49 +127,51 @@ class ComponentTemplateTagTest(SimpleTestCase):
)
self.assertHTMLEqual(rendered, dedent(expected_outcome))
def test_component_called_with_positional_name(self):
component.registry.register(name="test", component=SimpleComponent)
template = Template(
'{% load component_tags %}{% component "test" variable="variable" %}'
)
rendered = template.render(Context({}))
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
def test_component_called_with_singlequoted_name(self):
component.registry.register(name="test", component=SimpleComponent)
template = Template(
"{% load component_tags %}{% component 'test' variable=\"variable\" %}"
)
simple_tag_tempate = """{% load component_tags %}{% component 'test' variable="variable" %}"""
block_tag_template = self.inline_to_block(simple_tag_tempate)
for tag in [simple_tag_tempate, block_tag_template]:
template = Template(tag)
rendered = template.render(Context({}))
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
self.assertHTMLEqual(
rendered, "Variable: <strong>variable</strong>\n"
)
def test_component_called_with_variable_as_name(self):
component.registry.register(name="test", component=SimpleComponent)
template = Template(
"""
simple_tag_tempate = """
{% load component_tags %}
{% with component_name="test" %}
{% component component_name variable="variable" %}
{% endwith %}
"""
)
block_tag_template = self.inline_to_block(simple_tag_tempate)
for tag in [simple_tag_tempate, block_tag_template]:
template = Template(tag)
rendered = template.render(Context({}))
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n")
self.assertHTMLEqual(
rendered, "Variable: <strong>variable</strong>\n"
)
def test_component_called_with_invalid_variable_as_name(self):
component.registry.register(name="test", component=SimpleComponent)
template = Template(
"""
simple_tag_tempate = """
{% load component_tags %}
{% with component_name="BLAHONGA" %}
{% component component_name variable="variable" %}
{% endwith %}
"""
)
block_tag_template = self.inline_to_block(simple_tag_tempate)
for tag in [simple_tag_tempate, block_tag_template]:
template = Template(tag)
with self.assertRaises(
django_components.component_registry.NotRegistered
):