Lines
93.91 %
Functions
100 %
Branches
//! Transaction create through `submit-form`, and `c` on the list tabs.
use crate::app::App;
use crate::event::{Intent, apply};
use crate::form::{Form, FormKind};
use crate::modal::Modal;
use crate::tabs::fetch::Fetch;
use crate::test_support::{key, press};
use crate::view::Tab;
use crate::widgets::splits::{COL_FROM_COMM, COL_VALUE};
use crate::widgets::{FocusedSubWidget, SelectOption, Widget};
use cli_core::render::ListRow;
use ratatui::crossterm::event::KeyCode;
const ACCOUNT: &str = "550e8400-e29b-41d4-a716-446655440000";
fn accounts_app() -> App {
let mut app = crate::test_support::app();
app.switch_tab(Tab::Accounts);
app.accounts_mut().state = Fetch::Loaded(vec![ListRow {
id: Some(ACCOUNT.to_string()),
cells: vec!["Assets:Cash".to_string()],
}]);
app
}
fn top_form(app: &App) -> &Form {
match app.overlays.top() {
Some(Modal::Form(form)) => form,
other => panic!("expected a form, got {other:?}"),
fn first_from(form: &Form) -> &crate::widgets::SelectWidget {
match &form.fields[3].widget {
Widget::Splits(sw) => &sw.rows()[0].from,
other => panic!("field 2 must be Splits, got {other:?}"),
#[test]
fn c_on_the_accounts_tab_opens_the_transaction_form() {
let mut app = accounts_app();
press(&mut app, key(KeyCode::Char('c')));
assert!(matches!(
app.overlays.top(),
Some(Modal::Form(form)) if form.kind == FormKind::TransactionCreate
));
fn c_on_the_transactions_tab_opens_a_blank_transaction_form() {
app.switch_tab(Tab::Transactions);
assert!(first_from(top_form(&app)).value().is_empty());
fn c_on_the_commodities_tab_opens_the_commodity_form() {
app.switch_tab(Tab::Commodities);
Some(Modal::Form(form)) if form.kind == FormKind::CommodityCreate
fn new_transaction_from_an_account_prefills_from_by_uuid() {
apply(&mut app, Intent::NewRow);
let form = top_form(&app);
assert_eq!(form.kind, crate::form::FormKind::TransactionCreate);
assert_eq!(first_from(form).value(), ACCOUNT);
fn the_prefilled_account_keeps_its_uuid_when_options_arrive() {
let Some(Modal::Form(form)) = app.overlays.top_mut() else {
panic!("form must be open");
};
let Widget::Splits(sw) = &mut form.fields[3].widget else {
panic!("field 2 must be Splits");
sw.set_account_options(vec![
SelectOption {
id: String::new(),
label: "(none)".to_string(),
},
id: ACCOUNT.to_string(),
label: "Assets:Cash".to_string(),
]);
let from = first_from(top_form(&app));
assert_eq!(from.value(), ACCOUNT);
assert_eq!(from.display(), "Assets:Cash");
fn new_transaction_without_a_selected_account_says_so() {
assert_eq!(app.status, "no account selected");
assert!(app.overlays.is_empty());
#[tokio::test]
async fn submit_dispatches_submit_form_with_the_descriptor_payload() {
let sent = app.attach_echo();
let mut form = Form::transaction_create();
if let Widget::Splits(sw) = &mut form.fields[3].widget {
sw.set_account_options(vec![SelectOption {
id: "acc-1".to_string(),
label: "Cash".to_string(),
sw.set_commodity_options(vec![SelectOption {
id: "c-1".to_string(),
label: "USD".to_string(),
sw.col_focus = COL_FROM_COMM;
if let Some(FocusedSubWidget::Select(select)) = sw.focused_subwidget_mut() {
select.next();
sw.col_focus = COL_VALUE;
if let Some(FocusedSubWidget::Amount(aw)) = sw.focused_subwidget_mut() {
"7".chars().for_each(|c| aw.insert_char(c));
app.open_dialog_on_front(Modal::Form(form));
apply(&mut app, Intent::Submit);
assert!(app.pending_form.is_some(), "the form waits for its reply");
tokio::task::yield_now().await;
app.drain_eval();
let echoed = sent.frames().join("\n");
assert!(
echoed.contains(r#"(submit-form "transaction-create" "(:date "#),
"frame: {echoed}"
);
echoed.contains(
r#":splits ((:from \"acc-1\" :to \"acc-1\" :from-commodity \"c-1\" :value \"7\"))"#
),