Lines
97.74 %
Functions
100 %
Branches
//! The activity groups editor: build two groups, generate, see them in the
//! table and the chart data, and see the editor restored after the swap.
use anyhow::{Result, ensure};
use serde_json::{Value, json};
use super::harness::{Harness, with_harness};
use super::page::{login, open, wait_for};
use super::report_filter::transfer;
const DATA: &str = ".report-data";
pub(super) async fn build_group(
h: &Harness,
index: usize,
name: &str,
account: &str,
flip: bool,
) -> Result<()> {
let written = h
.driver
.run(
"const row = document.querySelectorAll('.activity-group-row')[arguments[0]];\
const name = row.querySelector('.activity-group-name');\
name.value = arguments[1];\
name.dispatchEvent(new Event('input', { bubbles: true }));\
row.querySelector('.tag-group-root > .tag-group-actions \
button[onclick^=\"addReportAccountFilter\"]').click();\
const id = row.querySelector('.account-filter-id');\
id.value = arguments[2];\
id.dispatchEvent(new Event('change', { bubbles: true }));\
if (arguments[3]) { row.querySelector('.activity-group-flip').click(); }\
return row.querySelector('.tag-filter-sexpr').value;",
vec![json!(index), json!(name), json!(account), json!(flip)],
)
.await?;
ensure!(
written == json!(format!("(account= \"{account}\")")),
"group {index} filter: {written}"
);
Ok(())
}
async fn replace_default_groups(h: &Harness) -> Result<()> {
wait_for(
&h.driver,
"return document.querySelectorAll(\
`${arguments[0]} .activity-group-row`).length === 2 ? true : null;",
vec![json!(DATA)],
"the default groups",
h.driver
"document.querySelectorAll('.activity-group-row button.danger')\
.forEach(b => b.click());",
vec![],
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?;
build_group(h, 1, "Card", &h.account("Suica").to_string(), true).await
async fn editor_state(h: &Harness) -> Result<Value> {
"const rows = [...document.querySelectorAll(`${arguments[0]} .activity-group-row`)];\
const labels = [...document.querySelectorAll('h4.activity-group-label')];\
if (rows.length !== 2 || labels.length !== 2) { return null; }\
return rows.map(r => [r.querySelector('.activity-group-name').value,\
r.querySelector('.activity-group-flip').checked,\
r.querySelector('.tag-filter-sexpr').name,\
r.querySelector('.account-filter-id') ? r.querySelector('.account-filter-id').value : null]);",
"the editor to come back from the swap",
.await
async fn chart_text(h: &Harness) -> Result<String> {
let body = h
.run_async(
"const done = arguments[arguments.length - 1];\
const url = document.querySelector('figure.report-chart')\
.getAttribute('data-chart-src-json');\
fetch(url).then(r => r.text()).then(done);",
Ok(body.as_str().unwrap_or_default().to_string())
async fn build_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?;
replace_default_groups(h).await?;
let generate = h
.find("form.nomi-form button[type=\"submit\"]")
h.driver.click(&generate).await?;
"const labels = [...document.querySelectorAll('h4.activity-group-label')];\
return labels.some(l => l.textContent.includes('Mine')) ? true : null;",
"the generated groups",
let state = editor_state(h).await?;
state
== json!([
[
"Mine",
false,
"groups.0.filter",
h.account("Wallet").to_string()
],
"Card",
true,
"groups.1.filter",
h.account("Suica").to_string()
]),
"restored editor: {state}"
let labels = h
"return [...document.querySelectorAll('h4.activity-group-label')]\
.map(l => l.textContent.trim().split(/\\s+/)[0]);",
ensure!(labels == json!(["Mine", "Card"]), "group tables: {labels}");
let chart = chart_text(h).await?;
chart.contains("Mine") && chart.contains("Card"),
"chart data: {chart}"
#[supp_macro::shared_runtime_test]
async fn the_editor_builds_groups_and_comes_back_after_generate() -> Result<()> {
with_harness(|h| Box::pin(build_and_generate(h))).await