From 84fcaf7ba744c013e5ecac83f02119208398723c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Stenstr=C3=B6m?= Date: Thu, 14 Jul 2022 12:49:21 +0200 Subject: [PATCH] Add tests for calling template tag with variable. --- tests/test_templatetags.py | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/test_templatetags.py b/tests/test_templatetags.py index 115503d3..0e554037 100644 --- a/tests/test_templatetags.py +++ b/tests/test_templatetags.py @@ -2,6 +2,7 @@ from textwrap import dedent from django.template import Context, Template, TemplateSyntaxError +import django_components from django_components import component from .django_test_setup import * # NOQA @@ -70,6 +71,17 @@ class ComponentTemplateTagTest(SimpleTestCase): rendered = template.render(Context({})) self.assertHTMLEqual(rendered, "Variable: variable\n") + def test_call_with_invalid_name(self): + # Note: No component registered + + template = Template( + '{% load component_tags %}{% component name="test" variable="variable" %}' + ) + with self.assertRaises( + django_components.component_registry.NotRegistered + ): + template.render(Context({})) + def test_single_component_positional_name(self): component.registry.register(name="test", component=SimpleComponent) @@ -111,6 +123,36 @@ class ComponentTemplateTagTest(SimpleTestCase): rendered = template.render(Context({})) self.assertHTMLEqual(rendered, "Variable: variable\n") + def test_component_called_with_variable_as_name(self): + component.registry.register(name="test", component=SimpleComponent) + + template = Template( + """ + {% load component_tags %} + {% with component_name="test" %} + {% component component_name variable="variable" %} + {% endwith %} + """ + ) + rendered = template.render(Context({})) + self.assertHTMLEqual(rendered, "Variable: variable\n") + + def test_component_called_with_invalid_variable_as_name(self): + component.registry.register(name="test", component=SimpleComponent) + + template = Template( + """ + {% load component_tags %} + {% with component_name="BLAHONGA" %} + {% component component_name variable="variable" %} + {% endwith %} + """ + ) + with self.assertRaises( + django_components.component_registry.NotRegistered + ): + template.render(Context({})) + class ComponentSlottedTemplateTagTest(SimpleTestCase): def setUp(self):