I can bind checkbox (needs refactoring)

This commit is contained in:
2025-11-02 10:11:15 +01:00
parent aaba6a5468
commit c3d6958c1a
4 changed files with 217 additions and 17 deletions

View File

@@ -1,11 +1,12 @@
from dataclasses import dataclass
from typing import Any
import pytest
from fasthtml.components import Input, Label
from fasthtml.fastapp import fast_app
from myfasthtml.controls.helpers import mk
from myfasthtml.core.bindings import Binding
from myfasthtml.core.bindings import Binding, DetectionMode, UpdateMode, BooleanConverter
from myfasthtml.core.commands import Command, CommandsManager
from myfasthtml.test.testclient import MyTestClient, TestableElement
@@ -16,7 +17,7 @@ def new_value(value):
@dataclass
class Data:
value: str
value: Any
@pytest.fixture()
@@ -62,7 +63,7 @@ class TestingCommand:
class TestingBindings:
def test_i_can_bind_elements(self, user, rt):
def test_i_can_bind_input(self, user, rt):
@rt("/")
def index():
data = Data("hello world")
@@ -76,4 +77,27 @@ class TestingBindings:
user.should_see("")
testable_input = user.find_element("input")
testable_input.send("new value")
user.should_see("new value") # the one from the label
user.should_see("new value") # the one from the label
def test_i_can_bind_checkbox(self, user, rt):
@rt("/")
def index():
data = Data(True)
input_elt = Input(name="input_name", type="checkbox")
label_elt = Label()
mk.manage_binding(input_elt, Binding(data, ft_attr="checked",
detection_mode=DetectionMode.AttributePresence,
update_mode=UpdateMode.AttributePresence,
data_converter=BooleanConverter()))
mk.manage_binding(label_elt, Binding(data))
return input_elt, label_elt
user.open("/")
user.should_see("")
testable_input = user.find_element("input")
testable_input.check()
user.should_see("True")
testable_input.uncheck()
user.should_see("False")