from pathlib import Path
import pytest
from django.template import Context, Template
from pytest_django.asserts import assertHTMLEqual
from django_components import registry, types
from django_components.testing import djc_test
from tests.testutils import PARAMETRIZE_CONTEXT_BEHAVIOR, setup_test_config
setup_test_config({"autodiscover": False})
# Instead of having to re-define the components from the examples section in documentation,
# we import them directly from sampleproject.
def _create_tab_components():
# Imported lazily, so we import it only once settings are set
from sampleproject.examples.components.tabs.tabs import Tab, Tablist, _TablistImpl
# NOTE: We're importing the component classes from the sampleproject, so we're
# testing the actual implementation.
registry.register("Tab", Tab)
registry.register("Tablist", Tablist)
registry.register("_tabset", _TablistImpl)
@djc_test(
parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR,
components_settings={
"dirs": [
Path(__file__).parent / "components",
# Include the directory where example components are defined
Path(__file__).parent.parent / "sampleproject/examples/components",
],
},
)
class TestExampleTabs:
def test_render_simple_tabs(self, components_settings):
_create_tab_components()
template_str: types.django_html = """
{% load component_tags %}
{% component "Tablist" name="My Tabs" %}
{% component "Tab" header="Tab 1" %}Content 1{% endcomponent %}
{% component "Tab" header="Tab 2" %}Content 2{% endcomponent %}
{% endcomponent %}
"""
template = Template(template_str)
rendered = template.render(Context({}))
if components_settings["context_behavior"] == "django":
comp_id = "ca1bc4b"
else:
comp_id = "ca1bc47"
assertHTMLEqual(
rendered,
f"""