1
use super::*;
2
use crate::tabs::fetch::Fetch;
3

            
4
3
fn wire(rid: i64, printed: &str) -> String {
5
3
    let escaped = printed.replace('"', "\\\"");
6
3
    format!(r#"(:id {rid} :value "{escaped}")"#)
7
3
}
8

            
9
1
fn names(tab: &TemplatesTab) -> Vec<&str> {
10
2
    tab.rows().iter().map(|row| row.name.as_str()).collect()
11
1
}
12

            
13
#[test]
14
1
fn on_reply_lists_the_printed_options_by_name() {
15
1
    let mut tab = TemplatesTab::new();
16
1
    tab.set_loading(1);
17
1
    tab.on_reply(&wire(
18
1
        1,
19
1
        r#"((:id "t1" :label "rent" :alias "rent") (:id "t2" :label "unnamed" :alias "unnamed"))"#,
20
1
    ));
21
1
    assert_eq!(tab.state, Fetch::Loaded(()));
22
1
    assert_eq!(names(&tab), ["rent", "unnamed"]);
23
1
    assert_eq!(tab.rows()[1].id, "t2");
24
1
}
25

            
26
#[test]
27
1
fn an_empty_list_is_no_rows() {
28
1
    let mut tab = TemplatesTab::new();
29
1
    tab.on_reply(&wire(1, "()"));
30
1
    assert_eq!(tab.state, Fetch::Loaded(()));
31
1
    assert!(tab.rows().is_empty());
32
1
}
33

            
34
#[test]
35
1
fn a_row_without_a_label_is_malformed() {
36
1
    let mut tab = TemplatesTab::new();
37
1
    tab.on_reply(&wire(1, r#"((:id "t1"))"#));
38
1
    assert!(matches!(&tab.state, Fetch::Error(m) if m.contains("malformed template")));
39
1
}
40

            
41
#[test]
42
1
fn cells_are_the_name() {
43
1
    let row = TemplateRow {
44
1
        id: "t1".into(),
45
1
        name: "rent".into(),
46
1
    };
47
1
    assert_eq!(row.cells(), ["rent"]);
48
1
}