Started writing more tests in the handlers
This commit is contained in:
+103
-1
@@ -57,7 +57,9 @@ fn handle_clear_errors(app: &mut App) {
|
||||
fn handle_prompt_toggle(app: &mut App, key: &Key) {
|
||||
match key {
|
||||
_ if *key == DEFAULT_KEYBINDINGS.left.key || *key == DEFAULT_KEYBINDINGS.right.key => {
|
||||
app.data.radarr_data.prompt_confirm = !app.data.radarr_data.prompt_confirm;
|
||||
if let Route::Radarr(_) = app.get_current_route().clone() {
|
||||
app.data.radarr_data.prompt_confirm = !app.data.radarr_data.prompt_confirm;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
@@ -77,3 +79,103 @@ macro_rules! handle_text_box_keys {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::app::App;
|
||||
use crate::event::Key;
|
||||
use crate::handlers::{handle_clear_errors, handle_prompt_toggle};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! simple_stateful_iterable_vec {
|
||||
($name:ident) => {
|
||||
vec![
|
||||
$name {
|
||||
title: "Test 1".to_owned(),
|
||||
..$name::default()
|
||||
},
|
||||
$name {
|
||||
title: "Test 2".to_owned(),
|
||||
..$name::default()
|
||||
},
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! extended_stateful_iterable_vec {
|
||||
($name:ident) => {
|
||||
vec![
|
||||
$name {
|
||||
title: "Test 1".to_owned(),
|
||||
..$name::default()
|
||||
},
|
||||
$name {
|
||||
title: "Test 2".to_owned(),
|
||||
..$name::default()
|
||||
},
|
||||
$name {
|
||||
title: "Test 3".to_owned(),
|
||||
..$name::default()
|
||||
},
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_handle_clear_errors() {
|
||||
let mut app = App::default();
|
||||
app.error = "test error".to_owned().into();
|
||||
|
||||
handle_clear_errors(&mut app);
|
||||
|
||||
assert!(app.error.text.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_handle_prompt_toggle_left() {
|
||||
let mut app = App::default();
|
||||
let key = Key::Left;
|
||||
|
||||
assert!(!app.data.radarr_data.prompt_confirm);
|
||||
|
||||
handle_prompt_toggle(&mut app, &key);
|
||||
|
||||
assert!(app.data.radarr_data.prompt_confirm);
|
||||
|
||||
handle_prompt_toggle(&mut app, &key);
|
||||
|
||||
assert!(!app.data.radarr_data.prompt_confirm);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_handle_prompt_toggle_right() {
|
||||
let mut app = App::default();
|
||||
let key = Key::Right;
|
||||
|
||||
assert!(!app.data.radarr_data.prompt_confirm);
|
||||
|
||||
handle_prompt_toggle(&mut app, &key);
|
||||
|
||||
assert!(app.data.radarr_data.prompt_confirm);
|
||||
|
||||
handle_prompt_toggle(&mut app, &key);
|
||||
|
||||
assert!(!app.data.radarr_data.prompt_confirm);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_handle_prompt_toggle_left_and_right_are_inverses() {
|
||||
let mut app = App::default();
|
||||
|
||||
assert!(!app.data.radarr_data.prompt_confirm);
|
||||
|
||||
handle_prompt_toggle(&mut app, &Key::Left);
|
||||
|
||||
assert!(app.data.radarr_data.prompt_confirm);
|
||||
|
||||
handle_prompt_toggle(&mut app, &Key::Right);
|
||||
|
||||
assert!(!app.data.radarr_data.prompt_confirm);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -493,3 +493,311 @@ impl RadarrHandler<'_> {
|
||||
filter_matches
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
mod test_handle_scroll_up_and_down {
|
||||
use pretty_assertions::assert_eq;
|
||||
use rstest::rstest;
|
||||
|
||||
use crate::app::radarr::ActiveRadarrBlock;
|
||||
use crate::app::App;
|
||||
use crate::event::Key;
|
||||
use crate::handlers::radarr_handlers::RadarrHandler;
|
||||
use crate::handlers::KeyEventHandler;
|
||||
use crate::models::radarr_models::{Collection, DownloadRecord, Movie};
|
||||
use crate::simple_stateful_iterable_vec;
|
||||
|
||||
#[rstest]
|
||||
fn test_collections_scroll(#[values(Key::Up, Key::Down)] key: Key) {
|
||||
let mut app = App::default();
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.set_items(simple_stateful_iterable_vec!(Collection));
|
||||
|
||||
RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Collections).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.collections.current_selection().title,
|
||||
"Test 2".to_owned()
|
||||
);
|
||||
|
||||
RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Collections).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.collections.current_selection().title,
|
||||
"Test 1".to_owned()
|
||||
);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_filtered_collections_scroll(#[values(Key::Up, Key::Down)] key: Key) {
|
||||
let mut app = App::default();
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_collections
|
||||
.set_items(simple_stateful_iterable_vec!(Collection));
|
||||
|
||||
RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Collections).handle();
|
||||
|
||||
assert_eq!(
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_collections
|
||||
.current_selection()
|
||||
.title,
|
||||
"Test 2".to_owned()
|
||||
);
|
||||
|
||||
RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Collections).handle();
|
||||
|
||||
assert_eq!(
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_collections
|
||||
.current_selection()
|
||||
.title,
|
||||
"Test 1".to_owned()
|
||||
);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_movies_scroll(#[values(Key::Up, Key::Down)] key: Key) {
|
||||
let mut app = App::default();
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.movies
|
||||
.set_items(simple_stateful_iterable_vec!(Movie));
|
||||
|
||||
RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Movies).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.movies.current_selection().title,
|
||||
"Test 2".to_owned()
|
||||
);
|
||||
|
||||
RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Movies).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.movies.current_selection().title,
|
||||
"Test 1".to_owned()
|
||||
);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_filtered_movies_scroll(#[values(Key::Up, Key::Down)] key: Key) {
|
||||
let mut app = App::default();
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_movies
|
||||
.set_items(simple_stateful_iterable_vec!(Movie));
|
||||
|
||||
RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Movies).handle();
|
||||
|
||||
assert_eq!(
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_movies
|
||||
.current_selection()
|
||||
.title,
|
||||
"Test 2".to_owned()
|
||||
);
|
||||
|
||||
RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Movies).handle();
|
||||
|
||||
assert_eq!(
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_movies
|
||||
.current_selection()
|
||||
.title,
|
||||
"Test 1".to_owned()
|
||||
);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_downloads_scroll(#[values(Key::Up, Key::Down)] key: Key) {
|
||||
let mut app = App::default();
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.downloads
|
||||
.set_items(simple_stateful_iterable_vec!(DownloadRecord));
|
||||
|
||||
RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Downloads).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.downloads.current_selection().title,
|
||||
"Test 2".to_owned()
|
||||
);
|
||||
|
||||
RadarrHandler::with(&key, &mut app, &ActiveRadarrBlock::Downloads).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.downloads.current_selection().title,
|
||||
"Test 1".to_owned()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
mod test_handle_home_end {
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use crate::app::radarr::ActiveRadarrBlock;
|
||||
use crate::app::App;
|
||||
use crate::event::Key;
|
||||
use crate::extended_stateful_iterable_vec;
|
||||
use crate::handlers::radarr_handlers::RadarrHandler;
|
||||
use crate::handlers::KeyEventHandler;
|
||||
use crate::models::radarr_models::{Collection, DownloadRecord, Movie};
|
||||
|
||||
#[test]
|
||||
fn test_collections_home_end() {
|
||||
let mut app = App::default();
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.set_items(extended_stateful_iterable_vec!(Collection));
|
||||
|
||||
RadarrHandler::with(&Key::End, &mut app, &ActiveRadarrBlock::Collections).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.collections.current_selection().title,
|
||||
"Test 3".to_owned()
|
||||
);
|
||||
|
||||
RadarrHandler::with(&Key::Home, &mut app, &ActiveRadarrBlock::Collections).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.collections.current_selection().title,
|
||||
"Test 1".to_owned()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_filtered_collections_home_end() {
|
||||
let mut app = App::default();
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_collections
|
||||
.set_items(extended_stateful_iterable_vec!(Collection));
|
||||
|
||||
RadarrHandler::with(&Key::End, &mut app, &ActiveRadarrBlock::Collections).handle();
|
||||
|
||||
assert_eq!(
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_collections
|
||||
.current_selection()
|
||||
.title,
|
||||
"Test 3".to_owned()
|
||||
);
|
||||
|
||||
RadarrHandler::with(&Key::Home, &mut app, &ActiveRadarrBlock::Collections).handle();
|
||||
|
||||
assert_eq!(
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_collections
|
||||
.current_selection()
|
||||
.title,
|
||||
"Test 1".to_owned()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_movies_home_end() {
|
||||
let mut app = App::default();
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.movies
|
||||
.set_items(extended_stateful_iterable_vec!(Movie));
|
||||
|
||||
RadarrHandler::with(&Key::End, &mut app, &ActiveRadarrBlock::Movies).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.movies.current_selection().title,
|
||||
"Test 3".to_owned()
|
||||
);
|
||||
|
||||
RadarrHandler::with(&Key::Home, &mut app, &ActiveRadarrBlock::Movies).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.movies.current_selection().title,
|
||||
"Test 1".to_owned()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_filtered_movies_home_end() {
|
||||
let mut app = App::default();
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_movies
|
||||
.set_items(extended_stateful_iterable_vec!(Movie));
|
||||
|
||||
RadarrHandler::with(&Key::End, &mut app, &ActiveRadarrBlock::Movies).handle();
|
||||
|
||||
assert_eq!(
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_movies
|
||||
.current_selection()
|
||||
.title,
|
||||
"Test 3".to_owned()
|
||||
);
|
||||
|
||||
RadarrHandler::with(&Key::Home, &mut app, &ActiveRadarrBlock::Movies).handle();
|
||||
|
||||
assert_eq!(
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.filtered_movies
|
||||
.current_selection()
|
||||
.title,
|
||||
"Test 1".to_owned()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_downloads_home_end() {
|
||||
let mut app = App::default();
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.downloads
|
||||
.set_items(extended_stateful_iterable_vec!(DownloadRecord));
|
||||
|
||||
RadarrHandler::with(&Key::End, &mut app, &ActiveRadarrBlock::Downloads).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.downloads.current_selection().title,
|
||||
"Test 3".to_owned()
|
||||
);
|
||||
|
||||
RadarrHandler::with(&Key::Home, &mut app, &ActiveRadarrBlock::Downloads).handle();
|
||||
|
||||
assert_eq!(
|
||||
app.data.radarr_data.downloads.current_selection().title,
|
||||
"Test 1".to_owned()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user