Skip to main content

thirtyfour_testing_library_ext/options/
simple.rs

1use crate::options::common::TestingLibraryOptions;
2use serde::Serialize;
3
4/// Simple options struct for testing-library queries that only need exact matching.
5///
6/// This struct consolidates the common pattern used by text, alt_text, display_value,
7/// placeholder_text, test_id, and title options.
8#[derive(Debug, Clone, Default, Serialize)]
9#[serde(rename_all = "camelCase")]
10pub struct SimpleOptions {
11    /// Whether to use exact text matching
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub exact: Option<bool>,
14}
15
16impl SimpleOptions {
17    /// Create a new instance with default values
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    /// Set the exact option
23    pub fn exact(mut self, exact: bool) -> Self {
24        self.exact = Some(exact);
25        self
26    }
27}
28
29impl TestingLibraryOptions for SimpleOptions {}
30
31// Type aliases for clarity and API compatibility
32pub type ByTextOptions = SimpleOptions;
33pub type ByAltTextOptions = SimpleOptions;
34pub type ByDisplayValueOptions = SimpleOptions;
35pub type ByPlaceholderTextOptions = SimpleOptions;
36pub type ByTestIdOptions = SimpleOptions;
37pub type ByTitleOptions = SimpleOptions;
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_simple_options_empty_serialization() {
45        let options = SimpleOptions::new();
46        let json = options.to_json_string().unwrap();
47        assert_eq!(json, "{}");
48    }
49
50    #[test]
51    fn test_simple_options_exact_true_serialization() {
52        let options = SimpleOptions::new().exact(true);
53
54        let json_value = options.to_json_value().unwrap();
55        assert_eq!(json_value["exact"], true);
56    }
57
58    #[test]
59    fn test_simple_options_exact_false_serialization() {
60        let options = SimpleOptions::new().exact(false);
61
62        let json_value = options.to_json_value().unwrap();
63        assert_eq!(json_value["exact"], false);
64    }
65
66    #[test]
67    fn test_simple_options_json_string() {
68        let options = SimpleOptions::new().exact(true);
69
70        let json_string = options.to_json_string().unwrap();
71        assert!(json_string.contains("\"exact\":true"));
72    }
73
74    // Test type aliases work correctly
75    #[test]
76    fn test_type_aliases() {
77        let text_options = ByTextOptions::new().exact(true);
78        let alt_text_options = ByAltTextOptions::new().exact(false);
79        let display_value_options = ByDisplayValueOptions::new().exact(true);
80        let placeholder_text_options = ByPlaceholderTextOptions::new().exact(false);
81        let test_id_options = ByTestIdOptions::new().exact(true);
82        let title_options = ByTitleOptions::new().exact(false);
83
84        // All should serialize correctly
85        assert!(text_options
86            .to_json_string()
87            .unwrap()
88            .contains("\"exact\":true"));
89        assert!(alt_text_options
90            .to_json_string()
91            .unwrap()
92            .contains("\"exact\":false"));
93        assert!(display_value_options
94            .to_json_string()
95            .unwrap()
96            .contains("\"exact\":true"));
97        assert!(placeholder_text_options
98            .to_json_string()
99            .unwrap()
100            .contains("\"exact\":false"));
101        assert!(test_id_options
102            .to_json_string()
103            .unwrap()
104            .contains("\"exact\":true"));
105        assert!(title_options
106            .to_json_string()
107            .unwrap()
108            .contains("\"exact\":false"));
109    }
110
111    #[test]
112    fn test_trait_implementation() {
113        let options = ByTextOptions::new().exact(true);
114
115        // Test that trait methods work
116        assert!(options.to_json_string().is_ok());
117        assert!(options.to_json_value().is_ok());
118    }
119}