test(hemx-test): name rendered target assertions
Add rendered target and handle assertions that include generated names in diagnostics instead of only raw runtime ids. req: test/017 req: dx/006
This commit is contained in:
+39
-6
@@ -197,15 +197,34 @@ pub fn page_nav_link_selector(href: &str) -> String {
|
||||
/// req: test/001 req: dx/006
|
||||
pub fn target_selector(target: impl GeneratedTarget) -> String {
|
||||
let resource = target.__hemx_resource_id();
|
||||
let attr = match resource.kind {
|
||||
ResourceKind::Slot => "data-sid",
|
||||
ResourceKind::Atom => "data-aid",
|
||||
ResourceKind::Handle => "data-hid",
|
||||
ResourceKind::Form => "data-fid",
|
||||
};
|
||||
let attr = runtime_resource_attr(resource.kind);
|
||||
attr_selector(attr, &resource.id.to_string())
|
||||
}
|
||||
|
||||
/// Assert rendered HTML contains a generated target and report the generated name on failure.
|
||||
/// req: test/017 req: dx/006
|
||||
pub fn assert_rendered_target(target: impl GeneratedTarget, generated_name: &str, html: &str) {
|
||||
let resource = target.__hemx_resource_id();
|
||||
let marker = runtime_attr_marker(
|
||||
runtime_resource_attr(resource.kind),
|
||||
&resource.id.to_string(),
|
||||
);
|
||||
assert!(
|
||||
html.contains(&marker),
|
||||
"rendered hemx target `{generated_name}` missing runtime marker {marker}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Assert rendered HTML contains a generated handle and report the generated name on failure.
|
||||
/// req: test/017 req: dx/006
|
||||
pub fn assert_rendered_handle<I>(handle: hemx_core::Handle<I>, generated_name: &str, html: &str) {
|
||||
let marker = runtime_attr_marker("data-hid", &handle.to_string());
|
||||
assert!(
|
||||
html.contains(&marker),
|
||||
"rendered hemx handle `{generated_name}` missing runtime marker {marker}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Build a selector for an hemx root from its authoring name.
|
||||
/// req: test/001 req: dx/006
|
||||
pub fn root_selector(name: &str) -> String {
|
||||
@@ -351,6 +370,20 @@ fn assert_simple_selector_part(value: &str, label: &str) {
|
||||
);
|
||||
}
|
||||
|
||||
fn runtime_resource_attr(kind: ResourceKind) -> &'static str {
|
||||
match kind {
|
||||
ResourceKind::Slot => "data-sid",
|
||||
ResourceKind::Atom => "data-aid",
|
||||
ResourceKind::Handle => "data-hid",
|
||||
ResourceKind::Form => "data-fid",
|
||||
}
|
||||
}
|
||||
|
||||
fn runtime_attr_marker(name: &str, value: &str) -> String {
|
||||
let escaped = value.replace('"', """);
|
||||
format!(r#"{name}="{escaped}""#)
|
||||
}
|
||||
|
||||
fn attr_selector(name: &str, value: &str) -> String {
|
||||
let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
|
||||
format!(r#"[{name}="{escaped}"]"#)
|
||||
|
||||
@@ -67,10 +67,20 @@ fn builds_authoring_boundary_selectors() {
|
||||
hemx_test::target_selector(TestTarget(ResourceKind::Slot, 42)),
|
||||
r#"[data-sid="42"]"#
|
||||
);
|
||||
hemx_test::assert_rendered_target(
|
||||
TestTarget(ResourceKind::Slot, 42),
|
||||
"gallery::search_result",
|
||||
r#"<li data-sid="42">row</li>"#,
|
||||
);
|
||||
assert_eq!(
|
||||
hemx_test::handle_button_selector(hemx_core::Handle::<()>::new(7)),
|
||||
r#"button[data-hid="7"]"#
|
||||
);
|
||||
hemx_test::assert_rendered_handle(
|
||||
hemx_core::Handle::<()>::new(7),
|
||||
"gallery::search",
|
||||
r#"<button data-hid="7">Search</button>"#,
|
||||
);
|
||||
assert_eq!(hemx_test::article_selector(), "article");
|
||||
assert_eq!(hemx_test::strong_text_selector(), "strong");
|
||||
assert_eq!(hemx_test::small_text_selector(), "small");
|
||||
|
||||
Reference in New Issue
Block a user