1
//! The account-create parent select is filled from `form-options`.
2

            
3
use crate::form::Form;
4
use crate::modal::Modal;
5
use crate::route::{FormOptionsSource, Route, RouteCtx};
6
use crate::view::ViewId;
7
use crate::widgets::{SelectWidget, Widget};
8

            
9
const ROOT: &str = "550e8400-e29b-41d4-a716-446655440001";
10

            
11
2
fn options_wire() -> String {
12
2
    format!(r#"(:id 9 :value "((:id \"{ROOT}\" :label \"Assets\" :alias \"x\"))")"#)
13
2
}
14

            
15
2
fn deliver(app: &mut crate::app::App, seq: u64) {
16
2
    app.deliver_reply(
17
2
        Route {
18
2
            target: Some(app.window_of(ViewId::Accounts)),
19
2
            ctx: RouteCtx::FormOptions {
20
2
                seq,
21
2
                source: FormOptionsSource::FormAccounts,
22
2
            },
23
2
        },
24
2
        &options_wire(),
25
2
    );
26
2
}
27

            
28
2
fn parent(app: &crate::app::App) -> Option<&SelectWidget> {
29
2
    let Some(Modal::Form(form)) = app.overlays.top() else {
30
        return None;
31
    };
32
2
    match &form.fields.get(1)?.widget {
33
2
        Widget::Select(select) => Some(select),
34
        _ => None,
35
    }
36
2
}
37

            
38
2
fn open_app() -> crate::app::App {
39
2
    let mut app = crate::test_support::app();
40
2
    app.overlays.push(Modal::Form(Form::account_create()));
41
2
    app.form_options_seq.advance();
42
2
    app
43
2
}
44

            
45
#[test]
46
1
fn the_parent_select_gets_none_and_the_accounts() {
47
1
    let mut app = open_app();
48
1
    deliver(&mut app, 1);
49
1
    let select = parent(&app).expect("parent select");
50
1
    assert_eq!(select.options().len(), 2);
51
1
    assert_eq!(select.display(), "(none)");
52
1
    assert_eq!(select.value(), "");
53
1
}
54

            
55
#[test]
56
1
fn a_stale_options_reply_is_dropped() {
57
1
    let mut app = open_app();
58
1
    app.form_options_seq.advance();
59
1
    deliver(&mut app, 1);
60
1
    assert_eq!(parent(&app).map(|select| select.options().len()), Some(0));
61
1
}