Lines
100 %
Functions
Branches
use std::collections::BTreeMap;
use forms::{FormValues, Row, TRANSACTION_CREATE, TRANSACTION_EDIT};
use rpc::forms::FormOption;
use super::tests::all_controls;
use super::*;
const CAFE: &str = "00000000-0000-4000-8000-0000000000c1";
const CAFE2: &str = "00000000-0000-4000-8000-0000000000c2";
const RENT: &str = "00000000-0000-4000-8000-0000000000d1";
const JR: &str = "00000000-0000-4000-8000-0000000000c3";
fn account(id: &str, label: &str, path: &str) -> FormOption {
FormOption {
id: id.parse().unwrap_or_default(),
label: label.into(),
alias: path.rsplit(':').next().unwrap_or_default().into(),
path: path.into(),
commodity: None,
}
fn references(typed: &[(&str, &str)]) -> References {
References {
accounts: vec![
account(CAFE, "Cafe ‹0000›", "Cafe"),
account(CAFE2, "Cafe ‹0001›", "Cafe"),
account(JR, "JR", "Transportation:JR"),
],
commodities: Vec::new(),
templates: vec![account(RENT, "rent", "rent")],
typed: typed
.iter()
.map(|(address, text)| (address.to_string(), text.to_string()))
.collect::<BTreeMap<_, _>>(),
filled: BTreeMap::new(),
fn split(from: &str, to: &str) -> FormValues {
let mut values = FormValues::default();
values.push_row(
"splits",
Row::from([
("from".to_string(), from.to_string()),
("to".to_string(), to.to_string()),
]),
);
values
/// The value of the control named `name`.
fn value(html: &str, name: &str) -> Option<String> {
all_controls(html)
.into_iter()
.find(|c| c.attr("name") == Some(name))
.and_then(|c| c.attr("value").map(str::to_string))
#[test]
fn a_select_shows_the_label_and_carries_the_value_in_a_hidden_input() {
let html = render_form(
&TRANSACTION_EDIT,
&split(JR, CAFE2),
&[],
"/x",
&references(&[]),
)
.unwrap();
assert_eq!(value(&html, "splits.0.from").as_deref(), Some(JR));
assert_eq!(value(&html, "label:splits.0.from").as_deref(), Some("JR"));
assert_eq!(value(&html, "splits.0.to").as_deref(), Some(CAFE2));
assert_eq!(
value(&html, "label:splits.0.to").as_deref(),
Some("Cafe ‹0001›")
assert!(html.contains(r#"<input type="hidden" name="splits.0.to""#));
fn typed_text_is_echoed_and_unknown_text_shown_as_is() {
&TRANSACTION_CREATE,
&split("Nowhere", ""),
&references(&[("splits.0.to", "Caf")]),
value(&html, "label:splits.0.from").as_deref(),
Some("Nowhere")
assert_eq!(value(&html, "label:splits.0.to").as_deref(), Some("Caf"));
assert_eq!(value(&html, "splits.0.to").as_deref(), Some(""));
fn a_cell_a_fill_wrote_renders_marked() {
let mut refs = references(&[]);
refs.filled.insert("splits.0.to".into(), false);
refs.filled.insert("splits.0.from".into(), true);
let html = render_form(&TRANSACTION_CREATE, &split("", ""), &[], "/x", &refs).unwrap();
assert_eq!(html.matches("data-filled").count(), 2, "{html}");
assert_eq!(html.matches("data-copied").count(), 1, "{html}");
let input = |name: &str| {
html.split("<input")
.find(|e| e.contains(&format!("label:{name}\"")))
.map(str::to_string)
};
let (to, from) = (input("splits.0.to"), input("splits.0.from"));
assert!(
to.as_ref()
.is_some_and(|e| e.contains("data-filled") && !e.contains("data-copied")),
"{to:?}"
assert!(from.is_some_and(|e| e.contains("data-copied")), "{html}");
fn only_an_option_id_resolves() {
let refs = references(&[]);
for name in ["Cafe", "jr", "Transportation:JR"] {
refs.resolve(forms::Source::Accounts, name).is_none(),
"{name}"
let cafe = refs
.resolve(forms::Source::Accounts, CAFE)
.map(|o| o.label.as_str());
assert_eq!(cafe, Some("Cafe ‹0000›"));
fn buttons_name_what_they_add_remove_and_submit() {
&split("", ""),
for text in [
">Add tag<",
">Add split<",
">Add split tag<",
">Remove split<",
">Remove split tag<",
">Create transaction<",
] {
assert!(html.contains(text), "{text}");
assert!(!html.contains(">Submit<") && !html.contains(">Add row<"));
assert_eq!(widgets::submit_text("transaction-edit"), "Save");
widgets::submit_text("transaction-query"),
"Show transactions"
assert_eq!(widgets::submit_text("other"), "Submit");
assert_eq!(widgets::add_text("other"), "Add row");
assert_eq!(widgets::remove_text("other"), "Remove");
fn a_template_resolves_to_its_label() {
refs.label_of(forms::Source::Templates, RENT).as_deref(),
Some("rent")
assert!(refs.label_of(forms::Source::Accounts, RENT).is_none());