|
1 | 1 | import re |
2 | 2 |
|
3 | 3 | from django.template import Context |
4 | | -from django.template.base import Token, TokenType |
| 4 | +from django.template.base import Parser, Token, TokenType |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
8 | 8 | from django_unicorn.components import UnicornView |
| 9 | +from django_unicorn.errors import ComponentNotValid |
9 | 10 | from django_unicorn.templatetags.unicorn import unicorn |
10 | 11 | from django_unicorn.utils import generate_checksum |
11 | 12 | from example.coffee.models import Flavor |
@@ -344,7 +345,24 @@ def test_unicorn_render_with_component_name_from_context(): |
344 | 345 | ) |
345 | 346 | unicorn_node = unicorn(Parser([]), token) |
346 | 347 | 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 | + |
348 | 353 |
|
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