1
use super::*;
2
use crate::widgets::grid::CellId;
3
use crate::widgets::splits::{COL_FROM, COL_TO, COL_VALUE};
4
use forms::{FormValues, Row};
5

            
6
const SPLITS: usize = 3;
7

            
8
12
fn row(cells: &[(&str, &str)]) -> Row {
9
12
    cells
10
12
        .iter()
11
40
        .map(|(k, v)| (k.to_string(), v.to_string()))
12
        .collect()
13
12
}
14

            
15
4
fn transaction() -> Form {
16
4
    let mut loaded = FormValues::default();
17
4
    loaded.set("id", "tx-1");
18
4
    loaded.set("revision", "3");
19
4
    loaded.set("date", "2024-06-15T10:00");
20
4
    for key in ["r1", "r2"] {
21
8
        loaded.push_row(
22
8
            "splits",
23
8
            row(&[("key", key), ("from", "a"), ("to", "b"), ("value", "1")]),
24
8
        );
25
    }
26
4
    loaded.set_rows("split-tags");
27
4
    loaded.push_row("split-tags", row(&[("row", "r1"), ("name", "k")]));
28
4
    Form::transaction_edit(&loaded)
29
4
}
30

            
31
2
fn splits(form: &mut Form) -> &mut crate::widgets::SplitsWidget {
32
2
    match form.fields.get_mut(SPLITS).map(|field| &mut field.widget) {
33
2
        Some(Widget::Splits(splits)) => splits,
34
        _ => panic!("field {SPLITS} is not the splits"),
35
    }
36
2
}
37

            
38
8
fn cell(row: usize, col: usize) -> CellId {
39
8
    CellId {
40
8
        tag: false,
41
8
        row,
42
8
        col,
43
8
    }
44
8
}
45

            
46
#[test]
47
1
fn focusing_a_split_cell_moves_the_form_and_the_cell() {
48
1
    let mut form = transaction();
49
1
    form.focus_cell(SPLITS, cell(1, COL_VALUE));
50
1
    assert_eq!(form.focus, SPLITS);
51
1
    assert!(form.cell_focused(SPLITS, cell(1, COL_VALUE)));
52
1
    assert!(!form.cell_focused(SPLITS, cell(0, COL_VALUE)));
53
1
}
54

            
55
#[test]
56
1
fn a_cell_of_another_field_is_not_focused() {
57
1
    let mut form = transaction();
58
1
    form.focus_cell(SPLITS, cell(0, COL_TO));
59
1
    form.focus_field(0);
60
1
    assert!(!form.cell_focused(SPLITS, cell(0, COL_TO)));
61
1
}
62

            
63
#[test]
64
1
fn a_field_that_is_not_a_grid_ignores_a_cell_focus() {
65
1
    let mut form = transaction();
66
1
    form.focus_cell(0, cell(0, 0));
67
1
    assert_eq!(form.focus, 0);
68
1
}
69

            
70
#[test]
71
1
fn a_tag_cell_opens_the_tag_list_and_a_split_cell_closes_it() {
72
1
    let mut form = transaction();
73
1
    let tag = CellId {
74
1
        tag: true,
75
1
        row: 0,
76
1
        col: 1,
77
1
    };
78
1
    form.focus_cell(SPLITS, tag);
79
1
    assert!(splits(&mut form).tags_open());
80
1
    assert!(form.cell_focused(SPLITS, tag));
81
1
    form.focus_cell(SPLITS, cell(1, COL_FROM));
82
1
    assert!(!splits(&mut form).tags_open());
83
1
    assert!(form.cell_focused(SPLITS, cell(1, COL_FROM)));
84
1
}