1
use std::collections::BTreeMap;
2

            
3
use forms::{FormValues, Row, TRANSACTION_CREATE, TRANSACTION_EDIT};
4
use rpc::forms::FormOption;
5

            
6
use super::tests::all_controls;
7
use super::*;
8

            
9
const CAFE: &str = "00000000-0000-4000-8000-0000000000c1";
10
const CAFE2: &str = "00000000-0000-4000-8000-0000000000c2";
11
const RENT: &str = "00000000-0000-4000-8000-0000000000d1";
12
const JR: &str = "00000000-0000-4000-8000-0000000000c3";
13

            
14
48
fn account(id: &str, label: &str, path: &str) -> FormOption {
15
48
    FormOption {
16
48
        id: id.parse().unwrap_or_default(),
17
48
        label: label.into(),
18
48
        alias: path.rsplit(':').next().unwrap_or_default().into(),
19
48
        path: path.into(),
20
48
        commodity: None,
21
48
    }
22
48
}
23

            
24
12
fn references(typed: &[(&str, &str)]) -> References {
25
    References {
26
12
        accounts: vec![
27
12
            account(CAFE, "Cafe ‹0000›", "Cafe"),
28
12
            account(CAFE2, "Cafe ‹0001›", "Cafe"),
29
12
            account(JR, "JR", "Transportation:JR"),
30
        ],
31
12
        commodities: Vec::new(),
32
12
        templates: vec![account(RENT, "rent", "rent")],
33
12
        typed: typed
34
12
            .iter()
35
2
            .map(|(address, text)| (address.to_string(), text.to_string()))
36
            .collect::<BTreeMap<_, _>>(),
37
12
        filled: BTreeMap::new(),
38
    }
39
12
}
40

            
41
8
fn split(from: &str, to: &str) -> FormValues {
42
8
    let mut values = FormValues::default();
43
8
    values.push_row(
44
8
        "splits",
45
8
        Row::from([
46
8
            ("from".to_string(), from.to_string()),
47
8
            ("to".to_string(), to.to_string()),
48
8
        ]),
49
8
    );
50
8
    values
51
8
}
52

            
53
/// The value of the control named `name`.
54
14
fn value(html: &str, name: &str) -> Option<String> {
55
14
    all_controls(html)
56
14
        .into_iter()
57
110
        .find(|c| c.attr("name") == Some(name))
58
14
        .and_then(|c| c.attr("value").map(str::to_string))
59
14
}
60

            
61
#[test]
62
2
fn a_select_shows_the_label_and_carries_the_value_in_a_hidden_input() {
63
2
    let html = render_form(
64
2
        &TRANSACTION_EDIT,
65
2
        &split(JR, CAFE2),
66
2
        &[],
67
2
        "/x",
68
2
        &references(&[]),
69
2
    )
70
2
    .unwrap();
71
2
    assert_eq!(value(&html, "splits.0.from").as_deref(), Some(JR));
72
2
    assert_eq!(value(&html, "label:splits.0.from").as_deref(), Some("JR"));
73
2
    assert_eq!(value(&html, "splits.0.to").as_deref(), Some(CAFE2));
74
2
    assert_eq!(
75
2
        value(&html, "label:splits.0.to").as_deref(),
76
2
        Some("Cafe ‹0001›")
77
    );
78
2
    assert!(html.contains(r#"<input type="hidden" name="splits.0.to""#));
79
2
}
80

            
81
#[test]
82
2
fn typed_text_is_echoed_and_unknown_text_shown_as_is() {
83
2
    let html = render_form(
84
2
        &TRANSACTION_CREATE,
85
2
        &split("Nowhere", ""),
86
2
        &[],
87
2
        "/x",
88
2
        &references(&[("splits.0.to", "Caf")]),
89
2
    )
90
2
    .unwrap();
91
2
    assert_eq!(
92
2
        value(&html, "label:splits.0.from").as_deref(),
93
2
        Some("Nowhere")
94
    );
95
2
    assert_eq!(value(&html, "label:splits.0.to").as_deref(), Some("Caf"));
96
2
    assert_eq!(value(&html, "splits.0.to").as_deref(), Some(""));
97
2
}
98

            
99
#[test]
100
2
fn a_cell_a_fill_wrote_renders_marked() {
101
2
    let mut refs = references(&[]);
102
2
    refs.filled.insert("splits.0.to".into(), false);
103
2
    refs.filled.insert("splits.0.from".into(), true);
104
2
    let html = render_form(&TRANSACTION_CREATE, &split("", ""), &[], "/x", &refs).unwrap();
105
2
    assert_eq!(html.matches("data-filled").count(), 2, "{html}");
106
2
    assert_eq!(html.matches("data-copied").count(), 1, "{html}");
107
4
    let input = |name: &str| {
108
4
        html.split("<input")
109
40
            .find(|e| e.contains(&format!("label:{name}\"")))
110
4
            .map(str::to_string)
111
4
    };
112
2
    let (to, from) = (input("splits.0.to"), input("splits.0.from"));
113
2
    assert!(
114
2
        to.as_ref()
115
2
            .is_some_and(|e| e.contains("data-filled") && !e.contains("data-copied")),
116
        "{to:?}"
117
    );
118
2
    assert!(from.is_some_and(|e| e.contains("data-copied")), "{html}");
119
2
}
120

            
121
#[test]
122
2
fn only_an_option_id_resolves() {
123
2
    let refs = references(&[]);
124
2
    for name in ["Cafe", "jr", "Transportation:JR"] {
125
6
        assert!(
126
6
            refs.resolve(forms::Source::Accounts, name).is_none(),
127
            "{name}"
128
        );
129
    }
130
2
    let cafe = refs
131
2
        .resolve(forms::Source::Accounts, CAFE)
132
2
        .map(|o| o.label.as_str());
133
2
    assert_eq!(cafe, Some("Cafe ‹0000›"));
134
2
}
135

            
136
#[test]
137
2
fn buttons_name_what_they_add_remove_and_submit() {
138
2
    let html = render_form(
139
2
        &TRANSACTION_CREATE,
140
2
        &split("", ""),
141
2
        &[],
142
2
        "/x",
143
2
        &references(&[]),
144
2
    )
145
2
    .unwrap();
146
2
    for text in [
147
2
        ">Add tag<",
148
2
        ">Add split<",
149
2
        ">Add split tag<",
150
2
        ">Remove split<",
151
2
        ">Remove split tag<",
152
2
        ">Create transaction<",
153
2
    ] {
154
12
        assert!(html.contains(text), "{text}");
155
    }
156
2
    assert!(!html.contains(">Submit<") && !html.contains(">Add row<"));
157
2
    assert_eq!(widgets::submit_text("transaction-edit"), "Save");
158
2
    assert_eq!(
159
2
        widgets::submit_text("transaction-query"),
160
2
        "Show transactions"
161
    );
162
2
    assert_eq!(widgets::submit_text("other"), "Submit");
163
2
    assert_eq!(widgets::add_text("other"), "Add row");
164
2
    assert_eq!(widgets::remove_text("other"), "Remove");
165
2
}
166

            
167
#[test]
168
2
fn a_template_resolves_to_its_label() {
169
2
    let refs = references(&[]);
170
2
    assert_eq!(
171
2
        refs.label_of(forms::Source::Templates, RENT).as_deref(),
172
2
        Some("rent")
173
    );
174
2
    assert!(refs.label_of(forms::Source::Accounts, RENT).is_none());
175
2
}