use pagetop::prelude::*; async fn render(mut icon: Icon) -> String { icon.render(&mut Context::default()).await.into_string() } #[pagetop::test] async fn icon_without_source_renders_nothing() { assert!(render(Icon::new()).await.is_empty()); assert!( render(Icon::with(icon::Core::Check).with_source(None)) .await .is_empty() ); } #[pagetop::test] async fn svg_icon_renders_an_inline_svg_scaled_to_the_font() { let html = render(Icon::with(icon::Core::Check)).await; assert!(html.starts_with("")); } #[pagetop::test] async fn icon_without_label_is_decorative() { for icon in [ Icon::with(icon::Core::Close), Icon::with(icon::Source::font("my-icons-star")), ] { let html = render(icon).await; assert!(html.contains(r#"aria-hidden="true""#)); assert!(!html.contains("role=")); assert!(!html.contains("aria-label")); } } #[pagetop::test] async fn icon_with_a_blank_label_is_decorative() { let html = render(Icon::with(icon::Core::Close).with_label(Lc::n(" "))).await; assert!(html.contains(r#"aria-hidden="true""#), "{html}"); assert!(!html.contains("role="), "{html}"); assert!(!html.contains("aria-label"), "{html}"); } #[pagetop::test] async fn icon_with_label_is_announced_as_an_image() { for icon in [ Icon::with(icon::Core::Close), Icon::with(icon::Source::font("my-icons-star")), ] { let html = render(icon.with_label(Lc::n("Close"))).await; assert!(html.contains(r#"role="img""#)); assert!(html.contains(r#"aria-label="Close""#)); assert!(!html.contains("aria-hidden")); } } #[pagetop::test] async fn icon_size_sets_the_font_size() { let sized = render(Icon::with(icon::Core::Plus).with_size(UnitValue::RelRem(2.0))).await; let unsized_icon = render(Icon::with(icon::Core::Plus)).await; assert!(sized.contains(r#"style="font-size: 2rem""#)); assert!(!unsized_icon.contains("style=")); } #[pagetop::test] async fn custom_classes_are_kept_after_the_icon_class() { let html = render(Icon::with(icon::Core::Minus).with_prop(PropsOp::add_classes("text-red"))).await; assert!(html.contains(r#"class="icon text-red""#)); } #[pagetop::test] async fn any_type_convertible_into_a_source_is_an_icon_set() { enum MyIcons { Dot, } impl From for icon::Source { fn from(icon: MyIcons) -> Self { match icon { MyIcons::Dot => { icon::Source::svg("0 0 10 10", html! { circle cx="5" cy="5" r="2" {} }) } } } } let html = render(Icon::with(MyIcons::Dot)).await; assert!(html.contains(r#"viewBox="0 0 10 10""#)); assert!(html.contains(" Add")); } #[pagetop::test] async fn icon_only_button_takes_its_name_from_the_icon_label() { let mut button = Button::plain(Lc::none()) .with_icon_start(Icon::with(icon::Core::Close).with_label(Lc::n("Close"))); let html = button.render(&mut Context::default()).await.into_string(); assert!(html.contains(r#"aria-label="Close""#)); // No separator without a label. assert!(html.contains("")); } #[pagetop::test] async fn button_renders_its_end_icon_after_the_label() { let mut button = Button::plain(Lc::n("Next")).with_icon_end(Icon::with(icon::Core::ChevronRight)); let html = button.render(&mut Context::default()).await.into_string(); assert!(html.contains("Next Add Text")); } #[pagetop::test] async fn core_icon_keeps_its_identity_until_rendered() { let icon = Icon::with(icon::Core::Gear); assert!(matches!( icon.source(), Some(icon::Source::Core(icon::Core::Gear)) )); assert!(render(icon).await.starts_with("