summaryrefslogtreecommitdiffstats
path: root/src/components/choice/tests.rs
blob: 3b04bdaab9c53c12eb5f466557a82b3039765564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use rstest::rstest;

use super::*;
use crate::{assert_rendered_output, input::MetaEvent, module::testutil::handle_event_test};

#[derive(Clone, Debug, PartialEq)]
pub enum TestAction {
	A,
	B,
	C,
}

fn create_choices() -> Vec<(TestAction, char, String)> {
	vec![
		(TestAction::A, 'a', String::from("Description A")),
		(TestAction::B, 'b', String::from("Description B")),
		(TestAction::C, 'c', String::from("Description C")),
	]
}

#[test]
fn render_options_no_prompt() {
	let mut module = Choice::new(create_choices());
	assert_rendered_output!(
		module.get_view_data(),
		"{TITLE}",
		"{BODY}",
		"{Normal}a) Description A",
		"{Normal}b) Description B",
		"{Normal}c) Description C",
		"",
		"{IndicatorColor}Please choose an option."
	);
}

#[test]
fn render_options_prompt() {
	let mut module = Choice::new(create_choices());
	module.set_prompt(vec![ViewLine::from("Prompt")]);
	assert_rendered_output!(
		module.get_view_data(),
		"{TITLE}",
		"{LEADING}",
		"{Normal}Prompt",
		"",
		"{BODY}",
		"{Normal}a) Description A",
		"{Normal}b) Description B",
		"{Normal}c) Description C",
		"",
		"{IndicatorColor}Please choose an option."
	);
}

#[test]
fn valid_selection() {
	handle_event_test(&[Event::from('b')], |context| {
		let mut module = Choice::new(create_choices());
		let (choice, event) = module.handle_event(&context.event_handler, &context.view_sender);
		assert_eq!(choice.unwrap(), &TestAction::B);
		assert_eq!(event, Event::from('b'));
		assert_rendered_output!(
			module.get_view_data(),
			"{TITLE}",
			"{BODY}",
			"{Normal}a) Description A",
			"{Normal}b) Description B",
			"{Normal}c) Description C",
			"",
			"{IndicatorColor}Please choose an option."
		);
	});
}

#[test]
fn invalid_selection_character() {
	handle_event_test(&[Event::from('z')], |context| {
		let mut module = Choice::new(create_choices());
		let (choice, event) = module.handle_event(&context.event_handler, &context.view_sender);
		assert!(choice.is_none());
		assert_eq!(event, Event::from('z'));
		assert_rendered_output!(
			module.get_view_data(),
			"{TITLE}",
			"{BODY}",
			"{Normal}a) Description A",
			"{Normal}b) Description B",
			"{Normal}c) Description C",
			"",
			"{IndicatorColor}Invalid option selected. Please choose an option."
		);
	});
}

#[rstest(
	event,
	case::resize(Event::Resize(100, 100)),
	case::scroll_left(Event::from(MetaEvent::ScrollLeft)),
	case::scroll_right(Event::from(MetaEvent::ScrollRight)),
	case::scroll_down(Event::from(MetaEvent::ScrollDown)),
	case::scroll_up(Event::from(MetaEvent::ScrollUp)),
	case::scroll_jump_down(Event::from(MetaEvent::ScrollJumpDown)),
	case::scroll_jump_up(Event::from(MetaEvent::ScrollJumpUp))
)]
fn event_standard(event: Event) {
	handle_event_test(&[event], |context| {
		let mut module = Choice::new(create_choices());
		module.handle_event(&context.event_handler, &context.view_sender);
		assert!(!module.invalid_selection);
	});
}