From 5a071067933383324deecb4a656c4d81c8119130 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Fri, 6 Sep 2024 22:54:27 +0200 Subject: [PATCH] Ui unit tests --- src/tui/app/machine/mod.rs | 53 +++++++++++++++++++++++++++----------- src/tui/ui/fetch.rs | 9 +++++++ src/tui/ui/mod.rs | 18 +++++-------- 3 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 src/tui/ui/fetch.rs diff --git a/src/tui/app/machine/mod.rs b/src/tui/app/machine/mod.rs index 24f4711..8c2e2f5 100644 --- a/src/tui/app/machine/mod.rs +++ b/src/tui/app/machine/mod.rs @@ -170,7 +170,8 @@ mod tests { use crate::tui::{ app::{AppState, IAppInteract, IAppInteractBrowse}, - lib::{interface::musicbrainz::MockIMusicBrainz, MockIMusicHoard}, EventChannel, + lib::{interface::musicbrainz::MockIMusicBrainz, MockIMusicHoard}, + EventChannel, }; use super::*; @@ -337,24 +338,46 @@ mod tests { assert!(!app.is_running()); } - // #[test] - // fn state_matches() { - // let mut app = App::new(music_hoard_init(vec![]), mb_api()); - // assert!(app.is_running()); + #[test] + fn state_fetch() { + let mut app = App::new(music_hoard_init(vec![]), mb_api(), events()); + assert!(app.is_running()); - // let (_, rx) = mpsc::channel(); - // app = AppMachine::app_matches(app.unwrap_browse().inner, rx); + let (_, rx) = mpsc::channel(); + let inner = app.unwrap_browse().inner; + let state = AppFetch::new(rx); + app = AppMachine { inner, state }.into(); - // let state = app.state(); - // assert!(matches!(state, AppState::Matches(_))); - // app = state; + let state = app.state(); + assert!(matches!(state, AppState::Fetch(_))); + app = state; - // let public = app.get(); - // assert!(matches!(public.state, AppState::Matches(_))); + let public = app.get(); + assert!(matches!(public.state, AppState::Fetch(_))); - // let app = app.force_quit(); - // assert!(!app.is_running()); - // } + let app = app.force_quit(); + assert!(!app.is_running()); + } + + #[test] + fn state_matches() { + let mut app = App::new(music_hoard_init(vec![]), mb_api(), events()); + assert!(app.is_running()); + + let (_, rx) = mpsc::channel(); + let fetch = AppFetch::new(rx); + app = AppMachine::matches(app.unwrap_browse().inner, AppMatches::new(None, fetch)).into(); + + let state = app.state(); + assert!(matches!(state, AppState::Matches(_))); + app = state; + + let public = app.get(); + assert!(matches!(public.state, AppState::Matches(_))); + + let app = app.force_quit(); + assert!(!app.is_running()); + } #[test] fn state_error() { diff --git a/src/tui/ui/fetch.rs b/src/tui/ui/fetch.rs new file mode 100644 index 0000000..063b059 --- /dev/null +++ b/src/tui/ui/fetch.rs @@ -0,0 +1,9 @@ +use ratatui::widgets::Paragraph; + +pub struct FetchOverlay; + +impl FetchOverlay { + pub fn paragraph<'a>() -> Paragraph<'a> { + Paragraph::new(" -- fetching --") + } +} diff --git a/src/tui/ui/mod.rs b/src/tui/ui/mod.rs index dcf0f4a..b40d809 100644 --- a/src/tui/ui/mod.rs +++ b/src/tui/ui/mod.rs @@ -1,6 +1,7 @@ mod browse; mod display; mod error; +mod fetch; mod info; mod matches; mod minibuffer; @@ -9,6 +10,7 @@ mod reload; mod style; mod widgets; +use fetch::FetchOverlay; use ratatui::{layout::Rect, widgets::Paragraph, Frame}; use musichoard::collection::{album::Album, Collection}; @@ -125,21 +127,14 @@ impl Ui { .with_width(OverlaySize::Value(39)) .with_height(OverlaySize::Value(4)) .build(frame.size()); - let reload_text = ReloadOverlay::paragraph(); - UiWidget::render_overlay_widget("Reload", reload_text, area, false, frame); } fn render_fetch_overlay(frame: &mut Frame) { let area = OverlayBuilder::default().build(frame.size()); - UiWidget::render_overlay_widget( - "Fetching", - Paragraph::new(" -- fetching --"), - area, - false, - frame, - ) + let fetch_text = FetchOverlay::paragraph(); + UiWidget::render_overlay_widget("Fetching", fetch_text, area, false, frame) } fn render_matches_overlay( @@ -156,9 +151,7 @@ impl Ui { let area = OverlayBuilder::default() .with_height(OverlaySize::Value(4)) .build(frame.size()); - let error_text = ErrorOverlay::paragraph(msg.as_ref()); - UiWidget::render_overlay_widget(title.as_ref(), error_text, area, true, frame); } } @@ -267,6 +260,9 @@ mod tests { app.state = AppState::Search(""); terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap(); + app.state = AppState::Fetch(()); + terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap(); + app.state = AppState::Error("get rekt scrub"); terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap();