1
use super::*;
2
use crate::form::{Field, Form, FormKind};
3
use crate::modal::Modal;
4
use crate::widgets::{SelectOption, SelectWidget, Widget};
5

            
6
#[test]
7
1
fn select_widget_intents_navigate_options() {
8
1
    let mut sw = SelectWidget::new();
9
1
    sw.set_options(vec![
10
1
        SelectOption {
11
1
            id: "id-0".to_string(),
12
1
            label: "Zero".to_string(),
13
1
        },
14
1
        SelectOption {
15
1
            id: "id-1".to_string(),
16
1
            label: "One".to_string(),
17
1
        },
18
    ]);
19
1
    let mut app = make_app();
20
1
    let form = Form {
21
1
        fields: vec![Field {
22
1
            label: "pick",
23
1
            widget: Widget::Select(sw),
24
1
        }],
25
1
        focus: 0,
26
1
        kind: FormKind::ConfigSet,
27
1
        entity_id: None,
28
1
    };
29
1
    app.overlays.push(Modal::Form(form));
30
1
    apply(&mut app, Intent::SelectNext);
31
1
    match app.overlays.top() {
32
1
        Some(Modal::Form(f)) => assert_eq!(f.fields[0].widget.value(), "id-1"),
33
        _ => panic!("expected form modal"),
34
    }
35
1
    apply(&mut app, Intent::SelectPrev);
36
1
    match app.overlays.top() {
37
1
        Some(Modal::Form(f)) => assert_eq!(f.fields[0].widget.value(), "id-0"),
38
        _ => panic!("expected form modal"),
39
    }
40
1
}
41

            
42
#[test]
43
1
fn select_insert_char_filters_and_value_is_filtered_id() {
44
1
    let mut sw = SelectWidget::new();
45
1
    sw.set_options(vec![
46
1
        SelectOption {
47
1
            id: "id-alpha".to_string(),
48
1
            label: "Alpha".to_string(),
49
1
        },
50
1
        SelectOption {
51
1
            id: "id-beta".to_string(),
52
1
            label: "Beta".to_string(),
53
1
        },
54
1
        SelectOption {
55
1
            id: "id-aleph".to_string(),
56
1
            label: "Aleph".to_string(),
57
1
        },
58
    ]);
59
1
    let mut app = make_app();
60
1
    let form = Form {
61
1
        fields: vec![Field {
62
1
            label: "from",
63
1
            widget: Widget::Select(sw),
64
1
        }],
65
1
        focus: 0,
66
1
        kind: FormKind::CommodityConvert,
67
1
        entity_id: None,
68
1
    };
69
1
    app.overlays.push(Modal::Form(form));
70
    // 'B' narrows to only "Beta"
71
1
    apply(&mut app, Intent::InsertChar('B'));
72
1
    match app.overlays.top() {
73
1
        Some(Modal::Form(f)) => assert_eq!(
74
1
            f.fields[0].widget.value(),
75
            "id-beta",
76
            "only Beta matches 'B'"
77
        ),
78
        _ => panic!("expected form modal"),
79
    }
80
    // Backspace clears filter; all options visible; focused resets to 0 = Alpha
81
1
    apply(&mut app, Intent::DeleteBackward);
82
1
    match app.overlays.top() {
83
1
        Some(Modal::Form(f)) => assert_eq!(
84
1
            f.fields[0].widget.value(),
85
            "id-alpha",
86
            "after filter pop all options visible, focused=0 is Alpha"
87
        ),
88
        _ => panic!("expected form modal"),
89
    }
90
1
}