summaryrefslogtreecommitdiffstats
path: root/src/components/choice/tests.rs
blob: b2019e5634aa2270416a0f7f569c2f6a8c819fb2 (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
use rstest::rstest;

use super::*;
use crate::{
	assert_rendered_output,
	input::StandardEvent,
	test_helpers::{assertions::assert_rendered_output::AssertRenderOptions, with_view_state},
};

#[derive(Clone, Debug, PartialEq)]
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!(
		Style 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!(
		Style 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() {
	with_view_state(|context| {
		let mut module = Choice::new(create_choices());
		let choice = module.handle_event(Event::from('b'), &context.state);
		assert_eq!(choice.unwrap(), &TestAction::B);
		assert_rendered_output!(
			Body module.get_view_data(),
			"a) Description A",
			"b) Description B",
			"c) Description C",
			"",
			"Please choose an option."
		);
	});
}

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

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