Skip to content

Commit

Permalink
chore(tui): add test for key handling (#9558)
Browse files Browse the repository at this point in the history
### Description

Before I realized that #9543
already fixed #9553 I started
working on a unit test for this section of the code. Putting it up just
so writing future tests should be easier.

### Testing Instructions

Unit test runs and passes.
  • Loading branch information
chris-olszewski authored Dec 4, 2024
1 parent ff89869 commit d985382
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions crates/turborepo-ui/src/tui/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,48 @@ fn encode_modifiers(mods: KeyModifiers) -> u8 {
}
number
}

#[cfg(test)]
mod test {
use std::{mem, sync::OnceLock};

use crossterm::event::{KeyCode, KeyEvent};
use test_case::test_case;

use super::*;
use crate::tui::{search::SearchResults, task::TasksByStatus};

fn search() -> &'static LayoutSections {
static SEARCH: OnceLock<LayoutSections> = OnceLock::new();
SEARCH.get_or_init(|| LayoutSections::Search {
previous_selection: "".into(),
results: SearchResults::new(&TasksByStatus::new()),
})
}

fn in_find() -> InputOptions<'static> {
InputOptions {
focus: search(),
has_selection: false,
}
}

const H: KeyEvent = KeyEvent::new(KeyCode::Char('h'), KeyModifiers::empty());

#[test_case(in_find(), H, Some(Event::SearchEnterChar('h')) ; "h while searching")]
// Note: This only checks event variants not any data contained in the variant
fn test_translate_key_event_variant(
opts: InputOptions,
key_event: crossterm::event::KeyEvent,
expected: Option<Event>,
) {
match (translate_key_event(opts, key_event), expected) {
(Some(actual), Some(expected)) => {
assert_eq!(mem::discriminant(&actual), mem::discriminant(&expected));
}
(None, None) => (),
(None, Some(_)) => panic!("expected event, got None"),
(Some(_), None) => panic!("expected no event, got an event"),
}
}
}

0 comments on commit d985382

Please sign in to comment.