refactor: Fix template caching, expose cached_template, Component.template API changes (#647)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Juro Oravec 2024-09-06 22:40:39 +02:00 committed by GitHub
parent 589e802625
commit 841dd77e91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 347 additions and 56 deletions

View file

@ -76,6 +76,35 @@ else:
optional: NotRequired[int]
# TODO_REMOVE_IN_V1 - Superseded by `self.get_template` in v1
class ComponentOldTemplateApiTest(BaseTestCase):
@parametrize_context_behavior(["django", "isolated"])
def test_get_template_string(self):
class SimpleComponent(Component):
def get_template_string(self, context):
content: types.django_html = """
Variable: <strong>{{ variable }}</strong>
"""
return content
def get_context_data(self, variable=None):
return {
"variable": variable,
}
class Media:
css = "style.css"
js = "script.js"
rendered = SimpleComponent.render(kwargs={"variable": "test"})
self.assertHTMLEqual(
rendered,
"""
Variable: <strong>test</strong>
""",
)
class ComponentTest(BaseTestCase):
class ParentComponent(Component):
template: types.django_html = """
@ -123,7 +152,7 @@ class ComponentTest(BaseTestCase):
pass
with self.assertRaises(ImproperlyConfigured):
EmptyComponent("empty_component").get_template(Context({}))
EmptyComponent("empty_component")._get_template(Context({}))
@parametrize_context_behavior(["django", "isolated"])
def test_template_string_static_inlined(self):
@ -152,7 +181,7 @@ class ComponentTest(BaseTestCase):
@parametrize_context_behavior(["django", "isolated"])
def test_template_string_dynamic(self):
class SimpleComponent(Component):
def get_template_string(self, context):
def get_template(self, context):
content: types.django_html = """
Variable: <strong>{{ variable }}</strong>
"""
@ -225,7 +254,7 @@ class ComponentTest(BaseTestCase):
)
@parametrize_context_behavior(["django", "isolated"])
def test_allows_to_override_get_template(self):
def test_allows_to_return_template(self):
class TestComponent(Component):
def get_context_data(self, variable, **attrs):
return {
@ -1037,7 +1066,6 @@ class ComponentRenderTest(BaseTestCase):
class ComponentHookTest(BaseTestCase):
@parametrize_context_behavior(["django", "isolated"])
def test_on_render_before(self):
class SimpleComponent(Component):
template: types.django_html = """
@ -1075,7 +1103,6 @@ class ComponentHookTest(BaseTestCase):
)
# Check that modifying the context or template does nothing
@parametrize_context_behavior(["django", "isolated"])
def test_on_render_after(self):
captured_content = None