Lines
94.12 %
Functions
100 %
Branches
use forms::{Action, Field, Form, FormValues, Kind};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use sqlx::types::Uuid;
use crate::form::{Form as TuiForm, FormKind};
use crate::modal::Modal;
use crate::widgets::Widget;
const FIELDS: &[Field] = &[Field {
key: "period",
label: "Period",
kind: Kind::Choice {
options: &[("month", "Month"), ("year", "Year")],
default: Some("month"),
},
required: false,
}];
const FORM: Form = Form {
name: "choice-test",
title: "Choice test",
fields: FIELDS,
action: Action::Query,
};
fn choice_form() -> TuiForm {
TuiForm::from_descriptor(&FORM, FormKind::TransactionCreate)
}
#[test]
fn a_choice_field_is_a_select_showing_the_default_label() {
let form = choice_form();
assert!(matches!(form.fields[0].widget, Widget::Select(_)));
let Widget::Select(select) = &form.fields[0].widget else {
return;
assert_eq!(select.display(), "Month");
assert_eq!(form.values().text("period"), Some("month"));
fn picking_an_option_submits_its_key() {
let mut form = choice_form();
let Widget::Select(select) = &mut form.fields[0].widget else {
select.next();
assert_eq!(select.display(), "Year");
assert_eq!(form.values().text("period"), Some("year"));
fn loading_a_key_selects_it_and_an_empty_value_keeps_the_default() {
let mut loaded = FormValues::default();
loaded.set("period", "year");
form.load(&loaded);
let mut empty = FormValues::default();
empty.set("period", "");
form.load(&empty);
fn the_focused_choice_renders_its_labels() {
let mut app = crate::test_support::app_for(Uuid::nil());
if let Widget::Select(select) = &mut form.fields[0].widget {
select.open = true;
app.overlays.push(Modal::Form(form));
let mut terminal = Terminal::new(TestBackend::new(100, 30)).expect("test terminal");
terminal
.draw(|frame| crate::draw::draw(frame, &app, &mut crate::hits::Hits::default()))
.expect("draw must not panic");
let screen: String = terminal
.backend()
.buffer()
.content()
.iter()
.map(|cell| cell.symbol())
.collect();
assert!(
screen.contains("Month") && screen.contains("Year"),
"{screen}"
);