testing_library_dom/
config.rs

1use std::sync::{Arc, LazyLock, Mutex};
2
3use crate::{
4    error::QueryError,
5    pretty_dom::pretty_dom,
6    types::{Config, ConfigFnOrPartial},
7};
8
9static CONFIG: LazyLock<Arc<Mutex<Config>>> = LazyLock::new(|| {
10    Arc::new(Mutex::new(Config {
11        test_id_attribute: "data-testid".into(),
12        default_hidden: false,
13        default_ignore: "script, style".into(),
14        show_original_stack_trace: false,
15        throw_suggestions: false,
16        get_element_error: Arc::new(|message, container| {
17            let prettified_dom = pretty_dom(Some(container.into()), None);
18
19            let default_ignore = {
20                let config = CONFIG.lock().expect("Config mutex should be acquired.");
21                config.default_ignore.clone()
22            };
23
24            QueryError::Element(
25                [
26                    message,
27                    Some(format!(
28                        "Ignored nodes: comments, {default_ignore}\n{prettified_dom}"
29                    )),
30                ]
31                .into_iter()
32                .flatten()
33                .collect::<Vec<_>>()
34                .join("\n\n"),
35            )
36        }),
37    }))
38});
39
40pub fn configure(new_config: ConfigFnOrPartial) {
41    let mut config = CONFIG.lock().expect("Config mutex should be acquired.");
42
43    let new_config = match new_config {
44        ConfigFnOrPartial::Fn(config_fn) => config_fn(&config),
45        ConfigFnOrPartial::Partial(new_config) => new_config,
46    };
47
48    config.update(new_config);
49}
50
51pub fn get_config() -> Config {
52    let config = CONFIG.lock().expect("Config mutex should be acquired.");
53
54    (*config).clone()
55}