django-components/tests/test_autodiscover.py
Juro Oravec c692b7a310
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
2025-07-03 12:27:21 +02:00

101 lines
4.1 KiB
Python

import sys
import pytest
from django_components import AlreadyRegistered, registry
from django_components.autodiscovery import autodiscover, import_libraries
from django_components.testing import djc_test
from .testutils import setup_test_config
setup_test_config({"autodiscover": False})
@djc_test
class TestAutodiscover:
def test_autodiscover(self):
all_components = registry.all().copy()
assert "single_file_component" not in all_components
assert "multi_file_component" not in all_components
assert "relative_file_component" not in all_components
assert "relative_file_pathobj_component" not in all_components
try:
modules = autodiscover(map_module=lambda p: "tests." + p if p.startswith("components") else p)
except AlreadyRegistered:
pytest.fail("Autodiscover should not raise AlreadyRegistered exception")
assert "tests.components" in modules
assert "tests.components.single_file" in modules
assert "tests.components.staticfiles.staticfiles" in modules
assert "tests.components.multi_file.multi_file" in modules
assert "tests.components.relative_file_pathobj.relative_file_pathobj" in modules
assert "tests.components.relative_file.relative_file" in modules
assert "tests.test_app.components.app_lvl_comp.app_lvl_comp" in modules
assert "django_components.components" in modules
assert "django_components.components.dynamic" in modules
all_components = registry.all().copy()
assert "single_file_component" in all_components
assert "multi_file_component" in all_components
assert "relative_file_component" in all_components
assert "relative_file_pathobj_component" in all_components
@djc_test
class TestImportLibraries:
@djc_test(
components_settings={
"libraries": ["tests.components.single_file", "tests.components.multi_file.multi_file"]
}
)
def test_import_libraries(self):
all_components = registry.all().copy()
assert "single_file_component" not in all_components
assert "multi_file_component" not in all_components
# Ensure that the modules are executed again after import
if "tests.components.single_file" in sys.modules:
del sys.modules["tests.components.single_file"]
if "tests.components.multi_file.multi_file" in sys.modules:
del sys.modules["tests.components.multi_file.multi_file"]
try:
modules = import_libraries()
except AlreadyRegistered:
pytest.fail("Autodiscover should not raise AlreadyRegistered exception")
assert "tests.components.single_file" in modules
assert "tests.components.multi_file.multi_file" in modules
all_components = registry.all().copy()
assert "single_file_component" in all_components
assert "multi_file_component" in all_components
@djc_test(
components_settings={
"libraries": ["components.single_file", "components.multi_file.multi_file"]
}
)
def test_import_libraries_map_modules(self):
all_components = registry.all().copy()
assert "single_file_component" not in all_components
assert "multi_file_component" not in all_components
# Ensure that the modules are executed again after import
if "tests.components.single_file" in sys.modules:
del sys.modules["tests.components.single_file"]
if "tests.components.multi_file.multi_file" in sys.modules:
del sys.modules["tests.components.multi_file.multi_file"]
try:
modules = import_libraries(map_module=lambda p: "tests." + p if p.startswith("components") else p)
except AlreadyRegistered:
pytest.fail("Autodiscover should not raise AlreadyRegistered exception")
assert "tests.components.single_file" in modules
assert "tests.components.multi_file.multi_file" in modules
all_components = registry.all().copy()
assert "single_file_component" in all_components
assert "multi_file_component" in all_components