Testing¶
django-htmx-nav provides automated test utilities to verify navigation state consistency across HTMX request pathways.
2. Verifying DOM Fragment Composition (assert_shell_composition)¶
While assert_shell_parity verifies template context parity, assert_shell_composition goes further by parsing the actual response HTML using BeautifulSoup to ensure that:
The full-reload response’s
#page-contentfragment matches the page-shell response body.The page-shell response’s
#tab-contentfragment matches the tab-shell response body.The full-reload response’s
#tab-contentfragment matches the tab-shell response body.
This catches bugs such as missing wrapper elements or header-dependent template branching that context assertions alone cannot detect.
from django.test import Client
from htmx_nav.testing import assert_shell_composition
def test_project_detail_html_composition(client: Client, project):
responses = assert_shell_composition(
client,
f"/projects/{project.pk}/",
page_shell_kwargs={"HTTP_HX_REQUEST": "true", "HTTP_HX_TARGET": "page-content"},
tab_shell_kwargs={"HTTP_HX_REQUEST": "true", "HTTP_HX_TARGET": "tab-content"},
page_container_id="page-content",
tab_container_id="tab-content",
)
# Further custom assertions on individual responses
assert responses["full_reload"].status_code == 200
Note:
assert_shell_compositionrequiresbeautifulsoup4. Install it via:pip install beautifulsoup4