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.form.form import Form, FormLabel # NOTE: We're importing the component classes from the sampleproject, so we're # testing the actual implementation. registry.register("form", Form) registry.register("form_label", FormLabel) @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 TestExampleForm: def test_render_simple_form(self, components_settings): _create_tab_components() template_str: types.django_html = """ {% load component_tags %} {% component "form" %} {% fill "field:project" %}{% endfill %} {% fill "field:option" %}{% endfill %} {% endcomponent %} """ template = Template(template_str) rendered = template.render(Context({})) assertHTMLEqual( rendered, """
""", ) def test_custom_label(self, components_settings): _create_tab_components() template_str = """ {% load component_tags %} {% component "form" %} {% fill "label:project" %}Custom Project Label{% endfill %} {% fill "field:project" %}{% endfill %} {% endcomponent %} """ template = Template(template_str) rendered = template.render(Context({})) assert "Custom Project Label" in rendered assert '