mirror of
https://github.com/django-components/django-components.git
synced 2025-09-24 14:42:29 +00:00
refactor: prepare registry for custom template tags and docs (#566)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
c202c5a901
commit
d6dec450ed
11 changed files with 491 additions and 94 deletions
|
@ -1,5 +1,7 @@
|
|||
import unittest
|
||||
|
||||
from django.template import Library
|
||||
|
||||
from django_components import AlreadyRegistered, Component, ComponentRegistry, NotRegistered, register, registry
|
||||
|
||||
from .django_test_setup import setup_test_config
|
||||
|
@ -22,6 +24,7 @@ class MockComponentView(Component):
|
|||
|
||||
class ComponentRegistryTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.registry = ComponentRegistry()
|
||||
|
||||
def test_register_class_decorator(self):
|
||||
|
@ -31,6 +34,23 @@ class ComponentRegistryTest(unittest.TestCase):
|
|||
|
||||
self.assertEqual(registry.get("decorated_component"), TestComponent)
|
||||
|
||||
# Cleanup
|
||||
registry.unregister("decorated_component")
|
||||
|
||||
def test_register_class_decorator_custom_registry(self):
|
||||
my_lib = Library()
|
||||
my_reg = ComponentRegistry(library=my_lib)
|
||||
|
||||
self.assertDictEqual(my_reg.all(), {})
|
||||
self.assertDictEqual(registry.all(), {})
|
||||
|
||||
@register("decorated_component", registry=my_reg)
|
||||
class TestComponent(Component):
|
||||
pass
|
||||
|
||||
self.assertDictEqual(my_reg.all(), {"decorated_component": TestComponent})
|
||||
self.assertDictEqual(registry.all(), {})
|
||||
|
||||
def test_simple_register(self):
|
||||
self.registry.register(name="testcomponent", component=MockComponent)
|
||||
self.assertEqual(self.registry.all(), {"testcomponent": MockComponent})
|
||||
|
@ -46,6 +66,44 @@ class ComponentRegistryTest(unittest.TestCase):
|
|||
},
|
||||
)
|
||||
|
||||
def test_unregisters_only_unused_tags(self):
|
||||
self.assertDictEqual(self.registry._tags, {})
|
||||
# NOTE: We preserve the default component tags
|
||||
self.assertIn("component", self.registry.library.tags)
|
||||
|
||||
# Register two components that use the same tag
|
||||
self.registry.register(name="testcomponent", component=MockComponent)
|
||||
self.registry.register(name="testcomponent2", component=MockComponent)
|
||||
|
||||
self.assertDictEqual(
|
||||
self.registry._tags,
|
||||
{
|
||||
"#component": {"testcomponent", "testcomponent2"},
|
||||
"component": {"testcomponent", "testcomponent2"},
|
||||
},
|
||||
)
|
||||
|
||||
self.assertIn("component", self.registry.library.tags)
|
||||
|
||||
# Unregister only one of the components. The tags should remain
|
||||
self.registry.unregister(name="testcomponent")
|
||||
|
||||
self.assertDictEqual(
|
||||
self.registry._tags,
|
||||
{
|
||||
"#component": {"testcomponent2"},
|
||||
"component": {"testcomponent2"},
|
||||
},
|
||||
)
|
||||
|
||||
self.assertIn("component", self.registry.library.tags)
|
||||
|
||||
# Unregister the second components. The tags should be removed
|
||||
self.registry.unregister(name="testcomponent2")
|
||||
|
||||
self.assertDictEqual(self.registry._tags, {})
|
||||
self.assertIn("component", self.registry.library.tags)
|
||||
|
||||
def test_prevent_registering_different_components_with_the_same_name(self):
|
||||
self.registry.register(name="testcomponent", component=MockComponent)
|
||||
with self.assertRaises(AlreadyRegistered):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue