HTMX kytkee uuden sisällön hx-attribuutit (esim. dialogilomakkeen hx-post) vasta settle-vaiheessa, oletuksena 20 ms swapin jälkeen. Jos lomake lähetettiin tuona aikana, selain teki tavallisen sivulatauksen: palvelin palautti koko sivun, eikä htmx:afterSettle siirtänyt fokusta virheelliseen kenttään. Tämä aiheutti test_add_contact_with_keyboard-testin noin joka kolmannen ajon epäonnistumisen. Asetetaan htmx.config.defaultSettleDelay = 0, jolloin uusi sisältö kytketään heti swapin yhteydessä. Sovellus ei käytä htmx-settling- tai htmx-added-luokkien CSS-siirtymiä, joten viiveelle ei ole tarvetta. Co-Authored-By: Claude Opus 5.5 <[email protected]>
78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
from collections.abc import Callable
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from tests.browser.conftest import ALL, assert_accessible, no_horizontal_scroll
|
|
|
|
|
|
@pytest.fixture
|
|
def customer(admin_user: Any) -> Any:
|
|
from apps.customers.infrastructure.orm import BillingUnitModel, ContactModel, CustomerModel
|
|
|
|
c = CustomerModel.objects.create(
|
|
name="Oy Esimerkki Ab", type="company", business_id="0112038-9"
|
|
)
|
|
ContactModel.objects.create(customer=c, name="Maija Meikäläinen", email="[email protected]")
|
|
BillingUnitModel.objects.create(
|
|
customer=c,
|
|
name="Pääkonttori",
|
|
e_invoice_address="003701120389",
|
|
e_invoice_operator="003721291126",
|
|
)
|
|
return c
|
|
|
|
|
|
@pytest.mark.parametrize("themed_page", ALL, indirect=True, ids=lambda p: f"{p[0]}-{p[1]}")
|
|
def test_customer_pages(
|
|
themed_page: Any,
|
|
live_server: Any,
|
|
admin_user: Any,
|
|
customer: Any,
|
|
login: Callable[[Any, str], None],
|
|
) -> None:
|
|
login(themed_page, admin_user.email)
|
|
for path in (
|
|
"/customers/",
|
|
"/customers/new/",
|
|
f"/customers/{customer.pk}/",
|
|
f"/customers/{customer.pk}/edit/",
|
|
):
|
|
themed_page.goto(f"{live_server.url}{path}")
|
|
assert_accessible(themed_page)
|
|
assert no_horizontal_scroll(themed_page), path
|
|
|
|
# The contact dialog (loaded with HTMX) is accessible too.
|
|
themed_page.goto(f"{live_server.url}/customers/{customer.pk}/")
|
|
themed_page.get_by_role("link", name="Lisää yhteyshenkilö").click()
|
|
themed_page.locator("#item-dialog[open] #id_name").wait_for()
|
|
themed_page.wait_for_timeout(300)
|
|
assert_accessible(themed_page)
|
|
|
|
|
|
def test_add_contact_with_keyboard(
|
|
page: Any, live_server: Any, admin_user: Any, customer: Any, login: Callable[[Any, str], None]
|
|
) -> None:
|
|
login(page, admin_user.email)
|
|
page.goto(f"{live_server.url}/customers/{customer.pk}/")
|
|
page.get_by_role("link", name="Lisää yhteyshenkilö").focus()
|
|
page.keyboard.press("Enter")
|
|
name = page.locator("#item-dialog[open] #id_name")
|
|
name.wait_for()
|
|
|
|
# Submitting empty shows the error and keeps focus in the dialog on the invalid field.
|
|
page.locator("#item-dialog button[type=submit]").focus()
|
|
page.keyboard.press("Enter")
|
|
page.wait_for_selector("#id_name[aria-invalid='true']")
|
|
# Focus moves in htmx:afterSettle.
|
|
page.wait_for_function("document.activeElement && document.activeElement.id === 'id_name'")
|
|
|
|
page.keyboard.type("Pekka Puhelin")
|
|
page.keyboard.press("Enter")
|
|
page.wait_for_function(
|
|
"document.getElementById('live-region').textContent.includes('tallennettu')"
|
|
)
|
|
assert page.evaluate("document.activeElement.id") == "contacts-heading"
|
|
assert not page.evaluate("document.getElementById('item-dialog').open")
|
|
assert "Pekka Puhelin" in page.locator("#contacts-section").inner_text()
|