refactor: Add Node metadata (#1229)

* refactor: `Slot.source` replaced with `Slot.fill_node`, new `Component.node` property, and `slot_node` available in `on_slot_rendered()` hook.

* refactor: fix windows path error in tests
This commit is contained in:
Juro Oravec 2025-06-03 12:58:48 +02:00 committed by GitHub
parent abc6be343e
commit 46e524e37d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 728 additions and 103 deletions

View file

@ -3,6 +3,7 @@ Tests focusing on the Component class.
For tests focusing on the `component` tag, see `test_templatetags_component.py`
"""
import os
import re
from typing import Any, NamedTuple
@ -17,6 +18,7 @@ from pytest_django.asserts import assertHTMLEqual, assertInHTML
from django_components import (
Component,
ComponentRegistry,
ComponentView,
Slot,
SlotInput,
@ -598,6 +600,110 @@ class TestComponentRenderAPI:
assert comp.slots == {} # type: ignore[attr-defined]
assert comp.context == Context() # type: ignore[attr-defined]
def test_metadata__template(self):
comp: Any = None
@register("test")
class TestComponent(Component):
template = "hello"
def get_template_data(self, args, kwargs, slots, context):
nonlocal comp
comp = self
template_str: types.django_html = """
{% load component_tags %}
<div class="test-component">
{% component "test" / %}
</div>
"""
template = Template(template_str)
rendered = template.render(Context())
assertHTMLEqual(rendered, '<div class="test-component">hello</div>')
assert isinstance(comp, TestComponent)
assert isinstance(comp.outer_context, Context)
assert comp.outer_context == Context()
assert isinstance(comp.registry, ComponentRegistry)
assert comp.registered_name == "test"
assert comp.node is not None
assert comp.node.template_component is None
assert comp.node.template_name == "<unknown source>"
def test_metadata__component(self):
comp: Any = None
@register("test")
class TestComponent(Component):
template = "hello"
def get_template_data(self, args, kwargs, slots, context):
nonlocal comp
comp = self
class Outer(Component):
template = "{% component 'test' only / %}"
rendered = Outer.render()
assert rendered == 'hello'
assert isinstance(comp, TestComponent)
assert isinstance(comp.outer_context, Context)
assert comp.outer_context is not comp.context
assert isinstance(comp.registry, ComponentRegistry)
assert comp.registered_name == "test"
assert comp.node is not None
assert comp.node.template_component == Outer
if os.name == "nt":
assert comp.node.template_name.endswith("tests\\test_component.py::Outer") # type: ignore
else:
assert comp.node.template_name.endswith("tests/test_component.py::Outer") # type: ignore
def test_metadata__python(self):
comp: Any = None
@register("test")
class TestComponent(Component):
template = "hello"
def get_template_data(self, args, kwargs, slots, context):
nonlocal comp
comp = self
rendered = TestComponent.render(
context=Context(),
args=(),
kwargs={},
slots={},
deps_strategy="document",
render_dependencies=True,
request=None,
outer_context=Context(),
registry=ComponentRegistry(),
registered_name="test",
)
assert rendered == 'hello'
assert isinstance(comp, TestComponent)
assert isinstance(comp.outer_context, Context)
assert comp.outer_context == Context()
assert isinstance(comp.registry, ComponentRegistry)
assert comp.registered_name == "test"
assert comp.node is None
@djc_test
class TestComponentTemplateVars: