refactor: Fix #1277 + Cache components' JS/CSS scripts at class creation (#1283)

* refactor: Cache components' JS and CSS scripts at class creation time

* refactor: add test for no template_rendered signal for component with no template
This commit is contained in:
Juro Oravec 2025-07-03 12:27:21 +02:00 committed by GitHub
parent 007009a480
commit c692b7a310
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 138 additions and 58 deletions

View file

@ -31,6 +31,7 @@ from django_components.extension import (
from django_components.extensions.cache import CacheExtension
from django_components.extensions.debug_highlight import DebugHighlightExtension
from django_components.extensions.defaults import DefaultsExtension
from django_components.extensions.dependencies import DependenciesExtension
from django_components.extensions.view import ViewExtension
from django_components.testing import djc_test
@ -219,12 +220,13 @@ class OverrideAssetExtension(ComponentExtension):
class TestExtension:
@djc_test(components_settings={"extensions": [DummyExtension]})
def test_extensions_setting(self):
assert len(app_settings.EXTENSIONS) == 5
assert len(app_settings.EXTENSIONS) == 6
assert isinstance(app_settings.EXTENSIONS[0], CacheExtension)
assert isinstance(app_settings.EXTENSIONS[1], DefaultsExtension)
assert isinstance(app_settings.EXTENSIONS[2], ViewExtension)
assert isinstance(app_settings.EXTENSIONS[3], DebugHighlightExtension)
assert isinstance(app_settings.EXTENSIONS[4], DummyExtension)
assert isinstance(app_settings.EXTENSIONS[2], DependenciesExtension)
assert isinstance(app_settings.EXTENSIONS[3], ViewExtension)
assert isinstance(app_settings.EXTENSIONS[4], DebugHighlightExtension)
assert isinstance(app_settings.EXTENSIONS[5], DummyExtension)
@djc_test(components_settings={"extensions": [DummyExtension]})
def test_access_component_from_extension(self):
@ -263,7 +265,7 @@ class TestExtension:
class TestExtensionHooks:
@djc_test(components_settings={"extensions": [DummyExtension]})
def test_component_class_lifecycle_hooks(self):
extension = cast(DummyExtension, app_settings.EXTENSIONS[4])
extension = cast(DummyExtension, app_settings.EXTENSIONS[5])
assert len(extension.calls["on_component_class_created"]) == 0
assert len(extension.calls["on_component_class_deleted"]) == 0
@ -295,7 +297,7 @@ class TestExtensionHooks:
@djc_test(components_settings={"extensions": [DummyExtension]})
def test_registry_lifecycle_hooks(self):
extension = cast(DummyExtension, app_settings.EXTENSIONS[4])
extension = cast(DummyExtension, app_settings.EXTENSIONS[5])
assert len(extension.calls["on_registry_created"]) == 0
assert len(extension.calls["on_registry_deleted"]) == 0
@ -332,7 +334,7 @@ class TestExtensionHooks:
return {"name": kwargs.get("name", "World")}
registry.register("test_comp", TestComponent)
extension = cast(DummyExtension, app_settings.EXTENSIONS[4])
extension = cast(DummyExtension, app_settings.EXTENSIONS[5])
# Verify on_component_registered was called
assert len(extension.calls["on_component_registered"]) == 1
@ -370,7 +372,7 @@ 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[4])
extension = cast(DummyExtension, app_settings.EXTENSIONS[5])
# Verify on_component_input was called with correct args
assert len(extension.calls["on_component_input"]) == 1
@ -419,7 +421,7 @@ class TestExtensionHooks:
slots={"content": "Some content"},
)
extension = cast(DummyExtension, app_settings.EXTENSIONS[4])
extension = cast(DummyExtension, app_settings.EXTENSIONS[5])
# Verify on_component_rendered was called with correct args
assert len(extension.calls["on_component_rendered"]) == 1
@ -448,7 +450,7 @@ class TestExtensionHooks:
assert rendered == "Hello Some content!"
extension = cast(DummyExtension, app_settings.EXTENSIONS[4])
extension = cast(DummyExtension, app_settings.EXTENSIONS[5])
# Verify on_slot_rendered was called with correct args
assert len(extension.calls["on_slot_rendered"]) == 1
@ -516,7 +518,7 @@ class TestExtensionHooks:
# Render the component to trigger all hooks
TestComponent.render(args=(), kwargs={"name": "Test"})
extension = cast(DummyExtension, app_settings.EXTENSIONS[4])
extension = cast(DummyExtension, app_settings.EXTENSIONS[5])
# on_template_loaded
assert len(extension.calls["on_template_loaded"]) == 1
@ -559,7 +561,7 @@ class TestExtensionHooks:
# Render the component to trigger all hooks
TestComponent.render(args=(), kwargs={"name": "Test"})
extension = cast(DummyExtension, app_settings.EXTENSIONS[4])
extension = cast(DummyExtension, app_settings.EXTENSIONS[5])
# on_template_loaded
# NOTE: The template file gets picked up by 'django.template.loaders.filesystem.Loader',