Lines
99.23 %
Functions
100 %
Branches
//! A dialog raises the window it belongs to, keeps it from the request to the reply, and forgets
//! it once the dialog is over.
use super::*;
use crate::event::{Intent, apply};
use crate::form::Form;
use crate::modal::Modal;
use forms::FormValues;
const CASH: &str = "550e8400-e29b-41d4-a716-446655440071";
fn run(app: &mut App, command: &str) {
apply(app, Intent::OpenCommandLine);
command
.chars()
.for_each(|c| apply(app, Intent::InsertChar(c)));
apply(app, Intent::Submit);
}
fn loading(app: &App, window: WindowId) -> bool {
app.win_by(window).is_some_and(|win| win.view.loading())
fn origin_window(app: &App) -> Option<WindowId> {
app.overlays.origin().map(|origin| origin.window)
fn the_only_request(app: &App) -> i64 {
let mut ids = app.pending_routes.keys();
match (ids.next(), ids.next()) {
(Some(id), None) => *id,
_ => panic!("one request expected"),
fn account_options() -> String {
super::edit::options_wire(&[(CASH, "Assets:Cash")])
#[tokio::test]
async fn a_dialog_raises_its_rear_origin() {
let mut app = crate::test_support::app();
app.attach_echo();
let tags = app.window_of(ViewId::Tags);
assert_ne!(app.front_window(), Some(tags));
app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
assert_eq!(app.front_window(), Some(tags));
assert_eq!(origin_window(&app), Some(tags));
async fn a_dialog_loads_the_rear_window_it_raises() {
assert!(!loading(&app, tags));
assert!(loading(&app, tags));
async fn a_dialog_for_a_missing_window_opens_it_loaded() {
app.close_window(app.window_of(ViewId::Config));
app.open_dialog_for(ViewId::Config, Modal::Form(Form::config_set()));
let config = app.window_of(ViewId::Config);
assert_eq!(app.front_window(), Some(config));
assert!(loading(&app, config));
async fn a_filter_for_a_missing_window_opens_it_loaded() {
app.close_window(app.window_of(ViewId::Transactions));
app.filter_transactions(FormValues::default());
let window = app.window_of(ViewId::Transactions);
let loads = app
.pending_routes
.values()
.filter(|route| route.target == Some(window) && matches!(route.ctx, RouteCtx::None))
.count();
assert_eq!(loads, 1);
async fn a_merge_reply_opens_its_form_over_the_window_that_asked() {
let older = app.window_of(ViewId::Accounts);
let asked = app.open_extra(ViewId::Accounts);
let mut values = FormValues::default();
values.set("from", CASH);
app.open_account_merge(values);
app.raise_window(older);
app.route_reply(the_only_request(&app), &account_options());
assert_eq!(origin_window(&app), Some(asked));
assert_eq!(app.front_window(), Some(asked));
async fn a_balance_reply_prints_in_the_console_that_asked() {
let older = app.window_of(ViewId::Console);
let asked = app.open_extra(ViewId::Console);
app.show_account_balance("Assets:Cash");
let printed = |window| {
app.win::<ConsoleState>(window).is_some_and(|console| {
console
.scrollback
.iter()
.any(|l| l.contains("get-balances"))
})
};
assert!(printed(asked));
assert!(!printed(older));
async fn a_report_lookup_runs_the_report_in_the_window_that_asked() {
let older = app.window_of(ViewId::Reports);
let asked = app.open_extra(ViewId::Reports);
run(&mut app, "reports balance target=usd");
let options =
r#"(:id 1 :value "((:id \"550e8400-e29b-41d4-a716-446655440001\" :label \"usd\"))")"#;
app.route_reply(the_only_request(&app), options);
assert!(loading(&app, asked));
assert!(!loading(&app, older));
async fn a_closed_dialog_leaves_no_origin_for_the_next_push() {
assert!(origin_window(&app).is_some());
app.overlays.pop();
assert_eq!(app.overlays.origin(), None);
app.overlays.push(Modal::Form(Form::tag_create()));
assert_eq!(app.dialog_window(), None);
assert_eq!(app.status, "no window");
async fn an_unlocked_form_comes_back_with_its_window() {
apply(&mut app, Intent::Submit);
let request = app.pending_form.as_ref().map(|pending| pending.request_id);
let request = request.expect("the form is locked");
let rejected = format!(
r#"(:id {request} :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "name" :MESSAGE "required"))))"#
);
app.route_reply(request, &rejected);
assert!(app.pending_form.is_some(), "{}", app.status);