| 1 |
""" |
| 2 |
JinjaX |
| 3 |
Copyright (c) Juan-Pablo Scaletti <[email protected]> |
| 4 |
""" |
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
import jinja2 |
| 8 |
import pytest |
| 9 |
from markupsafe import Markup |
| 10 |
|
| 11 |
|
| 12 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 13 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 14 |
def test_render_assets(catalog, folder, autoescape, undefined): |
| 15 |
catalog.jinja_env.autoescape = autoescape |
| 16 |
catalog.jinja_env.undefined = undefined |
| 17 |
|
| 18 |
(folder / "Greeting.jinja").write_text( |
| 19 |
""" |
| 20 |
{#def message #} |
| 21 |
{#css greeting.css, http://example.com/super.css #} |
| 22 |
{#js greeting.js #} |
| 23 |
<div class="greeting [&_a]:flex">{{ message }}</div> |
| 24 |
""" |
| 25 |
) |
| 26 |
|
| 27 |
(folder / "Card.jinja").write_text( |
| 28 |
""" |
| 29 |
{#css https://somewhere.com/style.css, card.css #} |
| 30 |
{#js card.js, shared.js #} |
| 31 |
<section class="card"> |
| 32 |
{{ content }} |
| 33 |
</section> |
| 34 |
""" |
| 35 |
) |
| 36 |
|
| 37 |
(folder / "Layout.jinja").write_text( |
| 38 |
""" |
| 39 |
<html> |
| 40 |
{{ catalog.render_assets() }} |
| 41 |
{{ content }} |
| 42 |
</html> |
| 43 |
""" |
| 44 |
) |
| 45 |
|
| 46 |
(folder / "Page.jinja").write_text( |
| 47 |
""" |
| 48 |
{#def message #} |
| 49 |
{#js https://somewhere.com/blabla.js, shared.js #} |
| 50 |
<Layout> |
| 51 |
<Card> |
| 52 |
<Greeting :message="message" /> |
| 53 |
<button type="button">Close</button> |
| 54 |
</Card> |
| 55 |
</Layout> |
| 56 |
""" |
| 57 |
) |
| 58 |
|
| 59 |
html = catalog.render("Page", message="Hello") |
| 60 |
print(html) |
| 61 |
assert ( |
| 62 |
""" |
| 63 |
<html> |
| 64 |
<link rel="stylesheet" href="https://somewhere.com/style.css"> |
| 65 |
<link rel="stylesheet" href="/static/components/card.css"> |
| 66 |
<link rel="stylesheet" href="/static/components/greeting.css"> |
| 67 |
<link rel="stylesheet" href="http://example.com/super.css"> |
| 68 |
<script type="module" src="https://somewhere.com/blabla.js"></script> |
| 69 |
<script type="module" src="/static/components/shared.js"></script> |
| 70 |
<script type="module" src="/static/components/card.js"></script> |
| 71 |
<script type="module" src="/static/components/greeting.js"></script> |
| 72 |
<section class="card"> |
| 73 |
<div class="greeting [&_a]:flex">Hello</div> |
| 74 |
<button type="button">Close</button> |
| 75 |
</section> |
| 76 |
</html> |
| 77 |
""".strip() |
| 78 |
in html |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 83 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 84 |
def test_cleanup_assets(catalog, folder, autoescape, undefined): |
| 85 |
catalog.jinja_env.autoescape = autoescape |
| 86 |
catalog.jinja_env.undefined = undefined |
| 87 |
|
| 88 |
(folder / "Layout.jinja").write_text(""" |
| 89 |
<html> |
| 90 |
{{ catalog.render_assets() }} |
| 91 |
{{ content }} |
| 92 |
</html> |
| 93 |
""") |
| 94 |
|
| 95 |
(folder / "Foo.jinja").write_text(""" |
| 96 |
{#js foo.js #} |
| 97 |
<Layout> |
| 98 |
<p>Foo</p> |
| 99 |
</Layout> |
| 100 |
""") |
| 101 |
|
| 102 |
(folder / "Bar.jinja").write_text(""" |
| 103 |
{#js bar.js #} |
| 104 |
<Layout> |
| 105 |
<p>Bar</p> |
| 106 |
</Layout> |
| 107 |
""") |
| 108 |
|
| 109 |
html = catalog.render("Foo") |
| 110 |
print(html, "\n") |
| 111 |
assert ( |
| 112 |
""" |
| 113 |
<html> |
| 114 |
<script type="module" src="/static/components/foo.js"></script> |
| 115 |
<p>Foo</p> |
| 116 |
</html> |
| 117 |
""".strip() |
| 118 |
in html |
| 119 |
) |
| 120 |
|
| 121 |
html = catalog.render("Bar") |
| 122 |
print(html) |
| 123 |
assert ( |
| 124 |
""" |
| 125 |
<html> |
| 126 |
<script type="module" src="/static/components/bar.js"></script> |
| 127 |
<p>Bar</p> |
| 128 |
</html> |
| 129 |
""".strip() |
| 130 |
in html |
| 131 |
) |
| 132 |
|
| 133 |
|
| 134 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 135 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 136 |
def test_fingerprint_assets(catalog, folder: Path, autoescape, undefined): |
| 137 |
catalog.jinja_env.autoescape = autoescape |
| 138 |
catalog.jinja_env.undefined = undefined |
| 139 |
|
| 140 |
subfolder = folder / "sub" |
| 141 |
subfolder.mkdir() |
| 142 |
prefixed = folder/ "ui" |
| 143 |
prefixed.mkdir() |
| 144 |
|
| 145 |
catalog.add_folder(prefixed, prefix="ui") |
| 146 |
|
| 147 |
(folder / "app.css").write_text("...") |
| 148 |
(subfolder / "sub.css").write_text("...") |
| 149 |
(prefixed / "button.css").write_text("...") |
| 150 |
|
| 151 |
(prefixed / "Button.jinja").write_text(""" |
| 152 |
{#css button.css #} |
| 153 |
<button class="btn">{{ content }}</button> |
| 154 |
""") |
| 155 |
|
| 156 |
(folder / "Layout.jinja").write_text(""" |
| 157 |
<html> |
| 158 |
{{ catalog.render_assets() }} |
| 159 |
{{ content }} |
| 160 |
</html> |
| 161 |
""") |
| 162 |
|
| 163 |
(folder / "Page.jinja").write_text(""" |
| 164 |
{#css app.css, sub/sub.css, http://example.com/external.css #} |
| 165 |
{#js app.js #} |
| 166 |
<Layout><ui:Button>Hi</ui:Button></Layout> |
| 167 |
""") |
| 168 |
|
| 169 |
catalog.fingerprint = True |
| 170 |
html = catalog.render("Page", message="Hello") |
| 171 |
print(html) |
| 172 |
|
| 173 |
# "app.js" doesn't exists, so it cannot be fingerprinted |
| 174 |
assert 'src="/static/components/app.js"' in html |
| 175 |
|
| 176 |
# external URLs are not fingerprinted |
| 177 |
assert 'href="http://example.com/external.css' in html |
| 178 |
|
| 179 |
# fingerprinted assets |
| 180 |
assert 'href="/static/components/sub/sub-' in html |
| 181 |
assert 'href="/static/components/app-' in html |
| 182 |
assert 'href="/static/components/ui/button-' in html |
| 183 |
|
| 184 |
|
| 185 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 186 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 187 |
def test_auto_load_assets_with_same_name(catalog, folder, autoescape, undefined): |
| 188 |
catalog.jinja_env.autoescape = autoescape |
| 189 |
catalog.jinja_env.undefined = undefined |
| 190 |
|
| 191 |
(folder / "Layout.jinja").write_text( |
| 192 |
"""{{ catalog.render_assets() }}\n{{ content }}""" |
| 193 |
) |
| 194 |
|
| 195 |
(folder / "FooBar.css").touch() |
| 196 |
|
| 197 |
(folder / "common").mkdir() |
| 198 |
(folder / "common" / "Form.jinja").write_text( |
| 199 |
""" |
| 200 |
{#js "shared.js" #} |
| 201 |
<form></form>""" |
| 202 |
) |
| 203 |
|
| 204 |
(folder / "common" / "Form.css").touch() |
| 205 |
(folder / "common" / "Form.js").touch() |
| 206 |
|
| 207 |
(folder / "Page.jinja").write_text( |
| 208 |
""" |
| 209 |
{#css "Page.css" #} |
| 210 |
<Layout><common.Form></common.Form></Layout>""" |
| 211 |
) |
| 212 |
|
| 213 |
(folder / "Page.css").touch() |
| 214 |
(folder / "Page.js").touch() |
| 215 |
|
| 216 |
html = catalog.render("Page") |
| 217 |
print(html) |
| 218 |
|
| 219 |
expected = """ |
| 220 |
<link rel="stylesheet" href="/static/components/Page.css"> |
| 221 |
<link rel="stylesheet" href="/static/components/common/Form.css"> |
| 222 |
<script type="module" src="/static/components/Page.js"></script> |
| 223 |
<script type="module" src="/static/components/shared.js"></script> |
| 224 |
<script type="module" src="/static/components/common/Form.js"></script> |
| 225 |
<form></form> |
| 226 |
""".strip() |
| 227 |
|
| 228 |
assert html == Markup(expected) |
| 229 |
|
| 230 |
|
| 231 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 232 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 233 |
def test_auto_load_assets_for_kebab_cased_names(catalog, folder, autoescape, undefined): |
| 234 |
catalog.jinja_env.autoescape = autoescape |
| 235 |
catalog.jinja_env.undefined = undefined |
| 236 |
|
| 237 |
(folder / "Layout.jinja").write_text( |
| 238 |
"""{{ catalog.render_assets() }}\n{{ content }}""" |
| 239 |
) |
| 240 |
|
| 241 |
(folder / "my-component.jinja").write_text("") |
| 242 |
(folder / "my-component.css").touch() |
| 243 |
(folder / "my-component.js").touch() |
| 244 |
(folder / "page.jinja").write_text("<Layout><MyComponent /></Layout>") |
| 245 |
|
| 246 |
html = catalog.render("Page") |
| 247 |
print(html) |
| 248 |
|
| 249 |
assert "/static/components/my-component.css" in html |
| 250 |
assert "/static/components/my-component.js" in html |
| 251 |
|
| 252 |
|
| 253 |
@pytest.mark.parametrize("undefined", [jinja2.Undefined, jinja2.StrictUndefined]) |
| 254 |
@pytest.mark.parametrize("autoescape", [True, False]) |
| 255 |
def test_render_assets_before_component_usage_injects_assets(catalog, folder, autoescape, undefined): |
| 256 |
""" |
| 257 |
Ensure that calling render_assets() before any component usage still |
| 258 |
collects and renders the component assets, irrespective of ordering |
| 259 |
in the template. |
| 260 |
""" |
| 261 |
catalog.jinja_env.autoescape = autoescape |
| 262 |
catalog.jinja_env.undefined = undefined |
| 263 |
|
| 264 |
# Define a simple component with associated JS asset |
| 265 |
(folder / "TestComponent.jinja").write_text("<div>Test</div>") |
| 266 |
(folder / "TestComponent.js").touch() |
| 267 |
|
| 268 |
# Template invokes render_assets() before using the component |
| 269 |
(folder / "IssueExample.jinja").write_text( |
| 270 |
""" |
| 271 |
<!DOCTYPE html> |
| 272 |
<html> |
| 273 |
<head> |
| 274 |
{{ catalog.render_assets() }} |
| 275 |
</head> |
| 276 |
<body> |
| 277 |
<TestComponent /> |
| 278 |
</body> |
| 279 |
</html> |
| 280 |
""" |
| 281 |
) |
| 282 |
|
| 283 |
html = catalog.render("IssueExample") |
| 284 |
# Assets are injected even though render_assets() appears before the component |
| 285 |
assert "/static/components/TestComponent.js" in html |