Skip to content

Commit

Permalink
Fix bug and fix and improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
bazubii committed Feb 13, 2023
1 parent 5f63d4f commit f1c9299
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion django_unicorn/templatetags/unicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def render(self, context):
# if there is no explicit parent, but this node is rendering under an existing
# unicorn template, set that as the parent
try:
implicit_parent = template.Variable("unicorn.parent").resolve(context)
implicit_parent = template.Variable("unicorn.component").resolve(context)
if implicit_parent:
self.parent = implicit_parent
except template.VariableDoesNotExist:
Expand Down
4 changes: 4 additions & 0 deletions tests/templates/test_component_child_implicit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
==child==
has_parent:{{ has_parent }}
</div>
4 changes: 2 additions & 2 deletions tests/templates/test_component_parent_implicit.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

<div>
--parent--
{% unicorn 'tests.templatetags.test_unicorn_render.FakeComponentChild' %}
</div>
{% unicorn 'tests.templatetags.test_unicorn_render.FakeComponentChildImplicit' %}
</div>
3 changes: 3 additions & 0 deletions tests/templates/test_parent_implicit_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% load unicorn %}

{% unicorn 'tests.templatetags.test_unicorn_render.FakeComponentParentImplicit' %}
16 changes: 14 additions & 2 deletions tests/templatetags/test_unicorn_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ class FakeComponentParent(UnicornView):
template_name = "templates/test_component_parent.html"


class FakeComponentParentImplicit(UnicornView):
template_name = "templates/test_component_parent_implicit.html"


class FakeComponentChild(UnicornView):
template_name = "templates/test_component_child.html"


class FakeComponentChildImplicit(UnicornView):
template_name = "templates/test_component_child_implicit.html"

def has_parent(self):
return self.parent is not None


class FakeComponentKwargs(UnicornView):
template_name = "templates/test_component_kwargs.html"
hello = "world"
Expand Down Expand Up @@ -134,15 +145,16 @@ def test_unicorn_template_renders_with_implicit_parent_and_child(client):
assert response.wsgi_request.path == "/test-parent-implicit"
assert content.startswith("<div unicorn:id")
assert (
'unicorn:name="tests.templatetags.test_unicorn_render.FakeComponentParent"'
'unicorn:name="tests.templatetags.test_unicorn_render.FakeComponentParentImplicit"'
in content
)
assert (
'unicorn:name="tests.templatetags.test_unicorn_render.FakeComponentChild"'
'unicorn:name="tests.templatetags.test_unicorn_render.FakeComponentChildImplicit"'
in content
)
assert "--parent--" in content
assert "==child==" in content
assert "has_parent:True" in content
assert '<script type="application/json" id="unicorn:data:' in content


Expand Down
2 changes: 1 addition & 1 deletion tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def parent_view(request):


def parent_implicit_view(request):
return render(request, "templates/test_parent_template.html")
return render(request, "templates/test_parent_implicit_template.html")


urlpatterns = (
Expand Down

0 comments on commit f1c9299

Please sign in to comment.