testing_library_dom/types/
config.rs

1use std::sync::Arc;
2
3use web_sys::Element;
4
5use crate::error::QueryError;
6
7pub type GetElementErrorFn = dyn Fn(Option<String>, Element) -> QueryError + Send + Sync;
8
9#[derive(Clone)]
10pub struct Config {
11    pub test_id_attribute: String,
12    // TODO
13    /// Default value for the `hidden` option in `by_role` queries.
14    pub default_hidden: bool,
15    /// Default value for the `ignore` option in `by_text` queries.
16    pub default_ignore: String,
17    /// Flag to show the full error stack traces for async errors.
18    pub show_original_stack_trace: bool,
19    /// Throw errors with suggestions for better queries. Opt in so off by default.
20    pub throw_suggestions: bool,
21    // Called when `get_by` queries fail.
22    pub get_element_error: Arc<GetElementErrorFn>,
23}
24
25impl Config {
26    pub fn update(&mut self, other: PartialConfig) {
27        if let Some(test_id_attribute) = other.test_id_attribute {
28            self.test_id_attribute = test_id_attribute;
29        }
30        if let Some(default_hidden) = other.default_hidden {
31            self.default_hidden = default_hidden;
32        }
33        if let Some(default_ignore) = other.default_ignore {
34            self.default_ignore = default_ignore;
35        }
36        if let Some(show_original_stack_trace) = other.show_original_stack_trace {
37            self.show_original_stack_trace = show_original_stack_trace;
38        }
39        if let Some(throw_suggestions) = other.throw_suggestions {
40            self.throw_suggestions = throw_suggestions;
41        }
42        if let Some(get_element_error) = other.get_element_error {
43            self.get_element_error = get_element_error;
44        }
45    }
46}
47
48#[derive(Clone, Default)]
49pub struct PartialConfig {
50    pub test_id_attribute: Option<String>,
51    // TODO
52    /// Default value for the `hidden` option in `by_role` queries.
53    pub default_hidden: Option<bool>,
54    /// Default value for the `ignore` option in `by_text` queries.
55    pub default_ignore: Option<String>,
56    /// Flag to show the full error stack traces for async errors.
57    pub show_original_stack_trace: Option<bool>,
58    /// Throw errors with suggestions for better queries. Opt in so off by default.
59    pub throw_suggestions: Option<bool>,
60    // Called when `get_by` queries fail.
61    pub get_element_error: Option<Arc<GetElementErrorFn>>,
62}
63
64impl PartialConfig {
65    pub fn test_id_attribute(mut self, value: String) -> Self {
66        self.test_id_attribute = Some(value);
67        self
68    }
69
70    pub fn default_hidden(mut self, value: bool) -> Self {
71        self.default_hidden = Some(value);
72        self
73    }
74
75    pub fn default_ignore(mut self, value: String) -> Self {
76        self.default_ignore = Some(value);
77        self
78    }
79
80    pub fn show_original_stack_trace(mut self, value: bool) -> Self {
81        self.show_original_stack_trace = Some(value);
82        self
83    }
84
85    pub fn throw_suggestions(mut self, value: bool) -> Self {
86        self.throw_suggestions = Some(value);
87        self
88    }
89
90    pub fn get_element_error(mut self, value: Arc<GetElementErrorFn>) -> Self {
91        self.get_element_error = Some(value);
92        self
93    }
94}
95
96impl From<&Config> for PartialConfig {
97    fn from(value: &Config) -> Self {
98        Self {
99            test_id_attribute: Some(value.test_id_attribute.clone()),
100            default_hidden: Some(value.default_hidden),
101            default_ignore: Some(value.default_ignore.clone()),
102            show_original_stack_trace: Some(value.show_original_stack_trace),
103            throw_suggestions: Some(value.throw_suggestions),
104            get_element_error: Some(value.get_element_error.clone()),
105        }
106    }
107}
108
109pub type ConfigFn = dyn Fn(&Config) -> PartialConfig;
110
111pub enum ConfigFnOrPartial {
112    Fn(Box<ConfigFn>),
113    Partial(PartialConfig),
114}