Lines
99.24 %
Functions
100 %
Branches
//! A dialog or action belongs to the window it started from, and says so when that is gone.
use crate::app::App;
use crate::event::{Intent, apply};
use crate::form::Form;
use crate::modal::Modal;
use crate::route::RouteCtx;
use crate::tabs::fetch::Fetch;
use crate::tabs::tags::TagsTab;
use crate::view::ViewId;
use crate::window::WindowId;
use forms::FormValues;
fn targets(app: &App, matching: fn(&RouteCtx) -> bool) -> Vec<Option<WindowId>> {
let routes = app.pending_routes.values();
routes
.filter(|route| matching(&route.ctx))
.map(|route| route.target)
.collect()
}
fn is_tag_edit(ctx: &RouteCtx) -> bool {
matches!(ctx, RouteCtx::TagEdit)
fn is_tag_create(ctx: &RouteCtx) -> bool {
matches!(ctx, RouteCtx::TagCreate)
#[tokio::test]
async fn a_dialog_routes_to_its_window_after_another_one_is_raised() {
let mut app = crate::test_support::app();
app.attach_echo();
let first = app.window_of(ViewId::Tags);
let second = app.open_extra(ViewId::Tags);
app.raise_window(first);
app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
app.raise_window(second);
assert!(app.submit_tag_create(&FormValues::default()).is_some());
assert_eq!(targets(&app, is_tag_create), [Some(first)]);
async fn a_dialog_of_a_closed_window_says_so_and_routes_nowhere() {
app.close_window(app.window_of(ViewId::Tags));
assert!(app.submit_tag_create(&FormValues::default()).is_none());
assert_eq!(app.status, "the Tags window was closed");
assert!(targets(&app, is_tag_create).is_empty());
async fn a_dialog_from_the_console_raises_the_window_of_its_kind() {
app.focus_console();
assert!(app.front_is(ViewId::Tags));
assert!(!app.console_input_focused());
let tags = app.window_of(ViewId::Tags);
assert_eq!(targets(&app, is_tag_create), [Some(tags)]);
async fn a_filter_reply_raises_its_window_and_takes_the_console_prompt_focus() {
app.filter_transactions(FormValues::default());
let request = *app
.pending_routes
.iter()
.find(|(_, route)| matches!(route.ctx, RouteCtx::TransactionFilter { .. }))
.map(|(id, _)| id)
.expect("a filter request");
assert!(app.console_input_focused());
let wire = format!("(:id {request} :value (:ROWS () :HAS-MORE nil :TOTAL 0))");
app.route_reply(request, &wire);
assert!(app.front_is(ViewId::Transactions));
async fn refreshing_a_kind_fetches_every_window_of_it() {
app.refresh_windows(ViewId::Tags);
for window in [first, second] {
let tags = app.win::<TagsTab>(window);
assert!(tags.is_some_and(TagsTab::any_loading), "{window:?}");
type Action = fn(&mut App, WindowId);
const MISSING_WINDOW_ACTIONS: [(ViewId, &str, Action); 9] = [
(ViewId::Reports, "chart", |app, _| app.toggle_report_chart()),
(ViewId::Reports, "csv", |app, _| app.request_report_csv()),
(ViewId::Reports, "edit report", |app, _| {
app.edit_last_report();
}),
(ViewId::Transactions, "detail", |app, _| {
app.open_transaction_detail_selected();
(ViewId::Transactions, "delete marked", |app, _| {
app.open_delete_marked();
(ViewId::Transactions, "page", |app, _| {
app.page_transactions(true);
(ViewId::Transactions, "edit", |app, _| {
app.open_transaction_edit_async("a");
(ViewId::Transactions, "filter", |app, window| {
app.submit_transaction_filter(window, &FormValues::default(), String::new());
(ViewId::Reports, "run", |app, window| {
app.submit_report(
window,
crate::tabs::reports::ReportKind::Balance,
&FormValues::default(),
);
];
async fn an_action_on_a_missing_window_says_which_window_is_missing() {
for (kind, name, action) in MISSING_WINDOW_ACTIONS {
let window = app.window_of(kind);
app.close_window(window);
app.set_status("");
action(&mut app, window);
assert_eq!(app.status, format!("no {} window", kind.label()), "{name}");
const OLDER_TAG: &str = "aaaaaaaa-0000-0000-0000-000000000001";
const NEWER_TAG: &str = "bbbbbbbb-0000-0000-0000-000000000002";
fn load_tags(app: &mut App, window: WindowId, tag: &str) {
app.fetch_tags(window);
let Some(Fetch::Loading { id }) = app.win::<TagsTab>(window).map(|tab| tab.state.clone())
else {
panic!("the tags window is not loading");
};
let row = format!(r#"(:tag :id "{tag}" :name "kind" :value "fiat" :description "")"#);
app.route_reply(id, &format!("(:id {id} :value ({row}))"));
async fn a_tag_edit_reads_the_selection_of_the_front_window() {
let sent = app.attach_echo();
let older = app.window_of(ViewId::Tags);
let newer = app.open_extra(ViewId::Tags);
load_tags(&mut app, older, OLDER_TAG);
load_tags(&mut app, newer, NEWER_TAG);
app.raise_window(older);
apply(&mut app, Intent::EditRow);
tokio::task::yield_now().await;
app.drain_eval();
let echoed = sent.frames().join("\n");
assert!(echoed.contains(&format!(r#"(load-form "tag-edit" "{OLDER_TAG}")"#)));
assert!(!echoed.contains(NEWER_TAG));
assert_eq!(targets(&app, is_tag_edit), [Some(older)]);