Skip to content

Commit 3db6eac

Browse files
committed
add editor demo kittest tests
1 parent c4d0afb commit 3db6eac

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ serde_json = { workspace = true }
1919
log = "0.4"
2020
wasm-bindgen-futures = "0.4"
2121
web-sys = "0.3.70" # to access the DOM (to hide the loading text)
22+
23+
[dev-dependencies]
24+
egui_kittest = { workspace = true }

demo/src/apps/editor.rs

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,174 @@ impl Show for JsonEditorExample {
468468
self.editor.apply_events(&mut self.value);
469469
}
470470
}
471+
472+
#[cfg(test)]
473+
mod tests {
474+
use egui::accesskit::Role;
475+
use egui_kittest::{Harness, kittest::Queryable};
476+
use serde_json::json;
477+
478+
use crate::apps::{Show, editor::JsonEditorExample};
479+
480+
#[test]
481+
fn add_to_array() {
482+
let mut app = JsonEditorExample::new(json!({ "abc": 123, "def": [5, 6, 7] }));
483+
let mut harness = Harness::new_ui(|ui| {
484+
app.show(ui);
485+
});
486+
487+
let array_node = harness.query_by_label_contains("def").unwrap();
488+
array_node.click_secondary();
489+
harness.run();
490+
491+
harness
492+
.get_by_role_and_label(Role::Button, "Add to array")
493+
.click();
494+
harness.run();
495+
496+
drop(harness);
497+
498+
assert_eq!(app.value, json!({ "abc": 123, "def": [5, 6, 7, null] }))
499+
}
500+
501+
#[test]
502+
fn delete_from_array() {
503+
let mut app = JsonEditorExample::new(json!({ "abc": 123, "def": [5, 6, 7] }));
504+
let mut harness = Harness::new_ui(|ui| {
505+
app.show(ui);
506+
});
507+
508+
let array_element_to_delete = harness.query_by_label_contains("6").unwrap();
509+
array_element_to_delete.click_secondary();
510+
harness.run();
511+
512+
harness
513+
.get_by_role_and_label(Role::Button, "Delete")
514+
.click();
515+
harness.run();
516+
517+
drop(harness);
518+
519+
assert_eq!(app.value, json!({ "abc": 123, "def": [5, 7] }))
520+
}
521+
522+
#[test]
523+
fn edit_array_element() {
524+
let mut app = JsonEditorExample::new(json!({ "abc": 123, "def": [5, 6, 7] }));
525+
let mut harness = Harness::new_ui(|ui| {
526+
app.show(ui);
527+
});
528+
529+
let array_element_to_edit = harness.query_by_label_contains("6").unwrap();
530+
array_element_to_edit.click_secondary();
531+
harness.run();
532+
533+
harness
534+
.get_by_role_and_label(Role::Button, "Edit value")
535+
.click();
536+
harness.run();
537+
538+
assert!(harness.get_by_role(Role::TextInput).is_focused());
539+
540+
// Text input is focussed and we can trigger text input event via any node.
541+
harness.root().type_text("foo");
542+
harness.run();
543+
544+
harness.get_by_role_and_label(Role::Button, "✅").click();
545+
harness.run();
546+
547+
drop(harness);
548+
549+
assert_eq!(app.value, json!({ "abc": 123, "def": [5, "foo", 7] }))
550+
}
551+
552+
#[test]
553+
fn add_to_object() {
554+
let mut app = JsonEditorExample::new(json!({ "abc": { "baz": "qux" }, "def": [5, 6, 7] }));
555+
let mut harness = Harness::new_ui(|ui| {
556+
app.show(ui);
557+
});
558+
559+
let object_node = harness.get_by_label_contains("abc");
560+
object_node.click_secondary();
561+
harness.run();
562+
563+
harness
564+
.get_by_role_and_label(Role::Button, "Add to object")
565+
.click();
566+
harness.run();
567+
568+
assert!(harness.get_by_role(Role::TextInput).is_focused());
569+
570+
// Text input is focussed and we can trigger text input event via any node.
571+
harness.root().type_text("foo");
572+
harness.run();
573+
574+
harness.get_by_role_and_label(Role::Button, "✅").click();
575+
harness.run();
576+
577+
drop(harness);
578+
579+
assert_eq!(
580+
app.value,
581+
json!({ "abc": { "baz": "qux", "foo": null }, "def": [5, 6, 7] })
582+
)
583+
}
584+
585+
#[test]
586+
fn edit_object_key() {
587+
let mut app = JsonEditorExample::new(json!({ "abc": { "baz": "qux" }, "def": [5, 6, 7] }));
588+
let mut harness = Harness::new_ui(|ui| {
589+
app.show(ui);
590+
});
591+
592+
let object_node = harness.get_by_label_contains("abc");
593+
object_node.click_secondary();
594+
harness.run();
595+
596+
harness
597+
.get_by_role_and_label(Role::Button, "Edit key")
598+
.click();
599+
harness.run();
600+
601+
assert!(harness.get_by_role(Role::TextInput).is_focused());
602+
603+
// Text input is focussed and we can trigger text input event via any node.
604+
harness.root().type_text("foo");
605+
harness.run();
606+
607+
harness.get_by_role_and_label(Role::Button, "✅").click();
608+
harness.run();
609+
610+
drop(harness);
611+
612+
assert_eq!(
613+
app.value,
614+
json!({ "foo": { "baz": "qux" }, "def": [5, 6, 7] })
615+
)
616+
}
617+
618+
#[test]
619+
fn delete_from_object() {
620+
let mut app = JsonEditorExample::new(json!({ "abc": { "baz": "qux" }, "def": [5, 6, 7] }));
621+
let mut harness = Harness::new_ui(|ui| {
622+
app.show(ui);
623+
});
624+
625+
let element_to_delete = harness.get_by_label_contains("qux");
626+
element_to_delete.click_secondary();
627+
harness.run();
628+
629+
harness
630+
.get_by_role_and_label(Role::Button, "Delete")
631+
.click();
632+
harness.run();
633+
634+
drop(harness);
635+
636+
assert_eq!(app.value, json!({ "abc": {}, "def": [5, 6, 7] }))
637+
}
638+
}
639+
640+
// let nodes: Vec<_> = harness.query_all(By::new()).collect();
641+
// println!("{:#?}", nodes);

0 commit comments

Comments
 (0)