use rstml_component::HtmlContent; use rstml_component_macro::{component, html}; #[test] fn simple() { #[component(pub MyComponent)] fn my_component(title: String) -> impl HtmlContent { html! {
{title}
} } let component = MyComponent { title: "Hello there".to_string(), }; let html = component .into_string() .expect("formatting works and produces valid utf-8"); let expected = r#"
Hello there
"#; assert_eq!(html, expected); } #[test] fn with_impl_param() { #[component(MyGenericComponent)] fn my_generic_component( title: impl Into, descriptions: (impl Into, impl Into), ) -> impl HtmlContent { html! {
{title.into()}
{descriptions.0.into()}
{descriptions.1.into()}
} } let html = MyGenericComponent { title: "Hello there", descriptions: ("desc1", "desc2"), } .into_string() .expect("formatting works and produces valid utf-8"); let expected = r#"
Hello there
desc1
desc2
"#; assert_eq!(html, expected); } #[test] fn with_where_clause() { #[component(WhereGenericComp)] #[expect(clippy::multiple_bound_locations)] fn where_generic_comp(title: T) -> impl HtmlContent where T: Into, { html! {
{title.clone().into()}
} } let html = WhereGenericComp { title: "Hello there", } .into_string() .expect("formatting works and produces valid utf-8"); let expected = r#"
Hello there
"#; assert_eq!(html, expected); } #[test] fn mixed_args() { #[component(MixedGenericComp)] fn mixed_generic_comp( title: T, description: impl Into, children: C, ) -> impl HtmlContent where T: Into, { html! {
{title.into()}
{description.into()}
{children} } } #[component(Inner)] fn inner(name: impl Into) -> impl HtmlContent { html! {
"Hello, "{name.into()}
} } let html = MixedGenericComp { title: "Hello there", description: "desc", children: Inner { name: "Test" }, } .into_string() .expect("formatting works and produces valid utf-8"); let expected = r#"
Hello there
desc
Hello, Test
"#; assert_eq!(html, expected); }