feat: render fragments without document strategy (#1339)

This commit is contained in:
Juro Oravec 2025-08-14 11:33:31 +02:00 committed by GitHub
parent aa14e3698d
commit c72fed8255
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 217 additions and 26 deletions

View file

@ -366,7 +366,24 @@ class TestRenderDependencies:
rendered_raw = Template(template_str).render(Context({"formset": [1], "DJC_DEPS_STRATEGY": "ignore"}))
rendered = render_dependencies(rendered_raw, strategy="fragment")
assertHTMLEqual(rendered, "<thead>")
# NOTE: Fragments adds a script to optionally load the component manager script.
assertHTMLEqual(
rendered,
"""
<thead>
<script>
(() => {
if (!globalThis.Components) {
const s = document.createElement('script');
s.src = "django_components/django_components.min.js";
document.head.appendChild(s);
}
// Remove this loader script
if (document.currentScript) document.currentScript.remove();
})();
</script>
""",
)
def test_does_not_modify_html_when_no_component_used(self):
registry.register(name="test", component=SimpleComponent)
@ -401,6 +418,7 @@ class TestRenderDependencies:
rendered_raw = Template(template_str).render(Context({"formset": [1], "DJC_DEPS_STRATEGY": "ignore"}))
rendered = render_dependencies(rendered_raw, strategy="fragment")
# NOTE: Fragments adds a script to optionally load the component manager script.
expected = """
<table class="table-auto border-collapse divide-y divide-x divide-slate-300 w-full">
<!-- Table head -->
@ -422,6 +440,17 @@ class TestRenderDependencies:
</tr>
</tbody>
</table>
<script>
(() => {
if (!globalThis.Components) {
const s = document.createElement('script');
s.src = "django_components/django_components.min.js";
document.head.appendChild(s);
}
// Remove this loader script
if (document.currentScript) document.currentScript.remove();
})();
</script>
"""
assertHTMLEqual(expected, rendered)
@ -469,6 +498,8 @@ class TestRenderDependencies:
# `PGxpbmsgaHJlZj0iL2NvbXBvbmVudHMvY2FjaGUvU2ltcGxlQ29tcG9uZW50XzMxMTA5Ny5jc3MiIG1lZGlhPSJhbGwiIHJlbD0ic3R5bGVzaGVldCI+` -> `<link href="/components/cache/SimpleComponent_311097.css" media="all" rel="stylesheet">` # noqa: E501
# `PHNjcmlwdCBzcmM9InNjcmlwdC5qcyI+PC9zY3JpcHQ+` -> `<script src="script.js"></script>`
# `PHNjcmlwdCBzcmM9Ii9jb21wb25lbnRzL2NhY2hlL1NpbXBsZUNvbXBvbmVudF8zMTEwOTcuanMiPjwvc2NyaXB0Pg==` -> `<script src="/components/cache/SimpleComponent_311097.js"></script>` # noqa: E501
#
# NOTE: Fragments adds a script to optionally load the component manager script.
expected = """
<table class="table-auto border-collapse divide-y divide-x divide-slate-300 w-full">
<!-- Table head -->
@ -491,6 +522,17 @@ class TestRenderDependencies:
</tr>
</tbody>
</table>
<script>
(() => {
if (!globalThis.Components) {
const s = document.createElement('script');
s.src = "django_components/django_components.min.js";
document.head.appendChild(s);
}
// Remove this loader script
if (document.currentScript) document.currentScript.remove();
})();
</script>
<script type="application/json" data-djc>
{"loadedCssUrls": [],
"loadedJsUrls": [],