Skip to content

Commit d0805a8

Browse files
add test for unused components in asset templatetags (#121)
1 parent 99de651 commit d0805a8

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

tests/templatetags/test_asset.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,73 @@ def test_asset_duplication(self, create_template, templates_dir):
259259
== 1
260260
)
261261

262+
def test_unused_component_asset_not_rendered(self, create_template, templates_dir):
263+
alert = TestComponent(
264+
name="alert", content='<div class="alert">{{ slot }}</div>'
265+
).create(templates_dir)
266+
alert_css = TestAsset(
267+
component=alert, content=".alert { color: red; }", asset_type=AssetType.CSS
268+
).create()
269+
alert_js = TestAsset(
270+
component=alert, content="console.log('alert');", asset_type=AssetType.JS
271+
).create()
272+
273+
button = TestComponent(
274+
name="button", content="<button>{{ slot }}</button>"
275+
).create(templates_dir)
276+
button_css = TestAsset(
277+
component=button,
278+
content=".button { color: blue; }",
279+
asset_type=AssetType.CSS,
280+
).create()
281+
button_js = TestAsset(
282+
component=button, content="console.log('button');", asset_type=AssetType.JS
283+
).create()
284+
285+
base_path = templates_dir / "base.html"
286+
base_path.write_text("""
287+
<html>
288+
<head>
289+
<title>Test</title>
290+
{% bird:css %}
291+
</head>
292+
<body>
293+
{% bird alert %}Base Alert{% endbird %}
294+
{% block content %}{% endblock %}
295+
{% bird:js %}
296+
</body>
297+
</html>
298+
""")
299+
300+
child_path = templates_dir / "child.html"
301+
child_path.write_text("""
302+
{% extends 'base.html' %}
303+
{% block content %}
304+
{% bird alert %}Base Alert{% endbird %}
305+
{% endblock %}
306+
""")
307+
308+
template = create_template(child_path)
309+
310+
rendered = template.render({})
311+
312+
assert (
313+
f'<link rel="stylesheet" href="/__bird__/assets/{alert.name}/{alert_css.file.name}">'
314+
in rendered
315+
)
316+
assert (
317+
f'<script src="/__bird__/assets/{alert.name}/{alert_js.file.name}"></script>'
318+
in rendered
319+
)
320+
assert (
321+
f'<link rel="stylesheet" href="/__bird__/assets/{button.name}/{button_css.file.name}">'
322+
not in rendered
323+
)
324+
assert (
325+
f'<script src="/__bird__/assets/{button.name}/{button_js.file.name}"></script>'
326+
not in rendered
327+
)
328+
262329

263330
class TestNode:
264331
def test_no_template(self):

0 commit comments

Comments
 (0)