testing_library_dom/queries/
test_id.rs

1use web_sys::HtmlElement;
2
3use crate::{
4    build_queries,
5    config::get_config,
6    error::QueryError,
7    query_helpers::query_all_by_attribute,
8    types::{Matcher, MatcherOptions},
9};
10
11fn get_test_id_attribute() -> String {
12    get_config().test_id_attribute
13}
14
15pub fn _query_all_by_test_id<M: Into<Matcher>>(
16    container: &HtmlElement,
17    id: M,
18    options: MatcherOptions,
19) -> Result<Vec<HtmlElement>, QueryError> {
20    query_all_by_attribute(get_test_id_attribute(), container, id, options)
21}
22
23fn get_multiple_error(
24    _container: &HtmlElement,
25    id: Matcher,
26    _options: MatcherOptions,
27) -> Result<String, QueryError> {
28    Ok(format!(
29        "Found multiple elements by: [{}=\"{}\"]",
30        get_test_id_attribute(),
31        id
32    ))
33}
34
35fn get_missing_error(
36    _container: &HtmlElement,
37    id: Matcher,
38    _options: MatcherOptions,
39) -> Result<String, QueryError> {
40    Ok(format!(
41        "Unable to find an element by: [{}=\"{}\"]",
42        get_test_id_attribute(),
43        id
44    ))
45}
46
47build_queries!(
48    _query_all_by_test_id,
49    get_multiple_error,
50    get_missing_error,
51    test_id,
52    crate::types::Matcher,
53    crate::types::MatcherOptions
54);
55
56pub use internal::{
57    find_all_by_test_id, find_by_test_id, get_all_by_test_id, get_by_test_id, query_all_by_test_id,
58    query_by_test_id,
59};