1
//! Re-picking a commodity and the fills that outlive their pick.
2

            
3
mod common;
4

            
5
use common::{
6
    FIXTURE, cell, focus, mount, open, press, reference, set_reference, type_text, until,
7
};
8
use wasm_bindgen::JsValue;
9
use wasm_bindgen_test::*;
10
use web_sys::{HtmlFormElement, HtmlInputElement};
11

            
12
wasm_bindgen_test_configure!(run_in_browser);
13

            
14
fn to_amount_disabled(form: &HtmlFormElement) -> Result<bool, JsValue> {
15
    Ok(cell(form, "splits.1.to-amount")?.disabled())
16
}
17

            
18
/// Pick `text` from the already cached options without yielding to the event loop.
19
fn pick_now(input: &HtmlInputElement, text: &str) -> Result<(), JsValue> {
20
    type_text(input, text)?;
21
    focus(input)?;
22
    press(input, "Enter")
23
}
24

            
25
/// The to-commodity as a loaded cell carries it: the text in both inputs.
26
fn load_to_commodity(form: &HtmlFormElement, text: &str) -> Result<HtmlInputElement, JsValue> {
27
    set_reference(form, "splits.1.to-commodity", text, text)?;
28
    let to = cell(form, "splits.1.to-commodity")?;
29
    common::fire(&to, "change")?;
30
    Ok(to)
31
}
32

            
33
#[wasm_bindgen_test]
34
async fn re_picking_the_from_commodity_hides_the_to_amount() -> Result<(), JsValue> {
35
    let (host, form) = mount(FIXTURE)?;
36
    let to = load_to_commodity(&form, "EUR")?;
37
    until(|| !to_amount_disabled(&form).unwrap_or(true)).await?;
38

            
39
    open(&to, "usd").await?;
40
    press(&to, "Enter")?;
41

            
42
    assert_eq!(to.value(), "USD");
43
    assert_eq!(
44
        reference(&form, "splits.1.to-commodity")?.value(),
45
        "com-usd"
46
    );
47
    assert!(to_amount_disabled(&form)?);
48
    host.remove();
49
    Ok(())
50
}
51

            
52
#[wasm_bindgen_test]
53
async fn an_id_and_a_name_of_one_commodity_are_the_same() -> Result<(), JsValue> {
54
    let (host, form) = mount(FIXTURE)?;
55
    let to = reference(&form, "splits.1.to-commodity")?;
56
    to.set_value("com-usd");
57
    common::fire(&to, "change")?;
58

            
59
    until(|| to_amount_disabled(&form).unwrap_or(false)).await?;
60

            
61
    to.set_value("com-eur");
62
    common::fire(&to, "change")?;
63
    assert!(!to_amount_disabled(&form)?);
64
    host.remove();
65
    Ok(())
66
}
67

            
68
#[wasm_bindgen_test]
69
async fn a_superseded_account_pick_does_not_fill_its_commodity() -> Result<(), JsValue> {
70
    let (host, form) = mount(FIXTURE)?;
71
    let from = cell(&form, "splits.1.from")?;
72
    let commodity = cell(&form, "splits.1.from-commodity")?;
73
    open(&from, "wallet").await?;
74
    common::fire(&from, "focusout")?;
75
    set_reference(&form, "splits.1.from-commodity", "", "")?;
76

            
77
    pick_now(&from, "cash:usd")?;
78
    pick_now(&from, "wallet")?;
79
    until(|| commodity.value() == "EUR").await?;
80
    common::tick().await?;
81

            
82
    assert_eq!(from.value(), "Assets:Wallet");
83
    assert_eq!(commodity.value(), "EUR");
84
    host.remove();
85
    Ok(())
86
}
87

            
88
#[wasm_bindgen_test]
89
async fn a_superseded_pick_leaves_the_commodity_empty_when_the_last_has_none() -> Result<(), JsValue>
90
{
91
    let (host, form) = mount(FIXTURE)?;
92
    let from = cell(&form, "splits.1.from")?;
93
    let commodity = cell(&form, "splits.1.from-commodity")?;
94
    open(&from, "wallet").await?;
95
    common::fire(&from, "focusout")?;
96
    set_reference(&form, "splits.1.from-commodity", "", "")?;
97

            
98
    pick_now(&from, "cash:usd")?;
99
    pick_now(&from, "food")?;
100
    common::tick().await?;
101
    common::tick().await?;
102

            
103
    assert_eq!(from.value(), "Expenses:Food");
104
    assert_eq!(commodity.value(), "");
105
    host.remove();
106
    Ok(())
107
}