feat: allow to set defaults (#1072)

* feat: allow to set defaults

* docs: update changelog

* refactor: fix new linter errors
This commit is contained in:
Juro Oravec 2025-03-31 10:38:41 +02:00 committed by GitHub
parent 48dd3b7a5a
commit f07818fc7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 553 additions and 36 deletions

View file

@ -20,6 +20,7 @@ from django_components.extension import (
OnComponentInputContext,
OnComponentDataContext,
)
from django_components.extensions.defaults import DefaultsExtension
from django_components.extensions.view import ViewExtension
from django_components.testing import djc_test
@ -125,9 +126,10 @@ def with_registry(on_created: Callable):
class TestExtension:
@djc_test(components_settings={"extensions": [DummyExtension]})
def test_extensions_setting(self):
assert len(app_settings.EXTENSIONS) == 2
assert isinstance(app_settings.EXTENSIONS[0], ViewExtension)
assert isinstance(app_settings.EXTENSIONS[1], DummyExtension)
assert len(app_settings.EXTENSIONS) == 3
assert isinstance(app_settings.EXTENSIONS[0], DefaultsExtension)
assert isinstance(app_settings.EXTENSIONS[1], ViewExtension)
assert isinstance(app_settings.EXTENSIONS[2], DummyExtension)
@djc_test(components_settings={"extensions": [DummyExtension]})
def test_access_component_from_extension(self):
@ -150,7 +152,7 @@ class TestExtension:
class TestExtensionHooks:
@djc_test(components_settings={"extensions": [DummyExtension]})
def test_component_class_lifecycle_hooks(self):
extension = cast(DummyExtension, app_settings.EXTENSIONS[1])
extension = cast(DummyExtension, app_settings.EXTENSIONS[2])
assert len(extension.calls["on_component_class_created"]) == 0
assert len(extension.calls["on_component_class_deleted"]) == 0
@ -182,7 +184,7 @@ class TestExtensionHooks:
@djc_test(components_settings={"extensions": [DummyExtension]})
def test_registry_lifecycle_hooks(self):
extension = cast(DummyExtension, app_settings.EXTENSIONS[1])
extension = cast(DummyExtension, app_settings.EXTENSIONS[2])
assert len(extension.calls["on_registry_created"]) == 0
assert len(extension.calls["on_registry_deleted"]) == 0
@ -219,7 +221,7 @@ class TestExtensionHooks:
return {"name": name}
registry.register("test_comp", TestComponent)
extension = cast(DummyExtension, app_settings.EXTENSIONS[1])
extension = cast(DummyExtension, app_settings.EXTENSIONS[2])
# Verify on_component_registered was called
assert len(extension.calls["on_component_registered"]) == 1
@ -257,14 +259,14 @@ class TestExtensionHooks:
test_slots = {"content": "Some content"}
TestComponent.render(context=test_context, args=("arg1", "arg2"), kwargs={"name": "Test"}, slots=test_slots)
extension = cast(DummyExtension, app_settings.EXTENSIONS[1])
extension = cast(DummyExtension, app_settings.EXTENSIONS[2])
# Verify on_component_input was called with correct args
assert len(extension.calls["on_component_input"]) == 1
input_call: OnComponentInputContext = extension.calls["on_component_input"][0]
assert input_call.component_cls == TestComponent
assert isinstance(input_call.component_id, str)
assert input_call.args == ("arg1", "arg2")
assert input_call.args == ["arg1", "arg2"]
assert input_call.kwargs == {"name": "Test"}
assert len(input_call.slots) == 1
assert isinstance(input_call.slots["content"], Slot)