test: Use insta, closes #57
Use insta for UI tests, adds snapshots of UI layout
This commit is contained in:
+41
-144
@@ -14,6 +14,9 @@ use crate::{
|
||||
|
||||
use super::popup;
|
||||
|
||||
const SUFFIX_CLEAR: &str = "clear error";
|
||||
const SUFFIX_QUIT: &str = "quit oxker";
|
||||
|
||||
/// Draw an error popup over whole screen
|
||||
pub fn draw(
|
||||
colors: AppColors,
|
||||
@@ -36,24 +39,21 @@ pub fn draw(
|
||||
seconds.unwrap_or(5)
|
||||
)
|
||||
} else {
|
||||
let clear_suffix = "clear error";
|
||||
let clear_text = if keymap.clear == Keymap::new().clear {
|
||||
format!("( {} ) {clear_suffix}", keymap.clear.0)
|
||||
format!("( {} ) {SUFFIX_CLEAR}", keymap.clear.0)
|
||||
} else if let Some(secondary) = keymap.clear.1 {
|
||||
format!(" ( {} | {secondary} ) {clear_suffix}", keymap.clear.0)
|
||||
format!(" ( {} | {secondary} ) {SUFFIX_CLEAR}", keymap.clear.0)
|
||||
} else {
|
||||
format!(" ( {} ) {clear_suffix}", keymap.clear.0)
|
||||
format!(" ( {} ) {SUFFIX_CLEAR}", keymap.clear.0)
|
||||
};
|
||||
|
||||
let quit_suffix = "quit oxker";
|
||||
let quit_text = if keymap.quit == Keymap::new().quit {
|
||||
format!("( {} ) {quit_suffix}", keymap.quit.0)
|
||||
format!("( {} ) {SUFFIX_QUIT}", keymap.quit.0)
|
||||
} else if let Some(secondary) = keymap.quit.1 {
|
||||
format!(" ( {} | {secondary} ) {quit_suffix}", keymap.quit.0)
|
||||
format!(" ( {} | {secondary} ) {SUFFIX_QUIT}", keymap.quit.0)
|
||||
} else {
|
||||
format!(" ( {} ) {quit_suffix}", keymap.quit.0)
|
||||
format!(" ( {} ) {SUFFIX_QUIT}", keymap.quit.0)
|
||||
};
|
||||
|
||||
format!("\n\n{clear_text}\n\n{quit_text}")
|
||||
};
|
||||
|
||||
@@ -94,21 +94,19 @@ pub fn draw(
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
mod tests {
|
||||
|
||||
use super::VERSION;
|
||||
use crate::{
|
||||
app_error::AppError,
|
||||
config::{AppColors, Keymap},
|
||||
ui::draw_blocks::tests::{expected_to_vec, get_result, test_setup},
|
||||
ui::draw_blocks::tests::{get_result, test_setup},
|
||||
};
|
||||
use crossterm::event::KeyCode;
|
||||
use insta::assert_snapshot;
|
||||
use ratatui::style::Color;
|
||||
|
||||
#[test]
|
||||
/// Test that the error popup is centered, red background, white border, white text, and displays the correct text
|
||||
fn test_draw_blocks_error_docker_connect_error() {
|
||||
let (w, h) = (46, 9);
|
||||
let mut setup = test_setup(w, h, true, true);
|
||||
let mut setup = test_setup(46, 9, true, true);
|
||||
|
||||
setup
|
||||
.terminal
|
||||
@@ -122,34 +120,15 @@ mod tests {
|
||||
);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let version_row = format!(" │ oxker::v{VERSION} closing in 04 seconds │ ");
|
||||
let expected = [
|
||||
" ",
|
||||
" ╭───────────────── Error ──────────────────╮ ",
|
||||
" │ │ ",
|
||||
" │ Unable to access docker daemon │ ",
|
||||
" │ │ ",
|
||||
version_row.as_str(),
|
||||
" │ │ ",
|
||||
" ╰──────────────────────────────────────────╯ ",
|
||||
" ",
|
||||
];
|
||||
|
||||
for (row_index, result_row) in get_result(&setup, w) {
|
||||
let expected_row = expected_to_vec(&expected, row_index);
|
||||
assert_snapshot!(setup.terminal.backend());
|
||||
for (row_index, result_row) in get_result(&setup) {
|
||||
for (result_cell_index, result_cell) in result_row.iter().enumerate() {
|
||||
assert_eq!(result_cell.symbol(), expected_row[result_cell_index]);
|
||||
|
||||
match (row_index, result_cell_index) {
|
||||
(0 | 8, _) | (1..=7, 0 | 45) => {
|
||||
assert_eq!(result_cell.bg, Color::Reset);
|
||||
assert_eq!(result_cell.fg, Color::Reset);
|
||||
}
|
||||
_ => {
|
||||
assert_eq!(result_cell.bg, Color::Red);
|
||||
assert_eq!(result_cell.fg, Color::White);
|
||||
}
|
||||
if let (0 | 8, _) = (row_index, result_cell_index) {
|
||||
assert_eq!(result_cell.bg, Color::Reset);
|
||||
assert_eq!(result_cell.fg, Color::Reset);
|
||||
} else {
|
||||
assert_eq!(result_cell.bg, Color::Red);
|
||||
assert_eq!(result_cell.fg, Color::White);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -158,8 +137,7 @@ mod tests {
|
||||
#[test]
|
||||
/// Test that the clearable error popup is centered, red background, white border, white text, and displays the correct text
|
||||
fn test_draw_blocks_error_clearable_error() {
|
||||
let (w, h) = (39, 11);
|
||||
let mut setup = test_setup(w, h, true, true);
|
||||
let mut setup = test_setup(39, 11, true, true);
|
||||
|
||||
setup
|
||||
.terminal
|
||||
@@ -174,25 +152,10 @@ mod tests {
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let expected = [
|
||||
" ",
|
||||
" ╭────────────── Error ──────────────╮ ",
|
||||
" │ │ ",
|
||||
" │ Unable to exec into container │ ",
|
||||
" │ │ ",
|
||||
" │ ( c ) clear error │ ",
|
||||
" │ │ ",
|
||||
" │ ( q ) quit oxker │ ",
|
||||
" │ │ ",
|
||||
" ╰───────────────────────────────────╯ ",
|
||||
" ",
|
||||
];
|
||||
assert_snapshot!(setup.terminal.backend());
|
||||
|
||||
for (row_index, result_row) in get_result(&setup, w) {
|
||||
let expected_row = expected_to_vec(&expected, row_index);
|
||||
for (row_index, result_row) in get_result(&setup) {
|
||||
for (result_cell_index, result_cell) in result_row.iter().enumerate() {
|
||||
assert_eq!(result_cell.symbol(), expected_row[result_cell_index]);
|
||||
|
||||
match (row_index, result_cell_index) {
|
||||
(0 | 10, _) | (1..=9, 0 | 38) => {
|
||||
assert_eq!(result_cell.bg, Color::Reset);
|
||||
@@ -211,8 +174,7 @@ mod tests {
|
||||
#[test]
|
||||
/// Custom colors applied to the error popup correctly
|
||||
fn test_draw_blocks_error_custom_colors() {
|
||||
let (w, h) = (39, 11);
|
||||
let mut setup = test_setup(w, h, true, true);
|
||||
let mut setup = test_setup(39, 11, true, true);
|
||||
|
||||
let mut colors = AppColors::new();
|
||||
colors.popup_error.background = Color::Yellow;
|
||||
@@ -225,25 +187,10 @@ mod tests {
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let expected = [
|
||||
" ",
|
||||
" ╭────────────── Error ──────────────╮ ",
|
||||
" │ │ ",
|
||||
" │ Unable to exec into container │ ",
|
||||
" │ │ ",
|
||||
" │ ( c ) clear error │ ",
|
||||
" │ │ ",
|
||||
" │ ( q ) quit oxker │ ",
|
||||
" │ │ ",
|
||||
" ╰───────────────────────────────────╯ ",
|
||||
" ",
|
||||
];
|
||||
assert_snapshot!(setup.terminal.backend());
|
||||
|
||||
for (row_index, result_row) in get_result(&setup, w) {
|
||||
let expected_row = expected_to_vec(&expected, row_index);
|
||||
for (row_index, result_row) in get_result(&setup) {
|
||||
for (result_cell_index, result_cell) in result_row.iter().enumerate() {
|
||||
assert_eq!(result_cell.symbol(), expected_row[result_cell_index]);
|
||||
|
||||
match (row_index, result_cell_index) {
|
||||
(0 | 10, _) | (1..=9, 0 | 38) => {
|
||||
assert_eq!(result_cell.bg, Color::Reset);
|
||||
@@ -260,10 +207,9 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// Custom keymap applied correct with both 1 and 2 definitions
|
||||
/// Custom keymap applied correctly
|
||||
fn test_draw_blocks_error_custom_keymap() {
|
||||
let (w, h) = (39, 11);
|
||||
let mut setup = test_setup(w, h, true, true);
|
||||
let mut setup = test_setup(39, 11, true, true);
|
||||
|
||||
let mut keymap = Keymap::new();
|
||||
keymap.clear = (KeyCode::BackTab, None);
|
||||
@@ -275,27 +221,12 @@ mod tests {
|
||||
super::draw(AppColors::new(), &AppError::DockerExec, f, &keymap, None);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let expected = [
|
||||
" ",
|
||||
" ╭────────────── Error ──────────────╮ ",
|
||||
" │ │ ",
|
||||
" │ Unable to exec into container │ ",
|
||||
" │ │ ",
|
||||
" │ ( Back Tab ) clear error │ ",
|
||||
" │ │ ",
|
||||
" │ ( F4 ) quit oxker │ ",
|
||||
" │ │ ",
|
||||
" ╰───────────────────────────────────╯ ",
|
||||
" ",
|
||||
];
|
||||
|
||||
for (row_index, result_row) in get_result(&setup, w) {
|
||||
let expected_row = expected_to_vec(&expected, row_index);
|
||||
for (result_cell_index, result_cell) in result_row.iter().enumerate() {
|
||||
assert_eq!(result_cell.symbol(), expected_row[result_cell_index]);
|
||||
}
|
||||
}
|
||||
assert_snapshot!(setup.terminal.backend());
|
||||
}
|
||||
#[test]
|
||||
/// Custom keymap applied with two definitions for each option
|
||||
fn test_draw_blocks_error_custom_keymap_two_definitions() {
|
||||
let mut setup = test_setup(39, 11, true, true);
|
||||
|
||||
let mut keymap = Keymap::new();
|
||||
keymap.clear = (KeyCode::BackTab, Some(KeyCode::Char('m')));
|
||||
@@ -307,27 +238,13 @@ mod tests {
|
||||
super::draw(AppColors::new(), &AppError::DockerExec, f, &keymap, None);
|
||||
})
|
||||
.unwrap();
|
||||
assert_snapshot!(setup.terminal.backend());
|
||||
}
|
||||
|
||||
let expected = [
|
||||
" ",
|
||||
" ╭────────────── Error ──────────────╮ ",
|
||||
" │ │ ",
|
||||
" │ Unable to exec into container │ ",
|
||||
" │ │ ",
|
||||
" │ ( Back Tab | m ) clear error │ ",
|
||||
" │ │ ",
|
||||
" │ ( F4 | End ) quit oxker │ ",
|
||||
" │ │ ",
|
||||
" ╰───────────────────────────────────╯ ",
|
||||
" ",
|
||||
];
|
||||
|
||||
for (row_index, result_row) in get_result(&setup, w) {
|
||||
let expected_row = expected_to_vec(&expected, row_index);
|
||||
for (result_cell_index, result_cell) in result_row.iter().enumerate() {
|
||||
assert_eq!(result_cell.symbol(), expected_row[result_cell_index]);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
/// Custom keymap applied correctly, with 1 definition for the first option, and 2 definitions for the other
|
||||
fn test_draw_blocks_error_custom_keymap_one_two_definitions() {
|
||||
let mut setup = test_setup(39, 11, true, true);
|
||||
|
||||
let mut keymap = Keymap::new();
|
||||
keymap.quit = (KeyCode::F(4), Some(KeyCode::End));
|
||||
@@ -338,26 +255,6 @@ mod tests {
|
||||
super::draw(AppColors::new(), &AppError::DockerExec, f, &keymap, None);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let expected = [
|
||||
" ",
|
||||
" ╭────────────── Error ──────────────╮ ",
|
||||
" │ │ ",
|
||||
" │ Unable to exec into container │ ",
|
||||
" │ │ ",
|
||||
" │ ( c ) clear error │ ",
|
||||
" │ │ ",
|
||||
" │ ( F4 | End ) quit oxker │ ",
|
||||
" │ │ ",
|
||||
" ╰───────────────────────────────────╯ ",
|
||||
" ",
|
||||
];
|
||||
|
||||
for (row_index, result_row) in get_result(&setup, w) {
|
||||
let expected_row = expected_to_vec(&expected, row_index);
|
||||
for (result_cell_index, result_cell) in result_row.iter().enumerate() {
|
||||
assert_eq!(result_cell.symbol(), expected_row[result_cell_index]);
|
||||
}
|
||||
}
|
||||
assert_snapshot!(setup.terminal.backend());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user