Skip to content

Commit d6cbf1c

Browse files
feat: add more element queries tests
1 parent e96875f commit d6cbf1c

1 file changed

Lines changed: 293 additions & 1 deletion

File tree

packages/dom/tests/element_queries.rs

Lines changed: 293 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
#![cfg(target_arch = "wasm32")]
1+
// TODO: Enable `cfg`, disable `expect`.
2+
// #![cfg(target_arch = "wasm32")]
3+
#![expect(dead_code)]
24

35
mod helpers;
46

57
use std::sync::{Arc, LazyLock, Mutex};
68

79
use indoc::indoc;
10+
use regex::Regex;
811
use testing_library_dom::{
912
ConfigFnOrPartial, MatcherOptions, PartialConfig, QueryError, SelectorMatcherOptions, configure,
1013
};
1114
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
1215

16+
use crate::helpers::test_utils::document;
17+
1318
use self::helpers::test_utils::{RenderReturn, render};
1419

1520
wasm_bindgen_test_configure!(run_in_browser);
@@ -231,3 +236,290 @@ fn get_throws_a_useful_error_message() -> Result<(), QueryError> {
231236

232237
Ok(())
233238
}
239+
240+
#[wasm_bindgen_test]
241+
fn can_get_elements_by_matching_their_text_content() {
242+
let RenderReturn {
243+
container_queries, ..
244+
} = render(
245+
indoc! {"
246+
<div>
247+
<span>Currently showing</span>
248+
<span>
249+
Step
250+
1
251+
of 4
252+
</span>
253+
</div>
254+
"},
255+
None,
256+
);
257+
258+
assert!(
259+
container_queries
260+
.query_by_text("Currently showing", SelectorMatcherOptions::default())
261+
.ok()
262+
.is_some()
263+
);
264+
assert!(
265+
container_queries
266+
.query_by_text("Step 1 of 4", SelectorMatcherOptions::default())
267+
.ok()
268+
.is_some()
269+
);
270+
}
271+
272+
#[wasm_bindgen_test]
273+
fn can_get_elements_by_matching_their_text_across_adjacent_text_nodes() {
274+
let document = document();
275+
276+
let text_div = document
277+
.create_element("div")
278+
.expect("Element should be created.");
279+
let text_node_content = ["£", "24", ".", "99"];
280+
281+
for text in text_node_content {
282+
let text_node = document.create_text_node(text);
283+
text_div
284+
.append_child(&text_node)
285+
.expect("Child should be appended.");
286+
}
287+
288+
let RenderReturn {
289+
container,
290+
container_queries,
291+
..
292+
} = render("<div />", None);
293+
294+
container
295+
.append_child(&text_div)
296+
.expect("Child should be appended.");
297+
298+
assert!(
299+
container_queries
300+
.query_by_text("£24.99", SelectorMatcherOptions::default())
301+
.ok()
302+
.is_some()
303+
);
304+
}
305+
306+
#[wasm_bindgen_test]
307+
fn can_get_input_elements_with_type_submit_or_button_or_reset() {
308+
let RenderReturn {
309+
container_queries, ..
310+
} = render(
311+
indoc! {"
312+
<div>
313+
<input type=\"submit\" value=\"Send data\"/>
314+
<input type=\"reset\" value=\"Clear EVERYTHING\"/>
315+
<input type=\"button\" value=\"Push me!\"/>
316+
<input type=\"text\" value=\"user data\" />
317+
</div>
318+
"},
319+
None,
320+
);
321+
322+
assert!(
323+
container_queries
324+
.query_by_text("Send data", SelectorMatcherOptions::default())
325+
.expect("Query should succeed.")
326+
.is_some()
327+
);
328+
assert!(
329+
container_queries
330+
.query_by_text("Clear EVERYTHING", SelectorMatcherOptions::default())
331+
.expect("Query should succeed.")
332+
.is_some()
333+
);
334+
assert!(
335+
container_queries
336+
.query_by_text("Push me!", SelectorMatcherOptions::default())
337+
.expect("Query should succeed.")
338+
.is_some()
339+
);
340+
assert!(
341+
container_queries
342+
.query_by_text("user data", SelectorMatcherOptions::default())
343+
.expect("Query should succeed.")
344+
.is_none()
345+
);
346+
}
347+
348+
#[wasm_bindgen_test]
349+
fn matches_case_with_regexp_matcher() {
350+
let RenderReturn {
351+
container_queries, ..
352+
} = render("<span>STEP 1 of 4</span>", None);
353+
354+
assert!(
355+
container_queries
356+
.query_by_text(
357+
Regex::new("STEP 1 of 4").expect("Regex should be valid."),
358+
SelectorMatcherOptions::default()
359+
)
360+
.expect("Query should succeed.")
361+
.is_some()
362+
);
363+
assert!(
364+
container_queries
365+
.query_by_text(
366+
Regex::new("Step 1 of 4").expect("Regex should be valid."),
367+
SelectorMatcherOptions::default()
368+
)
369+
.expect("Query should succeed.")
370+
.is_none()
371+
);
372+
}
373+
374+
#[wasm_bindgen_test]
375+
fn query_by_text_matches_case_with_non_string_matcher() {
376+
let RenderReturn {
377+
container_queries, ..
378+
} = render("<span>1</span>", None);
379+
380+
assert!(
381+
container_queries
382+
.query_by_text(1_f64, SelectorMatcherOptions::default())
383+
.expect("Query should succeed.")
384+
.is_some()
385+
);
386+
}
387+
388+
#[ignore = "TODO: Fix failing test."]
389+
#[wasm_bindgen_test]
390+
fn can_get_form_controls_by_label_text() {
391+
let RenderReturn {
392+
container_queries, ..
393+
} = render(
394+
indoc! {"
395+
<div>
396+
<label>
397+
1st<input id=\"first-id\" />
398+
</label>
399+
<div>
400+
<label for=\"second-id\">2nd</label>
401+
<input id=\"second-id\" />
402+
</div>
403+
<div>
404+
<label id=\"third-label\">3rd</label>
405+
<input aria-labelledby=\"third-label\" id=\"third-id\" />
406+
</div>
407+
<div>
408+
<label for=\"fourth.id\">4th</label>
409+
<input id=\"fourth.id\" />
410+
</div>
411+
<div>
412+
<div>
413+
<label id=\"fifth-label-one\">5th one</label>
414+
<label id=\"fifth-label-two\">5th two</label>
415+
<input aria-labelledby=\"fifth-label-one fifth-label-two\" id=\"fifth-id\" />
416+
</div>
417+
<div>
418+
<input id=\"sixth-label-one\" value=\"6th one\"/>
419+
<input id=\"sixth-label-two\" value=\"6th two\"/>
420+
<label id=\"sixth-label-three\">6th three</label>
421+
<input aria-labelledby=\"sixth-label-one sixth-label-two sixth-label-three\" id=\"sixth-id\" />
422+
</div>
423+
<div>
424+
<span id=\"seventh-label-one\">7th one</span>
425+
<input aria-labelledby=\"seventh-label-one\" id=\"seventh-id\" />
426+
</div>
427+
<div>
428+
<label id=\"eighth.label\">8th one</label>
429+
<input aria-labelledby=\"eighth.label\" id=\"eighth.id\" />
430+
</div>
431+
</div>
432+
"},
433+
None,
434+
);
435+
436+
assert_eq!(
437+
container_queries
438+
.get_by_label_text("1st", SelectorMatcherOptions::default())
439+
.expect("Get should succeed.")
440+
.map(|element| element.id()),
441+
Some("first-id".to_owned())
442+
);
443+
assert_eq!(
444+
container_queries
445+
.get_by_label_text("2nd", SelectorMatcherOptions::default())
446+
.expect("Get should succeed.")
447+
.map(|element| element.id()),
448+
Some("second-id".to_owned())
449+
);
450+
assert_eq!(
451+
container_queries
452+
.get_by_label_text("3rd", SelectorMatcherOptions::default())
453+
.expect("Get should succeed.")
454+
.map(|element| element.id()),
455+
Some("third-id".to_owned())
456+
);
457+
assert_eq!(
458+
container_queries
459+
.get_by_label_text("4th", SelectorMatcherOptions::default())
460+
.expect("Get should succeed.")
461+
.map(|element| element.id()),
462+
Some("fourth-id".to_owned())
463+
);
464+
assert_eq!(
465+
container_queries
466+
.get_by_label_text("5th one", SelectorMatcherOptions::default())
467+
.expect("Get should succeed.")
468+
.map(|element| element.id()),
469+
Some("fifth-id".to_owned())
470+
);
471+
assert_eq!(
472+
container_queries
473+
.get_by_label_text("5th two", SelectorMatcherOptions::default())
474+
.expect("Get should succeed.")
475+
.map(|element| element.id()),
476+
Some("fifth-id".to_owned())
477+
);
478+
assert_eq!(
479+
container_queries
480+
.get_by_label_text("6th one", SelectorMatcherOptions::default())
481+
.expect("Get should succeed.")
482+
.map(|element| element.id()),
483+
Some("sixth-id".to_owned())
484+
);
485+
assert_eq!(
486+
container_queries
487+
.get_by_label_text("6th two", SelectorMatcherOptions::default())
488+
.expect("Get should succeed.")
489+
.map(|element| element.id()),
490+
Some("sixth-id".to_owned())
491+
);
492+
assert_eq!(
493+
container_queries
494+
.get_by_label_text("6th one 6th two", SelectorMatcherOptions::default())
495+
.expect("Get should succeed.")
496+
.map(|element| element.id()),
497+
Some("sixth-id".to_owned())
498+
);
499+
assert_eq!(
500+
container_queries
501+
.get_by_label_text(
502+
"6th one 6th two 6th three",
503+
SelectorMatcherOptions::default()
504+
)
505+
.expect("Get should succeed.")
506+
.map(|element| element.id()),
507+
Some("sixth-id".to_owned())
508+
);
509+
assert_eq!(
510+
container_queries
511+
.get_by_label_text("7th one", SelectorMatcherOptions::default())
512+
.expect("Get should succeed.")
513+
.map(|element| element.id()),
514+
Some("seventh-id".to_owned())
515+
);
516+
assert_eq!(
517+
container_queries
518+
.get_by_label_text("8th one", SelectorMatcherOptions::default())
519+
.expect("Get should succeed.")
520+
.map(|element| element.id()),
521+
Some("eighth.id".to_owned())
522+
);
523+
}
524+
525+
// TODO: More tests.

0 commit comments

Comments
 (0)