Skip to content

Commit

Permalink
Add tests for CSS and JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Dec 17, 2024
1 parent f514dc8 commit b61f1ee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion js_asset/js.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def __str__(self):
@html_safe
@dataclass(eq=True)
class JSON:
id: str | None
data: dict[str, Any]
id: str | None = ""

def __hash__(self):
return hash(self.__str__())
Expand Down
39 changes: 28 additions & 11 deletions tests/testapp/test_js_asset.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import django
from django.forms import Media
from django.test import TestCase

from js_asset.js import JS


CSS_TYPE = ' type="text/css"' if django.VERSION < (4, 1) else ""
JS_TYPE = ' type="text/javascript"' if django.VERSION < (3, 1) else ""
from js_asset.js import CSS, JS, JSON


class AssetTest(TestCase):
Expand All @@ -24,19 +19,19 @@ def test_asset(self):
# print(html)

self.assertInHTML(
f'<link href="/static/app/print.css"{CSS_TYPE} media="print" rel="stylesheet" />',
'<link href="/static/app/print.css" media="print" rel="stylesheet" />',
html,
)
self.assertInHTML(
f'<script{JS_TYPE} src="/static/app/test.js"></script>',
'<script src="/static/app/test.js"></script>',
html,
)
self.assertInHTML(
f'<script{JS_TYPE} src="/static/app/asset.js" data-the-answer="42" id="asset-script"></script>',
'<script src="/static/app/asset.js" data-the-answer="42" id="asset-script"></script>',
html,
)
self.assertInHTML(
f'<script{JS_TYPE} src="/static/app/asset-without.js"></script>',
'<script src="/static/app/asset-without.js"></script>',
html,
)

Expand All @@ -45,7 +40,7 @@ def test_absolute(self):
html = str(media)

self.assertInHTML(
f'<script{JS_TYPE} src="https://cdn.example.org/script.js"></script>',
'<script src="https://cdn.example.org/script.js"></script>',
html,
)

Expand All @@ -71,3 +66,25 @@ def test_boolean_attributes(self):
str(JS("app/asset.js", {"bool": True, "cool": False})),
'<script src="/static/app/asset.js" bool></script>',
)

def test_css(self):
self.assertEqual(
str(CSS("app/style.css")),
'<link rel="stylesheet" href="/static/app/style.css">',
)

self.assertEqual(
str(CSS("p{color:red}", inline=True)),
"<style>p{color:red}</style>",
)

def test_json(self):
self.assertEqual(
str(JSON({"hello": "world"}, "hello")),
'<script id="hello" type="application/json">{"hello": "world"}</script>',
)

self.assertEqual(
str(JSON({"hello": "world"})),
'<script type="application/json">{"hello": "world"}</script>',
)

0 comments on commit b61f1ee

Please sign in to comment.