import os
import sys
from pathlib import Path
from django.forms.widgets import Media
from django.template import Context, Template
from django.templatetags.static import static
from django.test import override_settings
from django.utils.html import format_html, html_safe
from django.utils.safestring import mark_safe
from django_components import Component, registry, render_dependencies, types
from .django_test_setup import setup_test_config
from .testutils import BaseTestCase, autodiscover_with_cleanup
setup_test_config({"autodiscover": False})
class InlineComponentTest(BaseTestCase):
def test_html(self):
class InlineHTMLComponent(Component):
template = "
Hello Inline
"
self.assertHTMLEqual(
InlineHTMLComponent.render(),
"Hello Inline
",
)
def test_inlined_js_and_css(self):
class TestComponent(Component):
template = """
{% load component_tags %}
{% component_js_dependencies %}
{% component_css_dependencies %}
Content
"""
css = ".html-css-only { color: blue; }"
js = "console.log('HTML and JS only');"
rendered = TestComponent.render()
self.assertInHTML(
"Content
",
rendered,
)
self.assertInHTML(
"",
rendered,
)
self.assertInHTML(
"",
rendered,
)
def test_html_variable(self):
class VariableHTMLComponent(Component):
def get_template(self, context):
return Template("{{ variable }}
")
comp = VariableHTMLComponent("variable_html_component")
context = Context({"variable": "Dynamic Content"})
self.assertHTMLEqual(
comp.render(context),
"Dynamic Content
",
)
def test_html_variable_filtered(self):
class FilteredComponent(Component):
template: types.django_html = """
Var1: {{ var1 }}
Var2 (uppercased): {{ var2|upper }}
"""
def get_context_data(self, var1=None, var2=None):
return {
"var1": var1,
"var2": var2,
}
rendered = FilteredComponent.render(kwargs={"var1": "test1", "var2": "test2"})
self.assertHTMLEqual(
rendered,
"""
Var1: test1
Var2 (uppercased): TEST2
""",
)
class ComponentMediaTests(BaseTestCase):
def test_empty_media(self):
class SimpleComponent(Component):
template: types.django_html = """
{% load component_tags %}
{% component_js_dependencies %}
{% component_css_dependencies %}
Variable: {{ variable }}
"""
class Media:
pass
rendered = SimpleComponent.render()
self.assertEqual(rendered.count("