Pass context into component tags by default, and let components disable with "only" (#20)

Co-authored-by: rbeard0330 <@dul2k3BKW6m>
This commit is contained in:
rbeard0330 2021-01-25 06:31:54 -05:00 committed by GitHub
parent a99c8d7ad0
commit 2633c3f08f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 229 additions and 99 deletions

View file

@ -6,6 +6,18 @@ from .django_test_setup import * # NOQA
from .testutils import Django111CompatibleSimpleTestCase as SimpleTestCase
class SimpleComponent(component.Component):
def context(self, variable=None):
return {"variable": variable} if variable is not None else {}
def template(self, context):
return "simple_template.html"
@staticmethod
def expected_output(variable_value):
return 'Variable: < strong > {} < / strong >'.format(variable_value)
class ParentComponent(component.Component):
def context(self):
return {
@ -27,11 +39,13 @@ class ParentComponentWithArgs(component.Component):
class VariableDisplay(component.Component):
def context(self, shadowing_variable, new_variable):
return {
"shadowing_variable": shadowing_variable,
"unique_variable": new_variable
}
def context(self, shadowing_variable=None, new_variable=None):
context = {}
if shadowing_variable is not None:
context['shadowing_variable'] = shadowing_variable
if new_variable is not None:
context['unique_variable'] = new_variable
return context
def template(self, context):
return "variable_display.html"
@ -53,10 +67,20 @@ class IncrementerComponent(component.Component):
return "incrementer.html"
class OuterContextComponent(component.Component):
def context(self):
return self.outer_context
def template(self, context):
return "simple_template.html"
component.registry.register(name='parent_component', component=ParentComponent)
component.registry.register(name='parent_with_args', component=ParentComponentWithArgs)
component.registry.register(name='variable_display', component=VariableDisplay)
component.registry.register(name='incrementer', component=IncrementerComponent)
component.registry.register(name='simple_component', component=SimpleComponent)
component.registry.register(name='outer_context_component', component=OuterContextComponent)
class ContextTests(SimpleTestCase):
@ -225,3 +249,39 @@ class ContextCalledOnceTests(SimpleTestCase):
rendered = template.render(Context()).strip()
self.assertEqual(rendered, '<p class="incrementer">value=4;calls=1</p>\n<p>slot</p>', rendered)
class ComponentsCanAccessOuterContext(SimpleTestCase):
def test_simple_component_can_use_outer_context(self):
template = Template("{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' %}")
rendered = template.render(Context({'variable': 'outer_value'})).strip()
self.assertIn('outer_value', rendered, rendered)
class IsolatedContextTests(SimpleTestCase):
def test_simple_component_can_pass_outer_context_in_args(self):
template = Template("{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' variable only %}")
rendered = template.render(Context({'variable': 'outer_value'})).strip()
self.assertIn('outer_value', rendered, rendered)
def test_simple_component_cannot_use_outer_context(self):
template = Template("{% load component_tags %}{% component_dependencies %}"
"{% component 'simple_component' only %}")
rendered = template.render(Context({'variable': 'outer_value'})).strip()
self.assertNotIn('outer_value', rendered, rendered)
class OuterContextPropertyTests(SimpleTestCase):
def test_outer_context_property_with_component(self):
template = Template("{% load component_tags %}{% component_dependencies %}"
"{% component 'outer_context_component' only %}")
rendered = template.render(Context({'variable': 'outer_value'})).strip()
self.assertIn('outer_value', rendered, rendered)
def test_outer_context_property_with_component_block(self):
template = Template("{% load component_tags %}{% component_dependencies %}"
"{% component_block 'outer_context_component' only %}{% endcomponent_block %}")
rendered = template.render(Context({'variable': 'outer_value'})).strip()
self.assertIn('outer_value', rendered, rendered)