Skip to content

Commit b045f1a

Browse files
committed
Raise explicit error if the component name cannot be resolved from the context.
1 parent c52972a commit b045f1a

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

django_unicorn/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ class MissingComponentElement(Exception):
2020

2121
class MissingComponentViewElement(Exception):
2222
pass
23+
24+
25+
class ComponentNotValid(Exception):
26+
pass

django_unicorn/templatetags/unicorn.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import shortuuid
88

99
from django_unicorn.call_method_parser import InvalidKwarg, parse_kwarg
10+
from django_unicorn.errors import ComponentNotValid
1011

1112

1213
register = template.Library()
@@ -99,7 +100,13 @@ def render(self, context):
99100
self.parent = resolved_kwargs.pop("parent")
100101

101102
component_id = None
102-
component_name = self.component_name.resolve(context)
103+
104+
try:
105+
component_name = self.component_name.resolve(context)
106+
except AttributeError:
107+
raise ComponentNotValid(
108+
f"Component template is not valid: {self.component_name}."
109+
)
103110

104111
if self.parent:
105112
# Child components use the parent for part of the `component_id`

tests/templatetags/test_unicorn_render.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import re
22

33
from django.template import Context
4-
from django.template.base import Token, TokenType
4+
from django.template.base import Parser, Token, TokenType
55

66
import pytest
77

88
from django_unicorn.components import UnicornView
9+
from django_unicorn.errors import ComponentNotValid
910
from django_unicorn.templatetags.unicorn import unicorn
1011
from django_unicorn.utils import generate_checksum
1112
from example.coffee.models import Flavor
@@ -344,7 +345,24 @@ def test_unicorn_render_with_component_name_from_context():
344345
)
345346
unicorn_node = unicorn(Parser([]), token)
346347
context = {"component_name": "tests.templatetags.test_unicorn_render.FakeComponent"}
347-
unicorn_node.render(Context(context))
348+
html = unicorn_node.render(Context(context))
349+
350+
assert '<script type="module"' in html
351+
assert len(re.findall('<script type="module"', html)) == 1
352+
348353

349-
# Success if no exception was raised so far
350-
assert True
354+
def test_unicorn_render_with_invalid_component_name_from_context():
355+
token = Token(
356+
TokenType.TEXT,
357+
"unicorn bad_component_name",
358+
)
359+
unicorn_node = unicorn(Parser([]), token)
360+
context = {"component_name": "tests.templatetags.test_unicorn_render.FakeComponent"}
361+
362+
with pytest.raises(ComponentNotValid) as e:
363+
unicorn_node.render(Context(context))
364+
365+
assert (
366+
e.exconly()
367+
== "django_unicorn.errors.ComponentNotValid: Component template is not valid: bad_component_name."
368+
)

0 commit comments

Comments
 (0)