feat: Typing for component inputs and access inputs during render (#585)

This commit is contained in:
Juro Oravec 2024-08-22 23:42:34 +02:00 committed by GitHub
parent 4dd3e3d5b3
commit efd05d6150
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 470 additions and 59 deletions

View file

@ -3,7 +3,7 @@ Tests focusing on the Component class.
For tests focusing on the `component` tag, see `test_templatetags_component.py`
"""
from typing import Dict
from typing import Dict, Tuple, TypedDict, no_type_check
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpRequest, HttpResponse
@ -186,6 +186,88 @@ class ComponentTest(BaseTestCase):
""",
)
def test_typed(self):
TestCompArgs = Tuple[int, str]
class TestCompKwargs(TypedDict):
variable: str
another: int
class TestCompData(TypedDict):
abc: int
class TestCompSlots(TypedDict):
my_slot: str
class TestComponent(Component[TestCompArgs, TestCompKwargs, TestCompData, TestCompSlots]):
def get_context_data(self, var1, var2, variable, another, **attrs):
return {
"variable": variable,
}
def get_template(self, context):
template_str: types.django_html = """
{% load component_tags %}
Variable: <strong>{{ variable }}</strong>
{% slot 'my_slot' / %}
"""
return Template(template_str)
rendered = TestComponent.render(
kwargs={"variable": "test", "another": 1},
args=(123, "str"),
slots={"my_slot": "MY_SLOT"},
)
self.assertHTMLEqual(
rendered,
"""
Variable: <strong>test</strong> MY_SLOT
""",
)
def test_input(self):
tester = self
class TestComponent(Component):
@no_type_check
def get_context_data(self, var1, var2, variable, another, **attrs):
tester.assertEqual(self.input.args, (123, "str"))
tester.assertEqual(self.input.kwargs, {"variable": "test", "another": 1})
tester.assertIsInstance(self.input.context, Context)
tester.assertEqual(self.input.slots, {"my_slot": "MY_SLOT"})
return {
"variable": variable,
}
@no_type_check
def get_template(self, context):
tester.assertEqual(self.input.args, (123, "str"))
tester.assertEqual(self.input.kwargs, {"variable": "test", "another": 1})
tester.assertIsInstance(self.input.context, Context)
tester.assertEqual(self.input.slots, {"my_slot": "MY_SLOT"})
template_str: types.django_html = """
{% load component_tags %}
Variable: <strong>{{ variable }}</strong>
{% slot 'my_slot' / %}
"""
return Template(template_str)
rendered = TestComponent.render(
kwargs={"variable": "test", "another": 1},
args=(123, "str"),
slots={"my_slot": "MY_SLOT"},
)
self.assertHTMLEqual(
rendered,
"""
Variable: <strong>test</strong> MY_SLOT
""",
)
class ComponentRenderTest(BaseTestCase):
@parametrize_context_behavior(["django", "isolated"])