django-components/tests/test_template.py
Juro Oravec 28b61c1609
refactor: Update docs and tests to use get_template_data() (#1161)
* refactor: update docs and tests to use get_template_data()

* refactor: fix linting

* docs: add note about difference between the two methods
2025-05-03 12:04:10 +02:00

46 lines
1.5 KiB
Python

from django.template import Context, Template
from django_components import Component, cached_template, types
from django_components.testing import djc_test
from .testutils import setup_test_config
setup_test_config({"autodiscover": False})
@djc_test
class TestTemplateCache:
def test_cached_template(self):
template_1 = cached_template("Variable: <strong>{{ variable }}</strong>")
template_1._test_id = "123"
template_2 = cached_template("Variable: <strong>{{ variable }}</strong>")
assert template_2._test_id == "123"
def test_cached_template_accepts_class(self):
class MyTemplate(Template):
pass
template = cached_template("Variable: <strong>{{ variable }}</strong>", MyTemplate)
assert isinstance(template, MyTemplate)
def test_component_template_is_cached(self):
class SimpleComponent(Component):
def get_template(self, context):
content: types.django_html = """
Variable: <strong>{{ variable }}</strong>
"""
return content
def get_template_data(self, args, kwargs, slots, context):
return {
"variable": kwargs.get("variable", None),
}
comp = SimpleComponent()
template_1 = comp._get_template(Context({}), component_id="123")
template_1._test_id = "123"
template_2 = comp._get_template(Context({}), component_id="123")
assert template_2._test_id == "123"