File: test_js_asset.py

package info (click to toggle)
python-django-js-asset 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 140 kB
  • sloc: python: 210; sh: 18; makefile: 10
file content (95 lines) | stat: -rw-r--r-- 3,011 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from django.forms import Media
from django.test import TestCase

from js_asset.js import CSS, JS, JSON


class AssetTest(TestCase):
    def test_asset(self):
        media = Media(
            css={"print": ["app/print.css"]},
            js=[
                "app/test.js",
                JS("app/asset.js", {"id": "asset-script", "data-the-answer": 42}),
                JS("app/asset-without.js", {}),
            ],
        )
        html = str(media)

        # print(html)

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

    def test_absolute(self):
        media = Media(js=[JS("https://cdn.example.org/script.js")])
        html = str(media)

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

    def test_asset_merging(self):
        media1 = Media(js=["thing.js", JS("other.js"), "some.js"])
        media2 = Media(js=["thing.js", JS("other.js"), "some.js"])
        media = media1 + media2
        self.assertEqual(len(media._js), 3)
        self.assertEqual(media._js[0], "thing.js")
        self.assertEqual(media._js[2], "some.js")

    def test_set(self):
        media = [
            JS("app/asset.js", {"id": "asset-script", "data-the-answer": 42}),
            JS("app/asset.js", {"id": "asset-script", "data-the-answer": 42}),
            JS("app/asset.js", {"id": "asset-script", "data-the-answer": 43}),
        ]

        self.assertEqual(len(set(media)), 2)

    def test_boolean_attributes(self):
        self.assertEqual(
            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 href="/static/app/style.css" media="all" rel="stylesheet">',
        )

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

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

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

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