Lines
96.86 %
Functions
100 %
Branches
//! Account edit: the form opens from a `load-form` reply, takes the parent
//! options, and submits and settles through the shared descriptor.
use forms::{ACCOUNT_EDIT, FormValues, Row, write_payload};
use super::edit::{deliver_options, options_wire, top_form, wire_of};
use crate::app::App;
use crate::event::{Intent, apply};
use crate::form::{Form, FormKind};
use crate::modal::Modal;
use crate::route::{FormOptionsSource, Route, RouteCtx};
use crate::tabs::fetch::Fetch;
use crate::view::ViewId;
use crate::widgets::Widget;
const ACCOUNT: &str = "22222222-0000-0000-0000-000000000001";
const ROOT: &str = "550e8400-e29b-41d4-a716-446655440031";
const PARENT: &str = "550e8400-e29b-41d4-a716-446655440032";
fn row(cells: &[(&str, &str)]) -> Row {
cells
.iter()
.map(|(key, text)| (key.to_string(), text.to_string()))
.collect()
}
fn payload() -> String {
let mut values = FormValues::default();
values.set("id", ACCOUNT);
values.set("revision", "rev-1");
values.set("name", "Cash");
values.set("parent", PARENT);
values.set_rows("tags");
values.push_row("tags", row(&[("name", "type"), ("value", "asset")]));
values.push_row("tags", row(&[("name", "memo"), ("value", "daily")]));
write_payload(&ACCOUNT_EDIT, &values)
fn open_app() -> App {
let mut app = crate::test_support::app();
app.form_options_seq.advance();
app.deliver_account_edit_reply(
Some(app.window_of(crate::view::ViewId::Accounts)),
&wire_of(&payload()),
);
app
fn accounts() -> String {
options_wire(&[
(ROOT, "Assets"),
(PARENT, "Assets:Bank"),
(ACCOUNT, "Assets:Cash"),
])
fn pending_edit(app: &mut App) {
let Some(Modal::Form(form)) = app.overlays.pop() else {
panic!("edit form must be open");
};
app.lock_pending_form(5, form);
fn rejected(message: &str, field: &str) -> String {
let detail = format!(r#"((:FIELD "{field}" :MESSAGE "{message}"))"#);
format!(r#"(:id 5 :error (:code form-invalid :message "1 field invalid" :detail {detail}))"#)
fn tags(form: &Form) -> &crate::widgets::RowsWidget {
match &form.fields[2].widget {
Widget::Rows(rw) => rw,
other => panic!("field 2 must be Rows, got {other:?}"),
#[test]
fn the_reply_fills_name_tags_and_hidden_values() {
let app = open_app();
let form = top_form(&app);
assert_eq!(form.kind, FormKind::AccountEdit);
assert_eq!(form.fields.len(), 3, "id and revision are not rendered");
assert_eq!(form.fields[0].widget.value(), "Cash");
assert_eq!(
form.hidden,
[
("id", ACCOUNT.to_string()),
("revision", "rev-1".to_string())
]
assert_eq!(tags(form).values().map_or(0, |rows| rows.len()), 2);
form.values().rows("tags"),
Some(
&[
row(&[("name", "type"), ("value", "asset")]),
row(&[("name", "memo"), ("value", "daily")])
][..]
)
fn the_parent_options_keep_the_loaded_parent_selected() {
let mut app = open_app();
deliver_options(&mut app, FormOptionsSource::FormAccounts, &accounts());
let Widget::Select(select) = &form.fields[1].widget else {
panic!("field 1 must be the parent select");
assert_eq!(select.options().len(), 4, "(none) and the three accounts");
assert_eq!(select.value(), PARENT);
assert_eq!(select.display(), "Assets:Bank");
fn the_values_before_the_options_arrive_keep_the_loaded_parent() {
assert_eq!(top_form(&app).values().text("parent"), Some(PARENT));
fn a_root_account_selects_none_after_the_options_arrive() {
values.set("name", "Assets");
values.set("parent", "");
&wire_of(&write_payload(&ACCOUNT_EDIT, &values)),
assert_eq!(top_form(&app).values().text("parent"), Some(""));
#[tokio::test]
async fn opening_dispatches_load_form_for_the_account() {
let sent = app.attach_echo();
app.open_account_edit_async(ACCOUNT);
assert!(
app.pending_routes
.values()
.any(|route| matches!(route.ctx, RouteCtx::AccountEdit))
tokio::task::yield_now().await;
app.drain_eval();
let echoed = sent.frames().join("\n");
echoed.contains(&format!(r#"(load-form "account-edit" "{ACCOUNT}")"#)),
"frame: {echoed}"
fn opening_without_a_console_says_so() {
assert_eq!(app.status, "console not connected");
assert!(app.overlays.is_empty());
fn a_refused_load_shows_why() {
r#"(:id 1 :error (:code not-found :message "gone"))"#,
app.status.starts_with("can't edit this account"),
"{}",
app.status
async fn an_edited_name_parent_and_tags_submit_in_the_payload() {
let Some(Modal::Form(form)) = app.overlays.top_mut() else {
form.fields[0].widget.set_text("Wallet");
let Widget::Select(select) = &mut form.fields[1].widget else {
select.select_id(ROOT);
let Widget::Rows(rw) = &mut form.fields[2].widget else {
panic!("field 2 must be Rows");
rw.add_row();
for c in "trip".chars() {
if let Some(editor) = rw.focused_editor_mut() {
editor.insert_char(c);
let values = top_form(&app).values();
assert_eq!(values.text("id"), Some(ACCOUNT));
assert_eq!(values.text("revision"), Some("rev-1"));
assert_eq!(values.text("name"), Some("Wallet"));
assert_eq!(values.text("parent"), Some(ROOT));
assert_eq!(values.rows("tags").map(<[Row]>::len), Some(3), "{values:?}");
values.rows("tags").and_then(|rows| rows.get(1)),
Some(&row(&[("name", "trip")]))
apply(&mut app, Intent::Submit);
assert!(app.pending_form.is_some(), "the form waits for its reply");
echoed.contains(r#"(submit-form "account-edit" "(:id "#),
"{echoed}"
assert!(echoed.contains(r#":name \"Wallet\""#), "{echoed}");
fn a_rejected_tag_name_marks_its_cell() {
pending_edit(&mut app);
app.route_reply(5, &rejected("is reserved", "tags.1.name"));
assert_eq!(form.errors, ["Row 2 \u{b7} Name: is reserved"]);
assert_eq!(form.focus, 2);
assert_eq!((tags(form).row_focus, tags(form).col_focus), (1, 0));
fn a_stale_revision_shows_the_reload_status_and_keeps_the_form() {
let message = "the account changed since it was loaded; reload it";
app.route_reply(5, &rejected(message, "revision"));
assert_eq!(form.errors, [message]);
.ends_with("(close the form and open it again to reload)"),
assert_eq!(form.values().text("name"), Some("Cash"));
fn success_closes_the_form_and_refreshes_the_accounts() {
app.pending_routes.insert(
5,
Route {
target: Some(app.window_of(ViewId::Accounts)),
ctx: RouteCtx::Mutation {
refresh: ViewId::Accounts,
},
app.accounts_mut().state = Fetch::Loaded(Vec::new());
app.route_reply(5, &format!(r#"(:id 5 :value "{ACCOUNT}")"#));
assert!(app.pending_form.is_none());
assert_eq!(app.accounts().state, Fetch::Idle);
fn a_settled_edit_shows_the_reply_notice() {
app.route_reply(
&format!(r#"(:id 5 :value "{ACCOUNT}" :notices ("Another account named 'Cash' exists"))"#),
assert_eq!(app.status, "Another account named 'Cash' exists");