import pytest from fasthtml.components import Div from fasthtml.fastapp import fast_app from myfasthtml.core.testclient import MyTestClient, TestableElement, MyFT def test_i_can_create_testable_element_from_ft(): ft = Div("hello world", id="test") testable_element = TestableElement(None, ft) assert testable_element.ft == ft assert testable_element.html_fragment == '
hello world
' def test_i_can_create_testable_element_from_str(): ft = '
hello world
' testable_element = TestableElement(None, ft) assert testable_element.ft == MyFT('div', {'id': 'test'}) assert testable_element.html_fragment == '
hello world
' def test_i_can_create_testable_element_from_beautifulsoup_element(): ft = '
hello world
' from bs4 import BeautifulSoup tag = BeautifulSoup(ft, 'html.parser').div testable_element = TestableElement(None, tag) assert testable_element.ft == MyFT('div', {'id': 'test'}) assert testable_element.html_fragment == '
hello world
' def test_i_cannot_create_testable_element_from_other_type(): with pytest.raises(ValueError) as exc_info: TestableElement(None, 123) assert str(exc_info.value) == "Invalid source '123' for TestableElement." def test_i_can_click(): test_app, rt = fast_app(default_hdrs=False) client = MyTestClient(test_app) ft = Div( hx_post="/search", hx_target="#results", hx_swap="innerHTML", hx_vals='{"attr": "attr_value"}' ) testable_element = TestableElement(client, ft) @rt('/search') def post(hx_target: str, hx_swap: str, attr: str): # hx_post is used to the Verb. It's not a parameter return f"received {hx_target=}, {hx_swap=}, {attr=}" testable_element.click() assert client.get_content() == "received hx_target='#results', hx_swap='innerHTML', attr='attr_value'" def test_i_cannot_test_when_not_clickable(): test_app, rt = fast_app(default_hdrs=False) client = MyTestClient(test_app) ft = Div("hello world") testable_element = TestableElement(client, ft) with pytest.raises(ValueError) as exc_info: testable_element.click() assert str(exc_info.value) == "The
element has no HTMX verb attribute (e.g., hx_get, hx_post) to define a URL."