mirror of
https://github.com/django-components/django-components.git
synced 2025-09-26 15:39:08 +00:00
feat: allow different template settings for ComponentRegistries (#615)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
ee5c92ba00
commit
3b1f6088a0
10 changed files with 478 additions and 44 deletions
|
@ -1,19 +1,25 @@
|
|||
import unittest
|
||||
|
||||
from django.template import Library
|
||||
from django.template import Context, Engine, Library, Template
|
||||
from django.test import override_settings
|
||||
|
||||
from django_components import (
|
||||
AlreadyRegistered,
|
||||
Component,
|
||||
ComponentRegistry,
|
||||
ContextBehavior,
|
||||
NotRegistered,
|
||||
RegistrySettings,
|
||||
TagProtectedError,
|
||||
component_formatter,
|
||||
component_shorthand_formatter,
|
||||
register,
|
||||
registry,
|
||||
types,
|
||||
)
|
||||
|
||||
from .django_test_setup import setup_test_config
|
||||
from .testutils import BaseTestCase, parametrize_context_behavior
|
||||
|
||||
setup_test_config({"autodiscover": False})
|
||||
|
||||
|
@ -133,6 +139,81 @@ class ComponentRegistryTest(unittest.TestCase):
|
|||
self.registry.unregister(name="testcomponent")
|
||||
|
||||
|
||||
class MultipleComponentRegistriesTest(BaseTestCase):
|
||||
@parametrize_context_behavior(["django", "isolated"])
|
||||
def test_different_registries_have_different_settings(self):
|
||||
library_a = Library()
|
||||
registry_a = ComponentRegistry(
|
||||
library=library_a,
|
||||
settings=RegistrySettings(
|
||||
CONTEXT_BEHAVIOR=ContextBehavior.ISOLATED,
|
||||
TAG_FORMATTER=component_shorthand_formatter,
|
||||
),
|
||||
)
|
||||
|
||||
library_b = Library()
|
||||
registry_b = ComponentRegistry(
|
||||
library=library_b,
|
||||
settings=RegistrySettings(
|
||||
CONTEXT_BEHAVIOR=ContextBehavior.DJANGO,
|
||||
TAG_FORMATTER=component_formatter,
|
||||
),
|
||||
)
|
||||
|
||||
# NOTE: We cannot load the Libraries above using `{% load xxx %}` tag, because
|
||||
# for that we'd need to register a Django app and whatnot.
|
||||
# Instead, we insert the Libraries directly into the engine's builtins.
|
||||
engine = Engine.get_default()
|
||||
|
||||
# Add the custom template tags to Django's built-in tags
|
||||
engine.template_builtins.append(library_a)
|
||||
engine.template_builtins.append(library_b)
|
||||
|
||||
class SimpleComponent(Component):
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
Variable: <strong>{{ variable }}</strong>
|
||||
Slot: {% slot "default" default / %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable=None):
|
||||
return {
|
||||
"variable": variable,
|
||||
}
|
||||
|
||||
registry_a.register("simple_a", SimpleComponent)
|
||||
registry_b.register("simple_b", SimpleComponent)
|
||||
|
||||
template_str: types.django_html = """
|
||||
{% simple_a variable=123 %}
|
||||
SLOT 123
|
||||
{% endsimple_a %}
|
||||
{% component "simple_b" variable=123 %}
|
||||
SLOT ABC
|
||||
{% endcomponent %}
|
||||
"""
|
||||
template = Template(template_str)
|
||||
|
||||
rendered = template.render(Context({}))
|
||||
|
||||
self.assertHTMLEqual(
|
||||
rendered,
|
||||
"""
|
||||
Variable: <strong>123</strong>
|
||||
Slot:
|
||||
SLOT 123
|
||||
|
||||
Variable: <strong>123</strong>
|
||||
Slot:
|
||||
SLOT ABC
|
||||
""",
|
||||
)
|
||||
|
||||
# Remove the custom template tags to clean up after tests
|
||||
engine.template_builtins.remove(library_a)
|
||||
engine.template_builtins.remove(library_b)
|
||||
|
||||
|
||||
class ProtectedTagsTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue