mirror of
https://github.com/django-components/django-components.git
synced 2025-08-31 11:17:21 +00:00
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:
parent
589e802625
commit
841dd77e91
14 changed files with 347 additions and 56 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue