from unittest.mock import Mock from django.http import HttpResponseNotModified from django.template import Template from django.test import override_settings from django_components import Component, registry, types from django_components.middleware import ComponentDependencyMiddleware from .django_test_setup import setup_test_config from .testutils import BaseTestCase, create_and_process_template_response setup_test_config() class SimpleComponent(Component): template: types.django_html = """ Variable: {{ variable }} """ def get_context_data(self, variable, variable2="default"): return { "variable": variable, "variable2": variable2, } class Media: css = "style.css" js = "script.js" class SimpleComponentAlternate(Component): template: types.django_html = """ Variable: {{ variable }} """ def get_context_data(self, variable): return {} class Media: css = "style2.css" js = "script2.js" class SimpleComponentWithSharedDependency(Component): template: types.django_html = """ Variable: {{ variable }} """ def get_context_data(self, variable, variable2="default"): return {} class Media: css = ["style.css", "style2.css"] js = ["script.js", "script2.js"] class MultistyleComponent(Component): template: types.django_html = """ Variable: {{ variable }} """ class Media: css = ["style.css", "style2.css"] js = ["script.js", "script2.js"] @override_settings(COMPONENTS={"RENDER_DEPENDENCIES": True}) class ComponentMediaRenderingTests(BaseTestCase): def test_no_dependencies_when_no_components_used(self): registry.register(name="test", component=SimpleComponent) template_str: types.django_html = """ {% load component_tags %}{% component_dependencies %} """ template = Template(template_str) rendered = create_and_process_template_response(template) self.assertInHTML('' '' "Variable: value\n" ), )