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
1
    ]);
19
1
    let mut app = crate::test_support::app();
20
1
    let form = Form {
21
1
        fields: vec![Field {
22
1
            key: None,
23
1
            label: "pick",
24
1
            widget: Widget::Select(sw),
25
1
        }],
26
1
        focus: 0,
27
1
        kind: FormKind::ConfigSet,
28
1
        ..Form::config_set()
29
1
    };
30
1
    app.open_dialog_on_front(Modal::Form(form));
31
1
    apply(&mut app, Intent::SelectNext);
32
1
    match app.overlays.top() {
33
1
        Some(Modal::Form(f)) => assert_eq!(f.fields[0].widget.value(), "id-1"),
34
        _ => panic!("expected form modal"),
35
    }
36
1
    apply(&mut app, Intent::SelectPrev);
37
1
    match app.overlays.top() {
38
1
        Some(Modal::Form(f)) => assert_eq!(f.fields[0].widget.value(), "id-0"),
39
        _ => panic!("expected form modal"),
40
    }
41
1
}
42

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

            
94
2
fn select_app(query: Option<char>) -> crate::app::App {
95
2
    let mut sw = SelectWidget::new();
96
2
    sw.set_options(vec![SelectOption {
97
2
        id: "id-alpha".to_string(),
98
2
        label: "Alpha".to_string(),
99
2
    }]);
100
2
    if let Some(c) = query {
101
1
        sw.filter_push(c);
102
1
    }
103
2
    let mut app = crate::test_support::app();
104
2
    let form = Form {
105
2
        fields: vec![Field {
106
2
            key: None,
107
2
            label: "pick",
108
2
            widget: Widget::Select(sw),
109
2
        }],
110
2
        focus: 0,
111
2
        kind: FormKind::ConfigSet,
112
2
        ..Form::config_set()
113
2
    };
114
2
    app.open_dialog_on_front(Modal::Form(form));
115
2
    app.set_status("submit failed: boom");
116
2
    app
117
2
}
118

            
119
#[test]
120
1
fn confirming_a_filtering_select_keeps_the_status() {
121
1
    let mut app = select_app(Some('a'));
122
1
    apply(&mut app, Intent::SelectConfirm);
123
1
    assert_eq!(app.status, "submit failed: boom");
124
1
    assert!(matches!(app.overlays.top(), Some(Modal::Form(_))));
125
1
}
126

            
127
#[test]
128
1
fn enter_on_a_select_without_a_query_submits_and_clears_the_status() {
129
1
    let mut app = select_app(None);
130
1
    apply(&mut app, Intent::Submit);
131
1
    assert_eq!(app.status, "console not connected");
132
1
}