mirror of
https://github.com/django-components/django-components.git
synced 2025-08-08 16:27:59 +00:00

* feat: add decorator for writing component tests * refactor: udpate changelog + update deps pins * refactor: fix deps * refactor: make cached_ref into generic and fix linter errors * refactor: fix coverage testing * refactor: use global var instead of env var for is_testing state
46 lines
1.5 KiB
Python
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_context_data(self, variable=None):
|
|
return {
|
|
"variable": variable,
|
|
}
|
|
|
|
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"
|