use leptos::prelude::{
Children, ClassAttribute, CustomAttribute, ElementChild, GetUntracked, IntoView, Signal,
component, view,
};
use crate::util::TestAttr;
#[component]
pub fn Block(
#[prop(optional, into)]
classes: Option<Signal<String>>,
#[prop(optional, into)]
test_attr: Option<TestAttr>,
children: Children,
) -> impl IntoView {
let mut class_attr = String::from("block");
if let Some(extra) = classes {
let extra_val = extra.get_untracked();
if !extra_val.trim().is_empty() {
class_attr.push(' ');
class_attr.push_str(extra_val.trim());
}
}
let (data_testid, data_cy) = match &test_attr {
Some(attr) if attr.key == "data-testid" => (Some(attr.value.clone()), None),
Some(attr) if attr.key == "data-cy" => (None, Some(attr.value.clone())),
_ => (None, None),
};
view! {
<div
class=class_attr
attr:data-testid=move || data_testid.clone()
attr:data-cy=move || data_cy.clone()
>
{children()}
</div>
}
}
#[cfg(test)]
mod tests {
use super::*;
use leptos::prelude::RenderHtml;
#[test]
fn block_renders_default() {
let html = view! { <Block>"X"</Block> }.to_html();
assert!(
html.contains(r#"class="block""#),
"expected base 'block' class, got: {}",
html
);
assert!(html.contains('X'));
}
#[test]
fn block_with_extra_classes() {
let html = view! { <Block classes="my cls">"Y"</Block> }.to_html();
assert!(
html.contains(r#"class="block my cls""#),
"expected combined classes, got: {}",
html
);
assert!(html.contains('Y'));
}
#[test]
fn block_renders_test_id() {
let html = view! {
<Block test_attr=TestAttr::test_id("block-test")>"Content"</Block>
}
.to_html();
assert!(
html.contains(r#"data-testid="block-test""#),
"expected data-testid attribute; got: {}",
html
);
}
#[test]
fn block_no_test_id_when_not_provided() {
let html = view! {
<Block>"Content"</Block>
}
.to_html();
assert!(
!html.contains("data-testid") && !html.contains("data-cy"),
"expected no test attribute; got: {}",
html
);
}
#[test]
fn block_accepts_custom_test_attr_key() {
let html = view! {
<Block test_attr=TestAttr::new("data-cy", "block-cy")>"Content"</Block>
}
.to_html();
assert!(
html.contains(r#"data-cy="block-cy""#),
"expected custom data-cy attribute; got: {}",
html
);
}
}
#[cfg(all(test, target_arch = "wasm32"))]
mod wasm_tests {
}