Lines
96.82 %
Functions
100 %
Branches
//! Commodity edit: the form opens from a `load-form` reply, and submits and
//! settles through the shared descriptor.
use forms::{COMMODITY_EDIT, FormValues, Row, write_payload};
use super::edit::{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::{Route, RouteCtx};
use crate::tabs::fetch::Fetch;
use crate::view::ViewId;
use crate::widgets::Widget;
const COMMODITY: &str = "33333333-0000-0000-0000-000000000001";
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", COMMODITY);
values.set("revision", "rev-1");
values.set("symbol", "USD");
values.set("name", "US Dollar");
values.set_rows("tags");
values.push_row("tags", row(&[("name", "kind"), ("value", "fiat")]));
write_payload(&COMMODITY_EDIT, &values)
fn open_app() -> App {
let mut app = crate::test_support::app();
app.deliver_commodity_edit_reply(
Some(app.window_of(crate::view::ViewId::Commodities)),
&wire_of(&payload()),
);
app
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:?}"),
fn mutation_route(app: &mut App) {
app.pending_routes.insert(
5,
Route {
target: Some(app.window_of(ViewId::Commodities)),
ctx: RouteCtx::Mutation {
refresh: ViewId::Commodities,
},
#[test]
fn the_reply_fills_symbol_name_tags_and_hidden_values() {
let app = open_app();
let form = top_form(&app);
assert_eq!(form.kind, FormKind::CommodityEdit);
assert_eq!(form.fields.len(), 3, "id and revision are not rendered");
assert_eq!(form.fields[0].widget.value(), "USD");
assert_eq!(form.fields[1].widget.value(), "US Dollar");
assert_eq!(
form.hidden,
[
("id", COMMODITY.to_string()),
("revision", "rev-1".to_string())
]
assert_eq!(tags(form).values().map_or(0, |rows| rows.len()), 1);
#[tokio::test]
async fn opening_dispatches_load_form_for_the_commodity() {
let sent = app.attach_echo();
app.commodities_mut().state = Fetch::Loaded(Vec::new());
app.switch_tab(crate::view::Tab::Commodities);
app.open_commodity_edit_async(COMMODITY);
assert!(
app.pending_routes
.values()
.any(|route| matches!(route.ctx, RouteCtx::CommodityEdit))
tokio::task::yield_now().await;
app.drain_eval();
let echoed = sent.frames().join("\n");
echoed.contains(&format!(r#"(load-form "commodity-edit" "{COMMODITY}")"#)),
"frame: {echoed}"
fn opening_without_a_selection_says_so() {
apply(&mut app, Intent::EditRow);
assert_eq!(app.status, "no commodity selected");
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 commodity"),
"{}",
app.status
async fn an_edited_symbol_and_tags_submit_in_the_payload() {
let mut app = open_app();
let Some(Modal::Form(form)) = app.overlays.top_mut() else {
form.fields[0].widget.set_text("EUR");
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(COMMODITY));
assert_eq!(values.text("revision"), Some("rev-1"));
assert_eq!(values.text("symbol"), Some("EUR"));
assert_eq!(values.text("name"), Some("US Dollar"));
assert_eq!(values.rows("tags").map(<[Row]>::len), Some(2), "{values:?}");
apply(&mut app, Intent::Submit);
assert!(app.pending_form.is_some(), "the form waits for its reply");
echoed.contains(r#"(submit-form "commodity-edit" "(:id "#),
"{echoed}"
assert!(echoed.contains(r#":symbol \"EUR\""#), "{echoed}");
fn a_rejected_symbol_marks_its_field() {
pending_edit(&mut app);
app.route_reply(5, &rejected("is required", "symbol"));
assert_eq!(form.errors, ["Symbol: is required"]);
assert_eq!(form.focus, 0);
fn a_stale_revision_shows_the_reload_status_and_keeps_the_form() {
let message = "the commodity 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("symbol"), Some("USD"));
fn success_closes_the_form_and_refreshes_the_commodities() {
mutation_route(&mut app);
app.route_reply(5, &format!(r#"(:id 5 :value "{COMMODITY}")"#));
assert!(app.pending_form.is_none());
assert_eq!(app.commodities().state, Fetch::Idle);