From 255f145acad8b227eeafed1e88e44f10722603c8 Mon Sep 17 00:00:00 2001 From: Kodjo Sossouvi Date: Sat, 8 Nov 2025 23:10:27 +0100 Subject: [PATCH] Other unit test fixes --- src/myfasthtml/examples/binding_datalist.py | 49 +++++++++ tests/testclient/test_testable_button.py | 104 -------------------- tests/testclient/test_testable_form.py | 3 +- 3 files changed, 50 insertions(+), 106 deletions(-) create mode 100644 src/myfasthtml/examples/binding_datalist.py delete mode 100644 tests/testclient/test_testable_button.py diff --git a/src/myfasthtml/examples/binding_datalist.py b/src/myfasthtml/examples/binding_datalist.py new file mode 100644 index 0000000..e930ff0 --- /dev/null +++ b/src/myfasthtml/examples/binding_datalist.py @@ -0,0 +1,49 @@ +import logging +from dataclasses import dataclass +from typing import Any + +from fasthtml import serve +from fasthtml.components import * + +from myfasthtml.controls.helpers import mk +from myfasthtml.core.bindings import Binding +from myfasthtml.core.utils import debug_routes +from myfasthtml.myfastapp import create_app + +logging.basicConfig( + level=logging.DEBUG, # Set logging level to DEBUG + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', # Log format + datefmt='%Y-%m-%d %H:%M:%S', # Timestamp format +) + +app, rt = create_app(protect_routes=False) + + +@dataclass +class Data: + value: Any = "Hello World" + + +data = Data() + + +@rt("/") +def get(): + datalist = Datalist( + Option(value="suggestion1"), + Option(value="suggestion2"), + Option(value="suggestion3"), + id="suggestions" + ) + input_elt = Input(name="input_name", list="suggestions") + label_elt = Label() + + mk.manage_binding(input_elt, Binding(data)) + mk.manage_binding(label_elt, Binding(data)) + + return input_elt, datalist, label_elt + + +if __name__ == "__main__": + debug_routes(app) + serve(port=5002) diff --git a/tests/testclient/test_testable_button.py b/tests/testclient/test_testable_button.py deleted file mode 100644 index 7f80456..0000000 --- a/tests/testclient/test_testable_button.py +++ /dev/null @@ -1,104 +0,0 @@ -""" -Comprehensive binding tests for all bindable FastHTML components. - -This test suite covers: -- Input (text) - already tested -- Checkbox - already tested -- Textarea -- Select (single) -- Select (multiple) -- Range (slider) -- Radio buttons -- Button -- Input with Datalist (combobox) -""" - -from dataclasses import dataclass - -from fasthtml.components import ( - Label, Button -) - -from myfasthtml.controls.helpers import mk -from myfasthtml.core.bindings import Binding - - -@dataclass -class Data: - value: str = "hello world" - - -@dataclass -class NumericData: - value: int = 50 - - -@dataclass -class BoolData: - value: bool = True - - -@dataclass -class ListData: - value: list = None - - def __post_init__(self): - if self.value is None: - self.value = [] - - -class TestBindingButton: - """Tests for binding Button components.""" - - def test_i_can_click_button_with_binding(self, user, rt): - """ - Clicking a button with HTMX should trigger binding updates. - """ - - @rt("/") - def index(): - data = Data("initial") - button_elt = Button("Click me", hx_post="/update", hx_vals='{"action": "clicked"}') - label_elt = Label() - - mk.manage_binding(button_elt, Binding(data)) - mk.manage_binding(label_elt, Binding(data)) - - return button_elt, label_elt - - @rt("/update") - def update(action: str): - data = Data("button clicked") - label_elt = Label() - mk.manage_binding(label_elt, Binding(data)) - return label_elt - - user.open("/") - user.should_see("initial") - - testable_button = user.find_element("button") - testable_button.click() - user.should_see("button clicked") - - def test_button_without_htmx_does_nothing(self, user, rt): - """ - Button without HTMX should not trigger updates. - """ - - @rt("/") - def index(): - data = Data("initial") - button_elt = Button("Plain button") # No HTMX - label_elt = Label() - - mk.manage_binding(button_elt, Binding(data)) - mk.manage_binding(label_elt, Binding(data)) - - return button_elt, label_elt - - user.open("/") - user.should_see("initial") - - testable_button = user.find_element("button") - result = testable_button.click() - assert result is None # No HTMX, no response diff --git a/tests/testclient/test_testable_form.py b/tests/testclient/test_testable_form.py index cfe4f7b..284211e 100644 --- a/tests/testclient/test_testable_form.py +++ b/tests/testclient/test_testable_form.py @@ -268,8 +268,7 @@ class TestableFormUpdateFieldValues: ''' form = TestableForm(mock_client, html) - assert "size" not in form.fields, \ - f"Expected 'size' not in fields, got {form.fields}" + assert form.fields == {"size": None}, f"Expected 'size' not in fields, got {form.fields}" def test_i_can_handle_number_input_with_integer(self, mock_client): """