Lines
97.17 %
Functions
100 %
Branches
//! The activity report's descriptor form: change the dates and add a group,
//! generate, and see the table follow while the form keeps its values.
use anyhow::{Result, ensure};
use serde_json::json;
use super::harness::{Harness, with_harness};
use super::page::{login, open, wait_for};
use super::report_activity_groups::build_group;
use super::report_filter::transfer;
const LABELS: &str = "h4.activity-group-label";
async fn set_day(h: &Harness, name: &str, day: &str) -> Result<()> {
h.driver
.run(
"const input = document.querySelector(`input[name=\"${arguments[0]}\"]`);\
input.value = arguments[1];\
input.dispatchEvent(new Event('input', { bubbles: true }));",
vec![json!(name), json!(day)],
)
.await
.map(drop)
}
async fn generate(h: &Harness) -> Result<()> {
let button = h
.driver
.find("form.nomi-form button[type=\"submit\"]")
.await?;
h.driver.click(&button).await
async fn label_texts(h: &Harness) -> Result<serde_json::Value> {
"return [...document.querySelectorAll(arguments[0])]\
.map(l => l.textContent.trim().split(/\\s+/)[0]);",
vec![json!(LABELS)],
async fn kept(h: &Harness) -> Result<serde_json::Value> {
"const rows = [...document.querySelectorAll('.activity-group-row')];\
return [document.querySelector('input[name=\"from\"]').value,\
document.querySelector('input[name=\"to\"]').value,\
rows.map(r => r.querySelector('.activity-group-name').value)];",
vec![],
async fn table_rows(h: &Harness) -> Result<u64> {
let rows = h
"return document.querySelectorAll('.report-data table.report-table tbody tr').length;",
Ok(rows.as_u64().unwrap_or_default())
async fn first_group(h: &Harness) -> Result<()> {
wait_for(
&h.driver,
"return document.querySelectorAll('.activity-group-row').length === 2 ? true : null;",
"the default groups",
"document.querySelectorAll('.activity-group-row button.danger')\
.forEach(b => b.click());",
let add = h.driver.find(".activity-groups-actions button").await?;
h.driver.click(&add).await?;
build_group(h, 0, "Mine", &h.account("Wallet").to_string(), false).await?;
generate(h).await
async fn change_and_generate(h: &Harness) -> Result<()> {
transfer(h, h.account("Wallet"), h.account("Cafe"), 600).await?;
transfer(h, h.account("Suica"), h.account("Cafe2"), 300).await?;
login(h).await?;
open(h, "/report/activity").await?;
first_group(h).await?;
"return document.querySelectorAll('h4.activity-group-label').length === 1 ? true : null;",
"the first group's table",
ensure!(table_rows(h).await? > 0, "default range has no rows");
set_day(h, "from", "2001-01-01").await?;
set_day(h, "to", "2001-01-31").await?;
build_group(h, 1, "Card", &h.account("Suica").to_string(), true).await?;
generate(h).await?;
"return document.querySelectorAll('h4.activity-group-label').length === 2 ? true : null;",
"both groups' tables",
ensure!(table_rows(h).await? == 0, "early range has rows");
ensure!(
label_texts(h).await? == json!(["Mine", "Card"]),
"group tables"
);
let after = kept(h).await?;
after == json!(["2001-01-01", "2001-01-31", ["Mine", "Card"]]),
"kept {after}"
Ok(())
#[supp_macro::shared_runtime_test]
async fn changing_the_dates_and_adding_a_group_regenerates_the_activity_table() -> Result<()> {
with_harness(|h| Box::pin(change_and_generate(h))).await
async fn set_filter(h: &Harness, index: usize, text: &str) -> Result<()> {
"const area = document.querySelector(\
`textarea[name=\"groups.${arguments[0]}.filter\"]`);\
area.value = arguments[1];\
area.dispatchEvent(new Event('input', { bubbles: true }));",
vec![json!(index), json!(text)],
async fn editor_after_the_422(h: &Harness) -> Result<serde_json::Value> {
"const bad = document.querySelector('textarea[name=\"groups.1.filter\"]');\
const form = document.querySelector('#nf-report-activity');\
return bad && bad.getAttribute('aria-invalid') && form ? [\
!form.querySelector('.form-banner'),\
[...document.querySelectorAll('.activity-group-row')].map(r => [\
r.querySelector('.activity-group-name').value,\
r.querySelector('.tag-filter-sexpr').value.trim(),\
!!r.querySelector('.field-error')])] : null;",
"the 422 and the marked row",
async fn edit_first_row_visually(h: &Harness, account: &str) -> Result<serde_json::Value> {
"const row = document.querySelectorAll('.activity-group-row')[0];\
const id = row.querySelector('.account-filter-id');\
if (!id) { return null; }\
id.value = arguments[0];\
id.dispatchEvent(new Event('change', { bubbles: true }));\
return row.querySelector('.tag-filter-sexpr').value;",
vec![json!(account)],
async fn invalid_group_then_fix(h: &Harness) -> Result<()> {
let wallet = h.account("Wallet").to_string();
let suica = h.account("Suica").to_string();
build_group(h, 0, "Mine", &wallet, false).await?;
"const name = document.querySelectorAll('.activity-group-name')[1];\
name.value = 'Bad';\
name.dispatchEvent(new Event('input', { bubbles: true }));",
set_filter(h, 1, "(and").await?;
let state = editor_after_the_422(h).await?;
let mine = format!("(account= \"{wallet}\")");
state[0] == json!(true),
"the row error is repeated in a banner"
state[1] == json!([["Mine", mine, false], ["Bad", "(and", true]]),
"editor after the 422: {state}"
let edited = edit_first_row_visually(h, &suica).await?;
edited == json!(format!("(account= \"{suica}\")")),
"the restored builder did not update its textarea: {edited}"
set_filter(h, 1, &format!("(account= \"{wallet}\")")).await?;
"const names = [...document.querySelectorAll('h4.activity-group-label')]\
.map(l => l.textContent.trim().split(/\\s+/)[0]);\
const fixed = !document.querySelector('textarea[aria-invalid]');\
return fixed && names.join() === 'Mine,Bad' ? true : null;",
"the report after the fix",
label_texts(h).await? == json!(["Mine", "Bad"]),
async fn an_invalid_group_filter_marks_its_row_and_the_editor_keeps_working() -> Result<()> {
with_harness(|h| Box::pin(invalid_group_then_fix(h))).await