Ui unit tests

This commit is contained in:
Wojciech Kozlowski 2024-09-06 22:54:27 +02:00
parent 616f4c9f35
commit 5a07106793
3 changed files with 54 additions and 26 deletions

View File

@ -170,7 +170,8 @@ mod tests {
use crate::tui::{ use crate::tui::{
app::{AppState, IAppInteract, IAppInteractBrowse}, app::{AppState, IAppInteract, IAppInteractBrowse},
lib::{interface::musicbrainz::MockIMusicBrainz, MockIMusicHoard}, EventChannel, lib::{interface::musicbrainz::MockIMusicBrainz, MockIMusicHoard},
EventChannel,
}; };
use super::*; use super::*;
@ -337,24 +338,46 @@ mod tests {
assert!(!app.is_running()); assert!(!app.is_running());
} }
// #[test] #[test]
// fn state_matches() { fn state_fetch() {
// let mut app = App::new(music_hoard_init(vec![]), mb_api()); let mut app = App::new(music_hoard_init(vec![]), mb_api(), events());
// assert!(app.is_running()); assert!(app.is_running());
// let (_, rx) = mpsc::channel(); let (_, rx) = mpsc::channel();
// app = AppMachine::app_matches(app.unwrap_browse().inner, rx); let inner = app.unwrap_browse().inner;
let state = AppFetch::new(rx);
app = AppMachine { inner, state }.into();
// let state = app.state(); let state = app.state();
// assert!(matches!(state, AppState::Matches(_))); assert!(matches!(state, AppState::Fetch(_)));
// app = state; app = state;
// let public = app.get(); let public = app.get();
// assert!(matches!(public.state, AppState::Matches(_))); assert!(matches!(public.state, AppState::Fetch(_)));
// let app = app.force_quit(); let app = app.force_quit();
// assert!(!app.is_running()); 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] #[test]
fn state_error() { fn state_error() {

9
src/tui/ui/fetch.rs Normal file
View File

@ -0,0 +1,9 @@
use ratatui::widgets::Paragraph;
pub struct FetchOverlay;
impl FetchOverlay {
pub fn paragraph<'a>() -> Paragraph<'a> {
Paragraph::new(" -- fetching --")
}
}

View File

@ -1,6 +1,7 @@
mod browse; mod browse;
mod display; mod display;
mod error; mod error;
mod fetch;
mod info; mod info;
mod matches; mod matches;
mod minibuffer; mod minibuffer;
@ -9,6 +10,7 @@ mod reload;
mod style; mod style;
mod widgets; mod widgets;
use fetch::FetchOverlay;
use ratatui::{layout::Rect, widgets::Paragraph, Frame}; use ratatui::{layout::Rect, widgets::Paragraph, Frame};
use musichoard::collection::{album::Album, Collection}; use musichoard::collection::{album::Album, Collection};
@ -125,21 +127,14 @@ impl Ui {
.with_width(OverlaySize::Value(39)) .with_width(OverlaySize::Value(39))
.with_height(OverlaySize::Value(4)) .with_height(OverlaySize::Value(4))
.build(frame.size()); .build(frame.size());
let reload_text = ReloadOverlay::paragraph(); let reload_text = ReloadOverlay::paragraph();
UiWidget::render_overlay_widget("Reload", reload_text, area, false, frame); UiWidget::render_overlay_widget("Reload", reload_text, area, false, frame);
} }
fn render_fetch_overlay(frame: &mut Frame) { fn render_fetch_overlay(frame: &mut Frame) {
let area = OverlayBuilder::default().build(frame.size()); let area = OverlayBuilder::default().build(frame.size());
UiWidget::render_overlay_widget( let fetch_text = FetchOverlay::paragraph();
"Fetching", UiWidget::render_overlay_widget("Fetching", fetch_text, area, false, frame)
Paragraph::new(" -- fetching --"),
area,
false,
frame,
)
} }
fn render_matches_overlay( fn render_matches_overlay(
@ -156,9 +151,7 @@ impl Ui {
let area = OverlayBuilder::default() let area = OverlayBuilder::default()
.with_height(OverlaySize::Value(4)) .with_height(OverlaySize::Value(4))
.build(frame.size()); .build(frame.size());
let error_text = ErrorOverlay::paragraph(msg.as_ref()); let error_text = ErrorOverlay::paragraph(msg.as_ref());
UiWidget::render_overlay_widget(title.as_ref(), error_text, area, true, frame); UiWidget::render_overlay_widget(title.as_ref(), error_text, area, true, frame);
} }
} }
@ -267,6 +260,9 @@ mod tests {
app.state = AppState::Search(""); app.state = AppState::Search("");
terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap(); 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"); app.state = AppState::Error("get rekt scrub");
terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap(); terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap();