mirror of
https://github.com/django-components/django-components.git
synced 2025-07-07 17:34:59 +00:00
A simple ComponentRegistry to store components.
This commit is contained in:
parent
22b00a4a62
commit
313852ce21
4 changed files with 33 additions and 0 deletions
0
django_components/__init__.py
Normal file
0
django_components/__init__.py
Normal file
9
django_components/component.py
Normal file
9
django_components/component.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
class ComponentRegistry(object):
|
||||
def __init__(self):
|
||||
self._registry = {} # component name -> component_class mapping
|
||||
|
||||
def register(self, name=None, component=None):
|
||||
self._registry[name] = component
|
||||
|
||||
# This variable represents the global component registry
|
||||
registry = ComponentRegistry()
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
24
tests/test_registry.py
Normal file
24
tests/test_registry.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
import unittest
|
||||
from django_components import component
|
||||
|
||||
class MockComponent(object):
|
||||
pass
|
||||
|
||||
class ComponentRegistryTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.registry = component.ComponentRegistry()
|
||||
|
||||
def test_simple_register(self):
|
||||
self.registry.register(name="testcomponent", component=MockComponent)
|
||||
self.assertEqual(
|
||||
self.registry._registry.items(),
|
||||
[("testcomponent", MockComponent)]
|
||||
)
|
||||
|
||||
def test_register_two_components(self):
|
||||
self.registry.register(name="testcomponent", component=MockComponent)
|
||||
self.registry.register(name="testcomponent2", component=MockComponent)
|
||||
self.assertEqual(
|
||||
self.registry._registry.items(),
|
||||
[("testcomponent", MockComponent), ("testcomponent2", MockComponent)]
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue