Lines
96.27 %
Functions
100 %
Branches
//! Clicks on grid cells and on the rows of an open drop-down, under every key preset.
use super::click_tests::{click_on, now, presets, press};
use super::*;
use crate::form::Form;
use crate::test_support::{area_of, draw_into, redraw};
use crate::widgets::grid::CellId;
use crate::widgets::splits::{COL_FROM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE};
use crate::widgets::{FocusedSubWidget, SelectOption, SelectWidget, SplitsWidget, Widget};
use std::time::Duration;
use tvui::BarPart;
const SPLITS: usize = 3;
fn options(n: usize) -> Vec<SelectOption> {
(0..n)
.map(|i| SelectOption {
id: format!("id{i}"),
label: format!("label{i}"),
})
.collect()
}
fn cell(col: usize) -> Target {
let cell = CellId {
tag: false,
row: 0,
col,
};
Target::Cell {
field: SPLITS,
cell,
fn transaction(mut app: App, n: usize) -> App {
let mut form = Form::transaction_create();
if let Some(Widget::Splits(splits)) = form.fields.get_mut(SPLITS).map(|f| &mut f.widget) {
splits.set_account_options(options(n));
splits.set_commodity_options(options(n));
app.overlays.push(Modal::Form(form));
app
fn splits(app: &mut App) -> Option<&mut SplitsWidget> {
match app.overlays.top_mut() {
Some(Modal::Form(form)) => match form.fields.get_mut(SPLITS).map(|f| &mut f.widget) {
Some(Widget::Splits(splits)) => Some(splits),
_ => None,
},
fn form(app: &App) -> &Form {
match app.overlays.top() {
Some(Modal::Form(form)) => form,
_ => panic!("no form is open"),
fn open(app: &App) -> bool {
form(app).focused_select().is_some_and(|select| select.open)
fn value(app: &App) -> Option<String> {
form(app).focused_select().map(|s| s.value().to_string())
fn middle(rect: Rect) -> Position {
Position::new(rect.x + rect.width / 2, rect.y)
#[test]
fn a_click_on_a_cell_focuses_it_without_opening_a_select() {
for app in presets() {
let mut app = transaction(app, 3);
click_on(&mut app, cell(COL_TO), now());
assert!(form(&app).cell_focused(SPLITS, cell_id(COL_TO)));
assert!(!open(&app));
fn cell_id(col: usize) -> CellId {
CellId {
fn a_click_on_the_focused_select_cell_opens_it() {
let start = now();
click_on(&mut app, cell(COL_TO), start);
click_on(&mut app, cell(COL_TO), start + Duration::from_secs(2));
assert!(open(&app));
fn a_click_on_a_cell_leaves_a_list_that_was_closed_closed_on_another_cell() {
click_on(&mut app, cell(COL_FROM), start + Duration::from_secs(2));
assert!(form(&app).cell_focused(SPLITS, cell_id(COL_FROM)));
fn a_quick_second_click_is_tested_against_the_cell_the_first_one_hit() {
focus_splits(&mut app, COL_TO_AMOUNT);
draw_into(&mut app, 70, 24);
let first = middle(area_of(&app, cell(COL_TO_COMM)));
press(
&mut app,
first,
MouseEventKind::Down(MouseButton::Left),
start,
);
let under = app.hits.hit_at(first).map(|(_, target, _)| target);
assert_ne!(under, Some(cell(COL_TO_COMM)), "the grid did not shift");
let later = start + Duration::from_millis(100);
later,
assert!(form(&app).cell_focused(SPLITS, cell_id(COL_TO_COMM)));
fn a_slow_second_click_lands_on_what_is_under_the_pointer_now() {
let later = start + Duration::from_secs(2);
/// Puts the focus on column `col` of the first split.
fn focus_splits(app: &mut App, col: usize) {
if let Some(Modal::Form(form)) = app.overlays.top_mut() {
form.focus = SPLITS;
if let Some(splits) = splits(app) {
splits.col_focus = col;
fn open_from(app: &mut App) {
focus_splits(app, COL_FROM);
splits.open_select();
fn a_click_on_an_option_picks_it_and_closes_the_list() {
let mut app = transaction(app, 4);
open_from(&mut app);
click_on(&mut app, Target::Option(2), now());
assert_eq!(value(&app).as_deref(), Some("id2"));
fn a_click_elsewhere_closes_the_list_and_does_nothing_else() {
if let Some(splits) = splits(&mut app) {
splits.set_account_fills(fills());
apply(&mut app, Intent::SelectNext);
assert_eq!(value(&app).as_deref(), Some("id1"));
assert_eq!(from_commodity(&mut app), "id2");
let focus = form(&app).focus;
click_on(&mut app, Target::Field(0), now());
assert_eq!(form(&app).focus, focus);
assert_eq!(value(&app).as_deref(), Some("id0"));
assert_eq!(from_commodity(&mut app), "");
/// Picking account `id1` fills the commodity `id2`.
fn fills() -> Vec<cli_core::forms::FormOption> {
let fill = cli_core::forms::FormOption {
id: "id1".to_string(),
label: String::new(),
alias: String::new(),
commodity: Some("id2".to_string()),
vec![fill]
fn from_commodity(app: &mut App) -> String {
let first = splits(app).and_then(|splits| splits.rows().first());
first.map_or_else(String::new, |row| row.from_commodity.value().to_string())
fn a_click_on_a_dropdown_border_keeps_the_list_open() {
redraw(&mut app);
let area = area_of(&app, Target::Body(crate::hits::Surface::Options));
let at = Position::new(area.x, area.y);
press(&mut app, at, MouseEventKind::Down(MouseButton::Left), now());
fn focused_index(app: &App) -> Option<usize> {
form(app)
.focused_select()
.map(SelectWidget::focused_in_filtered)
fn the_dropdown_bar_steps_a_line_and_a_page_and_the_thumb_does_nothing() {
let mut app = transaction(app, 30);
let bar = |part| Target::Scroll(crate::hits::Surface::Options, part);
click_on(&mut app, bar(BarPart::Down), now());
assert_eq!(focused_index(&app), Some(1));
for expected in [9, 17, 25] {
click_on(&mut app, bar(BarPart::PageDown), now());
assert_eq!(focused_index(&app), Some(expected));
click_on(&mut app, bar(BarPart::Thumb), now());
assert_eq!(focused_index(&app), Some(25));
click_on(&mut app, bar(BarPart::PageUp), now());
assert_eq!(focused_index(&app), Some(17));
click_on(&mut app, bar(BarPart::Up), now());
assert_eq!(focused_index(&app), Some(16));
fn page_keys_move_the_open_list_a_page_under_every_preset() {
use crate::test_support::{key, press};
use ratatui::crossterm::event::KeyCode;
draw_into(&mut app, 80, 24);
press(&mut app, key(KeyCode::PageDown));
assert_eq!(focused_index(&app), Some(8));
press(&mut app, key(KeyCode::PageUp));
fn a_second_click_on_the_focused_plain_select_field_opens_it() {
form.fields.insert(0, select_field());
form.focus = 1;
click_on(&mut app, Target::Field(0), start);
click_on(&mut app, Target::Field(0), start + Duration::from_secs(2));
fn select_field() -> crate::form::Field {
let mut select = SelectWidget::new();
select.set_options(options(3));
crate::form::Field {
key: None,
label: "pick",
widget: Widget::Select(select),
fn a_quick_second_click_does_not_close_the_list_the_first_one_opened() {
click_on(&mut app, cell(COL_TO), start + Duration::from_millis(50));
click_on(&mut app, cell(COL_TO), start + Duration::from_millis(100));
click_on(&mut app, Target::Field(0), start + Duration::from_secs(3));
fn amount_text(col: usize) -> Target {
Target::Caret {
cell: cell_id(col).into(),
fn a_quick_second_click_on_a_text_cell_is_tested_against_the_cell_the_first_one_hit() {
if let Some(splits) = splits(&mut app)
&& let Some(FocusedSubWidget::Amount(amount)) = splits.focused_subwidget_mut()
{
amount.as_editor_mut().replace_buffer("42");
draw_into(&mut app, 60, 24);
let first = middle(area_of(&app, amount_text(COL_VALUE)));
assert_ne!(
under,
Some(amount_text(COL_VALUE)),
"the grid did not shift"
assert!(form(&app).cell_focused(SPLITS, cell_id(COL_VALUE)));
fn a_click_on_the_mark_of_a_tail_clipped_account_opens_it_and_focus_shows_its_start() {
let long = "Expenses:Food:Groceries:Weekly";
splits.set_account_options(vec![SelectOption {
id: "long".to_string(),
label: long.to_string(),
}]);
splits.col_focus = COL_FROM;
splits.edit_select(|select| select.select_id("long"));
splits.col_focus = COL_VALUE;
let mark = Target::Mark {
cell: Some(cell_id(COL_FROM)),
click_on(&mut app, mark, now());
let Some(splits) = splits(&mut app) else {
continue;
let grid = splits.grid(true, None, None);
let item = &crate::test_support::cells(&grid, 0)[2];
assert_eq!(item.shown(Rect::new(0, 0, 12, 1)), long);