1
//! Edit forms load UUID cells: the select shows the option label and
2
//! submits the id.
3

            
4
use super::edit::{
5
    BANK, CASH, EUR, FOOD, PAYLOAD, USD, deliver_options, options_wire, payload_of, top_form,
6
    wire_of,
7
};
8
use crate::app::App;
9
use crate::route::FormOptionsSource;
10
use crate::widgets::Widget;
11

            
12
3
fn open(accounts: &[(&str, &str)]) -> (App, String) {
13
3
    let payload = PAYLOAD.to_string();
14
3
    let mut app = crate::test_support::app();
15
3
    app.form_options_seq.advance();
16
3
    app.deliver_transaction_edit_reply(
17
3
        Some(app.window_of(crate::view::ViewId::Transactions)),
18
3
        &wire_of(&payload),
19
3
    );
20
3
    deliver_options(
21
3
        &mut app,
22
3
        FormOptionsSource::FormAccounts,
23
3
        &options_wire(accounts),
24
3
    );
25
3
    let commodities = options_wire(&[(USD, "USD"), (EUR, "EUR")]);
26
3
    deliver_options(&mut app, FormOptionsSource::FormCommodities, &commodities);
27
3
    (app, payload)
28
3
}
29

            
30
4
fn from_label(app: &App) -> String {
31
4
    let Widget::Splits(sw) = &top_form(app).fields[3].widget else {
32
        panic!("field 2 must be Splits");
33
    };
34
4
    sw.rows()[0].from.display().to_string()
35
4
}
36

            
37
const ALL: [(&str, &str); 3] = [
38
    (CASH, "Assets:Cash"),
39
    (FOOD, "Expenses:Food"),
40
    (BANK, "Assets:Bank"),
41
];
42

            
43
#[test]
44
1
fn a_loaded_id_shows_its_option_label_and_submits_unchanged() {
45
1
    let (app, payload) = open(&ALL);
46
1
    assert_eq!(from_label(&app), "Assets:Cash");
47
1
    assert_eq!(payload_of(&top_form(&app).values()), payload);
48
1
}
49

            
50
#[test]
51
1
fn a_loaded_id_is_never_shown_before_the_options_arrive() {
52
1
    let payload = PAYLOAD.to_string();
53
1
    let mut app = crate::test_support::app();
54
1
    app.deliver_transaction_edit_reply(
55
1
        Some(app.window_of(crate::view::ViewId::Transactions)),
56
1
        &wire_of(&payload),
57
1
    );
58
1
    let shown = from_label(&app);
59
1
    assert!(!shown.contains(CASH), "{shown}");
60
1
    assert_eq!(payload_of(&top_form(&app).values()), payload);
61
1
}
62

            
63
#[test]
64
1
fn a_renamed_entity_shows_its_current_label_and_keeps_its_id() {
65
1
    let renamed = [
66
1
        (CASH, "Assets:Wallet"),
67
1
        (FOOD, "Expenses:Food"),
68
1
        (BANK, "Cash ‹550e›"),
69
1
    ];
70
1
    let (app, payload) = open(&renamed);
71
1
    assert_eq!(from_label(&app), "Assets:Wallet");
72
1
    assert_eq!(payload_of(&top_form(&app).values()), payload);
73
1
}
74

            
75
#[test]
76
1
fn an_id_missing_from_the_options_falls_back_to_the_id_and_submits_unchanged() {
77
1
    let (app, payload) = open(&ALL[1..]);
78
1
    assert_eq!(from_label(&app), format!("{CASH} (unchanged)"));
79
1
    assert_eq!(payload_of(&top_form(&app).values()), payload);
80
1
}