Lines
96.74 %
Functions
100 %
Branches
//! Editing and adding from the Config tab: selection, reloads and the new-key form.
use crate::app::App;
use crate::event::{Intent, apply};
use crate::modal::Modal;
use crate::tabs::fetch::Fetch;
use crate::tabs::nms_eval::double::Sent;
use crate::view::Tab;
fn reply_with(app: &mut App, entries: &str) -> anyhow::Result<()> {
let Fetch::Loading { id } = app.config().state else {
anyhow::bail!("not loading");
};
app.route_reply(id, &format!("(:id {id} :value {entries})"));
Ok(())
}
fn config_app() -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
app.switch_tab(Tab::Config);
(app, sent)
fn form_values(app: &App) -> anyhow::Result<(String, String)> {
let Some(Modal::Form(form)) = app.overlays.top() else {
anyhow::bail!("no form open");
let values = form.values();
Ok((
values.text("key").unwrap_or_default().to_string(),
values.text("value").unwrap_or_default().to_string(),
))
#[tokio::test]
async fn only_the_stored_keys_are_listed_and_editable() -> anyhow::Result<()> {
let (mut app, _) = config_app();
reply_with(&mut app, r#"((:KEY "theme" :VALUE "dark"))"#)?;
assert_eq!(app.config().rows().len(), 1);
apply(&mut app, Intent::EditRow);
assert_eq!(
form_values(&app)?,
("theme".to_string(), "dark".to_string())
);
async fn a_new_key_opens_an_empty_form_and_is_selected_once_set() -> anyhow::Result<()> {
reply_with(
&mut app,
r#"((:KEY "alpha" :VALUE "1") (:KEY "theme" :VALUE "dark"))"#,
)?;
apply(&mut app, Intent::NewRow);
assert_eq!(form_values(&app)?, (String::new(), String::new()));
let Some(Modal::Form(form)) = app.overlays.top_mut() else {
form.fields[0].widget.set_text("mid");
form.fields[1].widget.set_text("v");
apply(&mut app, Intent::Submit);
tokio::task::yield_now().await;
app.route_reply(2, "(:id 2 :value t)");
r#"((:KEY "alpha" :VALUE "1") (:KEY "mid" :VALUE "v") (:KEY "theme" :VALUE "dark"))"#,
assert_eq!(app.config().selected_key(), Some("mid"));
async fn the_selection_follows_the_edited_key_after_a_set() -> anyhow::Result<()> {
r#"((:KEY "aaa" :VALUE "1") (:KEY "theme" :VALUE "dark"))"#,
assert_eq!(app.config().selected_key(), Some("theme"));
async fn an_edit_during_a_reload_uses_the_previous_rows() -> anyhow::Result<()> {
app.fetch_config(app.window_of(crate::view::ViewId::Config));
assert!(app.config().any_loading());
async fn an_edit_with_no_rows_says_why() {
assert!(app.overlays.is_empty());
assert_eq!(app.status, "config is loading");
async fn an_edit_on_an_empty_loaded_list_says_no_key_is_selected() -> anyhow::Result<()> {
reply_with(&mut app, "nil")?;
assert_eq!(app.status, "no config key selected");