1
use forms::{Action, Field, Form, FormValues, Kind};
2
use ratatui::Terminal;
3
use ratatui::backend::TestBackend;
4
use sqlx::types::Uuid;
5

            
6
use crate::form::{Form as TuiForm, FormKind};
7
use crate::modal::Modal;
8
use crate::widgets::Widget;
9

            
10
const FIELDS: &[Field] = &[Field {
11
    key: "period",
12
    label: "Period",
13
    kind: Kind::Choice {
14
        options: &[("month", "Month"), ("year", "Year")],
15
        default: Some("month"),
16
    },
17
    required: false,
18
}];
19

            
20
const FORM: Form = Form {
21
    name: "choice-test",
22
    title: "Choice test",
23
    fields: FIELDS,
24
    action: Action::Query,
25
};
26

            
27
5
fn choice_form() -> TuiForm {
28
5
    TuiForm::from_descriptor(&FORM, FormKind::TransactionCreate)
29
5
}
30

            
31
#[test]
32
1
fn a_choice_field_is_a_select_showing_the_default_label() {
33
1
    let form = choice_form();
34
1
    assert!(matches!(form.fields[0].widget, Widget::Select(_)));
35
1
    let Widget::Select(select) = &form.fields[0].widget else {
36
        return;
37
    };
38
1
    assert_eq!(select.display(), "Month");
39
1
    assert_eq!(form.values().text("period"), Some("month"));
40
1
}
41

            
42
#[test]
43
1
fn picking_an_option_submits_its_key() {
44
1
    let mut form = choice_form();
45
1
    let Widget::Select(select) = &mut form.fields[0].widget else {
46
        return;
47
    };
48
1
    select.next();
49
1
    assert_eq!(select.display(), "Year");
50
1
    assert_eq!(form.values().text("period"), Some("year"));
51
1
}
52

            
53
#[test]
54
1
fn loading_a_key_selects_it_and_an_empty_value_keeps_the_default() {
55
1
    let mut form = choice_form();
56
1
    let mut loaded = FormValues::default();
57
1
    loaded.set("period", "year");
58
1
    form.load(&loaded);
59
1
    assert_eq!(form.values().text("period"), Some("year"));
60
1
    let mut form = choice_form();
61
1
    let mut empty = FormValues::default();
62
1
    empty.set("period", "");
63
1
    form.load(&empty);
64
1
    assert_eq!(form.values().text("period"), Some("month"));
65
1
}
66

            
67
#[test]
68
1
fn the_focused_choice_renders_its_labels() {
69
1
    let mut app = crate::test_support::app_for(Uuid::nil());
70
1
    let mut form = choice_form();
71
1
    if let Widget::Select(select) = &mut form.fields[0].widget {
72
1
        select.open = true;
73
    }
74
1
    app.overlays.push(Modal::Form(form));
75
1
    let mut terminal = Terminal::new(TestBackend::new(100, 30)).expect("test terminal");
76
1
    terminal
77
1
        .draw(|frame| crate::draw::draw(frame, &app, &mut crate::hits::Hits::default()))
78
1
        .expect("draw must not panic");
79
1
    let screen: String = terminal
80
1
        .backend()
81
1
        .buffer()
82
1
        .content()
83
1
        .iter()
84
3000
        .map(|cell| cell.symbol())
85
        .collect();
86
1
    assert!(
87
1
        screen.contains("Month") && screen.contains("Year"),
88
        "{screen}"
89
    );
90
1
}