Recreate repository due to Git object corruption (all files preserved)
This commit is contained in:
122
tests/component_tests.rs
Normal file
122
tests/component_tests.rs
Normal file
@@ -0,0 +1,122 @@
|
||||
extern crate alloc;
|
||||
|
||||
use tui_orchestrator::component::{Component, ComponentAction};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
enum TestFocus {
|
||||
FieldA,
|
||||
FieldB,
|
||||
ButtonC,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum TestEvent {
|
||||
ButtonCPressed,
|
||||
TextTyped(char),
|
||||
}
|
||||
|
||||
struct TestComponent {
|
||||
field_a: alloc::string::String,
|
||||
field_b: alloc::string::String,
|
||||
}
|
||||
|
||||
impl Component for TestComponent {
|
||||
type Focus = TestFocus;
|
||||
type Action = ComponentAction;
|
||||
type Event = TestEvent;
|
||||
|
||||
fn targets(&self) -> &[Self::Focus] {
|
||||
&[
|
||||
Self::Focus::FieldA,
|
||||
Self::Focus::FieldB,
|
||||
Self::Focus::ButtonC,
|
||||
]
|
||||
}
|
||||
|
||||
fn handle(
|
||||
&mut self,
|
||||
focus: &Self::Focus,
|
||||
action: Self::Action,
|
||||
) -> Result<Option<Self::Event>, tui_orchestrator::component::error::ComponentError> {
|
||||
match (focus, action) {
|
||||
(Self::Focus::ButtonC, ComponentAction::Select) => {
|
||||
Ok(Some(Self::Event::ButtonCPressed))
|
||||
}
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> Result<(), tui_orchestrator::component::error::ComponentError> {
|
||||
self.field_a.clear();
|
||||
self.field_b.clear();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_component_targets() {
|
||||
let mut component = TestComponent {
|
||||
field_a: alloc::string::String::new(),
|
||||
field_b: alloc::string::String::new(),
|
||||
};
|
||||
|
||||
let targets = component.targets();
|
||||
assert_eq!(targets.len(), 3);
|
||||
assert_eq!(targets[0], TestFocus::FieldA);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_component_handle_select() {
|
||||
let mut component = TestComponent {
|
||||
field_a: alloc::string::String::new(),
|
||||
field_b: alloc::string::String::new(),
|
||||
};
|
||||
|
||||
let focus = TestFocus::ButtonC;
|
||||
let action = ComponentAction::Select;
|
||||
|
||||
let event = component.handle(&focus, action);
|
||||
assert!(event.is_ok());
|
||||
assert!(matches!(event.unwrap(), Some(TestEvent::ButtonCPressed)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_component_handle_text() {
|
||||
let mut component = TestComponent {
|
||||
field_a: alloc::string::String::new(),
|
||||
field_b: alloc::string::String::new(),
|
||||
};
|
||||
|
||||
let focus = TestFocus::FieldA;
|
||||
let ch = 'x';
|
||||
|
||||
let event = component.handle_text(&focus, ch);
|
||||
assert!(event.is_ok());
|
||||
assert!(matches!(event.unwrap(), Some(TestEvent::TextTyped('x'))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_component_on_enter_clears() {
|
||||
let mut component = TestComponent {
|
||||
field_a: alloc::string::String::from("test"),
|
||||
field_b: alloc::string::String::from("test"),
|
||||
};
|
||||
|
||||
component.on_enter().unwrap();
|
||||
assert_eq!(component.field_a.as_str(), "");
|
||||
assert_eq!(component.field_b.as_str(), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_component_defaults() {
|
||||
let component = TestComponent {
|
||||
field_a: alloc::string::String::new(),
|
||||
field_b: alloc::string::String::new(),
|
||||
};
|
||||
|
||||
assert!(component.on_exit().is_ok());
|
||||
assert!(component.on_focus(&TestFocus::FieldA).is_ok());
|
||||
assert!(component.on_blur(&TestFocus::FieldA).is_ok());
|
||||
assert!(component.can_navigate_forward(&TestFocus::FieldA));
|
||||
assert!(component.can_navigate_backward(&TestFocus::FieldA));
|
||||
}
|
||||
Reference in New Issue
Block a user