1
//! A new row's to-commodity starts empty, meaning the from-commodity.
2

            
3
use super::*;
4

            
5
6
fn usd_eur() -> Vec<SelectOption> {
6
6
    [("u-usd", "USD"), ("u-eur", "EUR")]
7
        .map(|(id, label)| SelectOption {
8
12
            id: id.to_string(),
9
12
            label: label.to_string(),
10
12
        })
11
        .to_vec()
12
6
}
13

            
14
#[test]
15
1
fn a_row_added_after_the_options_arrive_starts_on_same_as_from() {
16
1
    let mut sw = SplitsWidget::default();
17
1
    sw.set_commodity_options(usd_eur());
18
1
    sw.add_row();
19
1
    let row = &sw.rows()[1];
20
1
    assert_eq!(row.from_commodity.value(), "");
21
1
    assert_eq!(row.to_commodity.value(), "");
22
1
    assert_eq!(row.to_commodity.display(), "same");
23
1
}
24

            
25
#[test]
26
1
fn a_row_present_when_the_options_arrive_starts_on_same_as_from() {
27
1
    let mut sw = SplitsWidget::default();
28
1
    sw.add_row();
29
1
    sw.set_commodity_options(usd_eur());
30
2
    assert!(sw.rows().iter().all(|row| row.to_commodity.value() == ""));
31
1
}
32

            
33
#[test]
34
1
fn a_chosen_to_commodity_survives_a_second_delivery_of_options() {
35
1
    let mut sw = SplitsWidget::default();
36
1
    sw.set_commodity_options(usd_eur());
37
1
    sw.col_focus = COL_TO_COMM;
38
1
    if let Some(FocusedSubWidget::Select(select)) = sw.focused_subwidget_mut() {
39
1
        select.next();
40
1
        select.next();
41
    }
42
1
    sw.set_commodity_options(usd_eur());
43
1
    assert_eq!(sw.rows()[0].to_commodity.value(), "u-eur");
44
1
}
45

            
46
#[test]
47
1
fn a_loaded_row_without_a_to_commodity_keeps_it_empty() {
48
1
    let mut sw = SplitsWidget::default();
49
    let row =
50
1
        forms::Row::from([("from-commodity", "USD")].map(|(k, v)| (k.to_string(), v.to_string())));
51
1
    sw.load_rows(&[row]);
52
1
    sw.set_commodity_options(usd_eur());
53
1
    assert_eq!(sw.rows()[0].text("to-commodity"), "");
54
1
}
55

            
56
#[test]
57
1
fn the_conversion_arrow_stays_hidden_while_the_to_commodity_is_empty() {
58
1
    let mut sw = SplitsWidget::default();
59
1
    sw.set_commodity_options(usd_eur());
60
1
    sw.add_row();
61
1
    sw.row_focus = 0;
62
1
    sw.col_focus = COL_FROM;
63
1
    let grid = sw.grid(true, None, None);
64
1
    assert!(
65
2
        (0..grid.lines.len()).all(|line| crate::test_support::arrows(&grid, line) == 1),
66
        "{grid:?}"
67
    );
68
1
}