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 textwrap import dedent
from django.template import Context, Template, TemplateSyntaxError from django.template import Context, Template, TemplateSyntaxError
@ -62,92 +63,115 @@ class ComponentTemplateTagTest(SimpleTestCase):
# NOTE: component.registry is global, so need to clear before each test # NOTE: component.registry is global, so need to clear before each test
component.registry.clear() 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): def test_single_component(self):
component.registry.register(name="test", component=SimpleComponent) component.registry.register(name="test", component=SimpleComponent)
template = Template( simple_tag_tempate = '{% load component_tags %}{% component name="test" variable="variable" %}'
'{% load component_tags %}{% component name="test" variable="variable" %}' block_tag_template = self.inline_to_block(simple_tag_tempate)
)
rendered = template.render(Context({})) for tag in [simple_tag_tempate, block_tag_template]:
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n") template = Template(tag)
rendered = template.render(Context({}))
self.assertHTMLEqual(
rendered, "Variable: <strong>variable</strong>\n"
)
def test_call_with_invalid_name(self): def test_call_with_invalid_name(self):
# Note: No component registered # Note: No tag registered
template = Template( simple_tag_tempate = '{% load component_tags %}{% component name="test" variable="variable" %}'
'{% load component_tags %}{% component name="test" variable="variable" %}' block_tag_template = self.inline_to_block(simple_tag_tempate)
)
with self.assertRaises(
django_components.component_registry.NotRegistered
):
template.render(Context({}))
def test_single_component_positional_name(self): for tag in [simple_tag_tempate, block_tag_template]:
component.registry.register(name="test", component=SimpleComponent) template = Template(tag)
with self.assertRaises(
template = Template( django_components.component_registry.NotRegistered
'{% load component_tags %}{% component "test" variable="variable" %}' ):
) template.render(Context({}))
rendered = template.render(Context({}))
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" %}'
)
rendered = template.render(Context({}))
expected_outcome = (
"""Variable: <strong>variable</strong>\n"""
"""Variable2: <strong>hej</strong>"""
)
self.assertHTMLEqual(rendered, dedent(expected_outcome))
def test_component_called_with_positional_name(self): def test_component_called_with_positional_name(self):
component.registry.register(name="test", component=SimpleComponent) component.registry.register(name="test", component=SimpleComponent)
template = Template( simple_tag_tempate = '{% load component_tags %}{% component "test" variable="variable" %}'
'{% load component_tags %}{% component "test" variable="variable" %}' block_tag_template = self.inline_to_block(simple_tag_tempate)
)
rendered = template.render(Context({})) for tag in [simple_tag_tempate, block_tag_template]:
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n") template = Template(tag)
rendered = template.render(Context({}))
self.assertHTMLEqual(
rendered, "Variable: <strong>variable</strong>\n"
)
def test_call_component_with_two_variables(self):
component.registry.register(name="test", component=IffedComponent)
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"""
"""Variable2: <strong>hej</strong>"""
)
self.assertHTMLEqual(rendered, dedent(expected_outcome))
def test_component_called_with_singlequoted_name(self): def test_component_called_with_singlequoted_name(self):
component.registry.register(name="test", component=SimpleComponent) component.registry.register(name="test", component=SimpleComponent)
template = Template( simple_tag_tempate = """{% load component_tags %}{% component 'test' variable="variable" %}"""
"{% load component_tags %}{% component 'test' variable=\"variable\" %}" block_tag_template = self.inline_to_block(simple_tag_tempate)
)
rendered = template.render(Context({})) for tag in [simple_tag_tempate, block_tag_template]:
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n") template = Template(tag)
rendered = template.render(Context({}))
self.assertHTMLEqual(
rendered, "Variable: <strong>variable</strong>\n"
)
def test_component_called_with_variable_as_name(self): def test_component_called_with_variable_as_name(self):
component.registry.register(name="test", component=SimpleComponent) component.registry.register(name="test", component=SimpleComponent)
template = Template( simple_tag_tempate = """
"""
{% load component_tags %} {% load component_tags %}
{% with component_name="test" %} {% with component_name="test" %}
{% component component_name variable="variable" %} {% component component_name variable="variable" %}
{% endwith %} {% endwith %}
""" """
) block_tag_template = self.inline_to_block(simple_tag_tempate)
rendered = template.render(Context({}))
self.assertHTMLEqual(rendered, "Variable: <strong>variable</strong>\n") for tag in [simple_tag_tempate, block_tag_template]:
template = Template(tag)
rendered = template.render(Context({}))
self.assertHTMLEqual(
rendered, "Variable: <strong>variable</strong>\n"
)
def test_component_called_with_invalid_variable_as_name(self): def test_component_called_with_invalid_variable_as_name(self):
component.registry.register(name="test", component=SimpleComponent) component.registry.register(name="test", component=SimpleComponent)
template = Template( simple_tag_tempate = """
"""
{% load component_tags %} {% load component_tags %}
{% with component_name="BLAHONGA" %} {% with component_name="BLAHONGA" %}
{% component component_name variable="variable" %} {% component component_name variable="variable" %}
{% endwith %} {% 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( with self.assertRaises(
django_components.component_registry.NotRegistered django_components.component_registry.NotRegistered
): ):