105 lines
2.7 KiB
Python
105 lines
2.7 KiB
Python
"""
|
|
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
|
|
|
|
import pytest
|
|
from fasthtml.fastapp import fast_app
|
|
|
|
from myfasthtml.test.testclient import MyTestClient, TestableRadio
|
|
|
|
|
|
@dataclass
|
|
class Data:
|
|
value: str = "hello world"
|
|
|
|
|
|
@pytest.fixture
|
|
def test_app():
|
|
test_app, rt = fast_app(default_hdrs=False)
|
|
return test_app
|
|
|
|
|
|
@pytest.fixture
|
|
def rt(test_app):
|
|
return test_app.route
|
|
|
|
|
|
@pytest.fixture
|
|
def test_client(test_app):
|
|
return MyTestClient(test_app)
|
|
|
|
|
|
def test_i_can_read_not_selected_radio(test_client):
|
|
html = '''<input type="radio" name="radio_name" value="option1" />'''
|
|
|
|
input_elt = TestableRadio(test_client, html)
|
|
|
|
assert input_elt.name == "radio_name"
|
|
assert input_elt.value is None
|
|
|
|
|
|
def test_i_can_read_selected_radio(test_client):
|
|
html = '''<input type="radio" name="radio_name" value="option1" checked="true"/>'''
|
|
|
|
input_elt = TestableRadio(test_client, html)
|
|
|
|
assert input_elt.name == "radio_name"
|
|
assert input_elt.value == "option1"
|
|
|
|
|
|
def test_i_cannot_read_radio_with_multiple_values(test_client):
|
|
html = '''
|
|
<input type="radio" name="radio_name" value="option1" checked="true" />
|
|
<input type="radio" name="radio_name" value="option2" />
|
|
'''
|
|
|
|
with pytest.raises(AssertionError) as exc_info:
|
|
TestableRadio(test_client, html)
|
|
|
|
assert "Only one radio button per name is supported" in str(exc_info.value)
|
|
|
|
|
|
def test_i_cannot_read_radio_when_no_radio_button(test_client):
|
|
html = '''
|
|
<input type="text" name="radio_name" value="option1" checked="true" /> '''
|
|
|
|
with pytest.raises(AssertionError) as exc_info:
|
|
TestableRadio(test_client, html)
|
|
|
|
assert "No radio buttons found" in str(exc_info.value)
|
|
|
|
|
|
def test_i_can_read_input_with_label(test_client):
|
|
html = '''<label for="uid">John Doe</label><input id="uid" type="radio" name="username" value="john_doe" />'''
|
|
|
|
input_elt = TestableRadio(test_client, html)
|
|
assert input_elt.fields_mapping == {"John Doe": "username"}
|
|
assert input_elt.name == "username"
|
|
assert input_elt.value is None
|
|
|
|
|
|
def test_i_can_send_values(test_client, rt):
|
|
html = '''<input type="text" name="username" type="radio" value="john_doe" hx_post="/submit"/>'''
|
|
|
|
@rt('/submit')
|
|
def post(username: str):
|
|
return f"Input received {username=}"
|
|
|
|
input_elt = TestableRadio(test_client, html)
|
|
input_elt.select()
|
|
|
|
assert test_client.get_content() == "Input received username='john_doe'"
|