1
//! Palette leaves that switch tabs or open the selected entity's edit form.
2

            
3
use crate::app::App;
4
use crate::event::{Intent, apply};
5
use crate::route::RouteCtx;
6
use crate::tabs::fetch::Fetch;
7
use crate::view::Tab;
8
use cli_core::render::ListRow;
9

            
10
const ID: &str = "550e8400-e29b-41d4-a716-446655440051";
11

            
12
9
fn run(app: &mut App, command: &str) {
13
9
    apply(app, Intent::OpenCommandLine);
14
9
    command
15
9
        .chars()
16
117
        .for_each(|c| apply(app, Intent::InsertChar(c)));
17
9
    apply(app, Intent::Submit);
18
9
}
19

            
20
3
fn loaded() -> Fetch<Vec<ListRow>> {
21
3
    Fetch::Loaded(vec![ListRow {
22
3
        id: Some(ID.to_string()),
23
3
        cells: vec!["x".to_string()],
24
3
    }])
25
3
}
26

            
27
3
fn pending(app: &App) -> Vec<&RouteCtx> {
28
3
    app.pending_routes
29
3
        .values()
30
3
        .map(|route| &route.ctx)
31
        .collect()
32
3
}
33

            
34
#[test]
35
1
fn account_list_switches_to_the_accounts_tab() {
36
1
    let mut app = crate::test_support::app();
37
1
    run(&mut app, "account list");
38
1
    assert_eq!(app.front_kind(), Some(Tab::Accounts));
39
1
    assert!(
40
1
        app.console().scrollback.is_empty(),
41
        "nothing in the console"
42
    );
43
1
}
44

            
45
#[test]
46
1
fn commodity_list_switches_to_the_commodities_tab() {
47
1
    let mut app = crate::test_support::app();
48
1
    run(&mut app, "commodity list");
49
1
    assert_eq!(app.front_kind(), Some(Tab::Commodities));
50
1
    assert!(
51
1
        app.console().scrollback.is_empty(),
52
        "nothing in the console"
53
    );
54
1
}
55

            
56
#[test]
57
1
fn account_edit_without_a_selection_says_so() {
58
1
    let mut app = crate::test_support::app();
59
1
    run(&mut app, "account edit");
60
1
    assert_eq!(app.status, "no account selected");
61
1
}
62

            
63
#[test]
64
1
fn commodity_edit_without_a_selection_says_so() {
65
1
    let mut app = crate::test_support::app();
66
1
    run(&mut app, "commodity edit");
67
1
    assert_eq!(app.status, "no commodity selected");
68
1
}
69

            
70
#[tokio::test]
71
1
async fn account_edit_loads_the_selected_account() {
72
1
    let mut app = crate::test_support::app();
73
1
    app.attach_echo();
74
1
    app.accounts_mut().state = loaded();
75
1
    run(&mut app, "account edit");
76
1
    assert!(matches!(pending(&app)[..], [RouteCtx::AccountEdit]));
77
1
}
78

            
79
#[tokio::test]
80
1
async fn commodity_edit_loads_the_selected_commodity() {
81
1
    let mut app = crate::test_support::app();
82
1
    app.attach_echo();
83
1
    app.commodities_mut().state = loaded();
84
1
    app.switch_tab(Tab::Commodities);
85
1
    run(&mut app, "commodity edit");
86
1
    assert!(matches!(pending(&app)[..], [RouteCtx::CommodityEdit]));
87
1
}
88

            
89
#[tokio::test]
90
1
async fn a_palette_edit_raises_its_window_and_edits_that_selection() {
91
1
    let mut app = crate::test_support::app();
92
1
    app.attach_echo();
93
1
    app.commodities_mut().state = loaded();
94
1
    assert!(!app.front_is(Tab::Commodities));
95
1
    run(&mut app, "commodity edit");
96
1
    assert!(app.front_is(Tab::Commodities));
97
1
    assert!(matches!(pending(&app)[..], [RouteCtx::CommodityEdit]));
98
1
}
99

            
100
#[tokio::test]
101
1
async fn a_palette_edit_without_its_window_says_so() {
102
1
    let mut app = crate::test_support::app();
103
1
    app.attach_echo();
104
1
    app.close_window(app.window_of(Tab::Commodities));
105
1
    run(&mut app, "commodity edit");
106
1
    assert_eq!(app.status, "no Commodities window");
107
1
    assert!(app.pending_routes.is_empty());
108
1
}
109

            
110
#[test]
111
1
fn the_removed_account_tag_leaf_is_unknown() {
112
1
    let mut app = crate::test_support::app();
113
1
    run(&mut app, "account tag");
114
1
    assert_eq!(app.status, "unknown command: account tag");
115
1
}