Lines
100 %
Functions
Branches
//! Tab walks a transaction form in the order it is filled, and Shift+Tab
//! walks it back, at desktop and phone width.
use anyhow::{Result, ensure};
use serde_json::json;
use super::driver::{SHIFT_TAB, TAB};
use super::harness::{Harness, with_harness};
use super::page::{DESKTOP, PHONE, login, open};
use super::rows::{Split, fill_splits};
/// The control that has the focus: its name, or the text of a button.
const FOCUSED: &str = "const e = document.activeElement;\
return ((e.name || '').replace('label:', '') || e.textContent.trim());";
/// The cells of the first split on screen, in reading order.
const READING_ORDER: &str = "const row = document.querySelector('[data-row=\"splits\"]');\
const cells = [...row.querySelectorAll(':scope > label')].filter(l => l.offsetParent);\
const at = (l) => l.getBoundingClientRect();\
return cells.sort((a, b) => at(a).top - at(b).top || at(a).left - at(b).left)\
.map(l => l.dataset.cell);";
const ADD_TAG: &str = "button[data-rows-for$=\"-tags\"]";
async fn focus(h: &Harness, script: &str) -> Result<()> {
h.driver.run(script, vec![]).await.map(drop)
}
/// The controls reached by pressing `key` `count` times from the focus.
async fn walk(h: &Harness, key: &str, count: usize) -> Result<Vec<String>> {
let mut stops = Vec::new();
for _ in 0..count {
let focused = h.driver.active().await?;
h.driver.type_into(&focused, key).await?;
let stop = h.driver.run(FOCUSED, vec![]).await?;
stops.push(stop.as_str().unwrap_or_default().to_string());
Ok(stops)
fn names(list: &[&str]) -> Vec<String> {
list.iter().map(ToString::to_string).collect()
async fn tab_through(
h: &Harness,
size: (u32, u32),
splits: &[Split],
forward: &[&str],
) -> Result<()> {
walk_form(h, size, splits, false, forward).await
async fn walk_form(
tagged: bool,
login(h).await?;
h.driver.set_size(size.0, size.1).await?;
open(h, "/transaction/new").await?;
fill_splits(h, splits).await?;
if tagged {
let add = h.driver.find(ADD_TAG).await?;
h.driver.click(&add).await?;
focus(
h,
"document.querySelector('[name=\"note\"]').focus(); return true;",
)
.await?;
let found = walk(h, TAB, forward.len()).await?;
ensure!(found == names(forward), "Tab walked {found:#?}");
let mut backward = names(forward);
backward.pop();
backward.insert(0, "note".into());
backward.reverse();
let back = walk(h, SHIFT_TAB, backward.len()).await?;
ensure!(back == backward, "Shift+Tab walked {back:#?}");
Ok(())
async fn reads_in_tab_order(h: &Harness, expected: &[&str]) -> Result<()> {
let order = h.driver.run(READING_ORDER, vec![]).await?;
ensure!(order == json!(expected), "the cells read {order}");
const ONE: [&str; 9] = [
"splits.0.from",
"splits.0.to",
"splits.0.from-commodity",
"splits.0.value",
"splits.0.to-commodity",
"Add split tag",
"Add tag",
"Add split",
"Create transaction",
];
async fn one_split(h: &Harness, size: (u32, u32)) -> Result<()> {
let split = [Split::plain("Suica", "JR", "199")];
tab_through(h, size, &split, &ONE).await?;
reads_in_tab_order(
&["from", "to", "from-commodity", "value", "to-commodity"],
.await
async fn converting(h: &Harness, size: (u32, u32)) -> Result<()> {
let split = [Split {
to_commodity: Some("EUR"),
to_amount: Some("2"),
..Split::plain("Suica", "JR", "199")
}];
let forward = [
"splits.0.to-amount",
tab_through(h, size, &split, &forward).await?;
let order = [
"from",
"to",
"from-commodity",
"value",
"to-commodity",
"to-amount",
reads_in_tab_order(h, &order).await
async fn two_splits(h: &Harness, size: (u32, u32)) -> Result<()> {
let splits = [
Split::plain("Suica", "JR", "199"),
Split::plain("Suica", "McDonalds", "600"),
"splits.1.from",
"splits.1.to",
"splits.1.from-commodity",
"splits.1.value",
"splits.1.to-commodity",
tab_through(h, size, &splits, &forward).await
async fn with_a_split_tag(h: &Harness, size: (u32, u32)) -> Result<()> {
let split = [Split::plain("Suica", "JR", "199").tagged("to")];
"split-tags.0.name",
"split-tags.0.value",
"split-tags.0.description",
"split-tags.0.side",
"Remove split tag",
tab_through(h, size, &split, &forward).await
async fn with_a_transaction_tag(h: &Harness, size: (u32, u32)) -> Result<()> {
"tags.0.name",
"tags.0.value",
"tags.0.description",
"Remove tag",
walk_form(h, size, &split, true, &forward).await
#[supp_macro::shared_runtime_test]
async fn desktop_tabs_a_transaction_tag_before_the_splits() -> Result<()> {
with_harness(|h| Box::pin(with_a_transaction_tag(h, DESKTOP))).await
async fn phone_tabs_a_transaction_tag_before_the_splits() -> Result<()> {
with_harness(|h| Box::pin(with_a_transaction_tag(h, PHONE))).await
async fn desktop_tabs_one_split_in_order() -> Result<()> {
with_harness(|h| Box::pin(one_split(h, DESKTOP))).await
async fn phone_tabs_one_split_in_order() -> Result<()> {
with_harness(|h| Box::pin(one_split(h, PHONE))).await
async fn desktop_tabs_to_the_converted_amount() -> Result<()> {
with_harness(|h| Box::pin(converting(h, DESKTOP))).await
async fn phone_tabs_to_the_converted_amount() -> Result<()> {
with_harness(|h| Box::pin(converting(h, PHONE))).await
async fn desktop_tabs_two_splits_and_back() -> Result<()> {
with_harness(|h| Box::pin(two_splits(h, DESKTOP))).await
async fn phone_tabs_two_splits_and_back() -> Result<()> {
with_harness(|h| Box::pin(two_splits(h, PHONE))).await
async fn desktop_tabs_a_split_tag_before_its_add_button() -> Result<()> {
with_harness(|h| Box::pin(with_a_split_tag(h, DESKTOP))).await
async fn phone_tabs_a_split_tag_before_its_add_button() -> Result<()> {
with_harness(|h| Box::pin(with_a_split_tag(h, PHONE))).await