mod support;
use std::cell::RefCell;
use std::rc::Rc;
use dioxus::prelude::*;
use lumen_dioxus::prelude::*;
use support::{attribute, opening_tags, render};
fn render_change<T: 'static>(
app: fn(Slot<T>) -> Element,
change: impl FnOnce(Signal<T>),
) -> (String, String) {
let slot = Slot(Rc::new(RefCell::new(None)));
let mut dom = VirtualDom::new_with_props(app, slot.clone());
dom.rebuild_in_place();
let before = dioxus_ssr::render(&dom);
let signal = slot.0.borrow().expect("the app stores its signal");
dom.in_runtime(|| change(signal));
dom.render_immediate(&mut dioxus::dioxus_core::NoOpMutations);
let after = dioxus_ssr::render(&dom);
(before, after)
}
struct Slot<T: 'static>(Rc<RefCell<Option<Signal<T>>>>);
impl<T> Clone for Slot<T> {
fn clone(&self) -> Self {
Self(Rc::clone(&self.0))
}
}
fn store<T>(slot: Slot<T>, signal: Signal<T>) {
let Slot(cell) = slot;
cell.borrow_mut().replace(signal);
}
fn hidden_contents(html: &str) -> Vec<bool> {
opening_tags(html, "div")
.iter()
.filter(|attributes| {
attribute(attributes, "data-slot").as_deref() == Some("collapsible-content")
})
.map(|attributes| attribute(attributes, "hidden").is_some())
.collect()
}
fn open_details(html: &str) -> Vec<bool> {
opening_tags(html, "details")
.iter()
.map(|attributes| attribute(attributes, "open").is_some())
.collect()
}
#[test]
fn collapsible_parts_hide_closed_content_and_focus_on_native() {
let html = render(rsx! {
Collapsible {
CollapsibleTrigger { "Advanced" }
CollapsibleContent { p { "Body" } }
}
});
let summary = &opening_tags(&html, "summary")[0];
assert_eq!(attribute(summary, "tabindex").as_deref(), Some("0"));
assert_eq!(open_details(&html), vec![false]);
assert_eq!(hidden_contents(&html), vec![true]);
let open = render(rsx! {
Collapsible { default_open: true,
CollapsibleTrigger { "Advanced" }
CollapsibleContent { class: "body", p { "Body" } }
}
});
assert_eq!(open_details(&open), vec![true]);
assert_eq!(hidden_contents(&open), vec![false]);
}
fn controlled_collapsible(slot: Slot<bool>) -> Element {
let open = use_signal(|| false);
store(slot, open);
rsx! {
Collapsible { open: open(),
CollapsibleTrigger { "Advanced" }
CollapsibleContent { "Body" }
}
}
}
#[test]
fn controlled_collapsible_follows_its_prop() {
let (before, after) = render_change(controlled_collapsible, |mut open| open.set(true));
assert_eq!(
(open_details(&before), hidden_contents(&before)),
(vec![false], vec![true])
);
assert_eq!(
(open_details(&after), hidden_contents(&after)),
(vec![true], vec![false])
);
}
fn controlled_accordion(slot: Slot<Vec<String>>) -> Element {
let value = use_signal(|| vec!["billing".to_string()]);
store(slot, value);
rsx! {
Accordion { r#type: AccordionType::Single, value: value(),
AccordionItem { value: "billing",
AccordionTrigger { "Billing" }
AccordionContent { "Invoices" }
}
AccordionItem { value: "team",
AccordionTrigger { "Team" }
AccordionContent { "Members" }
}
Collapsible { value: "refunds",
CollapsibleTrigger { "Refunds" }
CollapsibleContent { "Within 30 days" }
}
}
}
}
#[test]
fn accordion_items_follow_the_accordion_value() {
let (before, after) = render_change(controlled_accordion, |mut value| {
value.set(vec!["team".to_string(), "refunds".to_string()]);
});
assert_eq!(open_details(&before), vec![true, false, false]);
assert_eq!(hidden_contents(&before), vec![false, true, true]);
assert_eq!(open_details(&after), vec![false, true, true]);
assert_eq!(hidden_contents(&after), vec![true, false, false]);
}
#[test]
fn accordion_items_render_plain_details_unless_they_have_a_class() {
let html = render(rsx! {
Accordion { default_value: vec!["a".to_string()],
AccordionItem { value: "a", AccordionTrigger { "A" } AccordionContent { "One" } }
AccordionItem { class: "ui-collapsible", value: "b", AccordionTrigger { "B" } AccordionContent { "Two" } }
}
});
let details = opening_tags(&html, "details");
assert_eq!(attribute(&details[0], "class"), None);
assert_eq!(
attribute(&details[1], "class").as_deref(),
Some("ui-collapsible")
);
}
#[test]
fn carousel_parts_render_the_runtime_contract() {
let html = render(rsx! {
Carousel { aria_label: "Templates",
CarouselViewport {
CarouselItem { "One" }
CarouselItem { class: "featured", "Two" }
}
footer {
CarouselPrevious { variant: ButtonVariant::Ghost, "Back" }
CarouselNext {}
}
}
});
let items = opening_tags(&html, "article");
let buttons = opening_tags(&html, "button");
assert_eq!(items.len(), 2);
assert!(items.iter().all(|item| attribute(item, "hidden").is_none()));
assert_eq!(attribute(&items[1], "class").as_deref(), Some("featured"));
assert_eq!(
attribute(&buttons[0], "data-ui-carousel-prev").as_deref(),
Some("true")
);
assert_eq!(
attribute(&buttons[0], "class").as_deref(),
Some("ui-button ui-button--ghost ui-button--sm")
);
assert_eq!(
attribute(&buttons[1], "data-ui-carousel-next").as_deref(),
Some("true")
);
assert!(html.contains(">Back<") && html.contains(">Next<"));
}
#[test]
fn kanban_items_and_handles_carry_the_move_contract() {
let html = render(rsx! {
KanbanBoard { aria_label: "Workflow",
KanbanColumn { value: "planned",
KanbanItem { item_id: "notes",
KanbanHandle { aria_label: "Move notes", "Move" }
}
KanbanItem { busy: true, id: "card-audit", item_id: "audit", "Audit" }
}
}
});
let items = opening_tags(&html, "article");
let handle = &opening_tags(&html, "button")[0];
assert_eq!(
attribute(&items[0], "data-ui-kanban-item").as_deref(),
Some("notes")
);
assert_eq!(attribute(&items[0], "id").as_deref(), Some("notes"));
assert_eq!(attribute(&items[0], "aria-busy"), None);
assert_eq!(attribute(&items[1], "id").as_deref(), Some("card-audit"));
assert_eq!(attribute(&items[1], "aria-busy").as_deref(), Some("true"));
assert_eq!(
attribute(handle, "data-ui-kanban-handle").as_deref(),
Some("notes")
);
assert_eq!(
attribute(handle, "aria-keyshortcuts").as_deref(),
Some("ArrowLeft ArrowRight")
);
assert_eq!(attribute(handle, "type").as_deref(), Some("button"));
assert_eq!(
attribute(handle, "class").as_deref(),
Some("ui-button ui-button--ghost ui-button--sm")
);
assert_eq!(attribute(handle, "draggable"), None);
}
#[test]
fn kanban_move_requests_use_core_rules() {
let columns: Vec<String> = ["planned", "active"]
.iter()
.map(|c| (*c).to_string())
.collect();
assert_eq!(
adjacent_kanban_column(&columns, "planned", true, false),
Some("active")
);
assert_eq!(
adjacent_kanban_column(&columns, "planned", true, true),
None
);
assert_eq!(
KanbanMoveDetail::new("a", "planned", "active", KanbanMoveInput::Keyboard, None)
.map(|detail| detail.input.as_str()),
Some("keyboard")
);
}
#[test]
fn message_scroller_enhances_only_after_mount() {
let html = render(rsx! {
MessageScroller { auto_scroll: true,
p { "Hello" }
MessageScrollerJump {}
}
});
let root = &opening_tags(&html, "div")[0];
let jump = &opening_tags(&html, "button")[0];
assert_eq!(attribute(root, "tabindex"), None);
assert_eq!(attribute(root, "data-at-end"), None);
assert_eq!(
attribute(jump, "data-ui-message-jump").as_deref(),
Some("true")
);
assert!(attribute(jump, "hidden").is_some());
assert!(html.contains("Jump to latest"));
}
#[test]
fn resizable_counts_panels_from_the_prop() {
let html = render(rsx! {
Resizable { panel_count: 4,
ResizablePanel { "A" }
ResizablePanel { "B" }
ResizablePanel { "C" }
ResizablePanel { style: "padding: 0", "D" }
}
});
let handles = opening_tags(&html, "button");
let panels: Vec<_> = opening_tags(&html, "div")
.into_iter()
.filter(|attributes| attribute(attributes, "data-ui-resizable-panel").is_some())
.collect();
assert_eq!(handles.len(), 3);
assert_eq!(
attribute(&handles[2], "aria-label").as_deref(),
Some("Resize panel 3")
);
assert_eq!(
attribute(&handles[2], "aria-valuenow").as_deref(),
Some("25")
);
assert_eq!(
attribute(&panels[3], "style").as_deref(),
Some("padding:0;--ui-resizable-size:25%")
);
}
#[component]
fn DirectionProbe() -> Element {
let direction = use_direction();
rsx! { span { "{direction.as_str()}" } }
}
#[test]
fn direction_is_shared_with_descendants() {
assert_eq!(render(rsx! { DirectionProbe {} }), "<span>ltr</span>");
assert!(
render(rsx! { Direction { dir: DirectionValue::Rtl, DirectionProbe {} } })
.contains("<span>rtl</span>")
);
assert!(
render(rsx! {
Direction { dir: DirectionValue::Rtl,
Direction { dir: DirectionValue::Auto, DirectionProbe {} }
}
})
.contains("<span>auto</span>")
);
}
#[test]
fn heroes_get_distinct_generated_title_ids() {
let html = render(rsx! {
Hero { title: rsx! { "One" } }
Hero { title: rsx! { "Two" } }
});
let sections = opening_tags(&html, "section");
let first = attribute(§ions[0], "aria-labelledby");
assert!(first.as_deref().is_some_and(|id| id.ends_with("-title")));
assert_ne!(first, attribute(§ions[1], "aria-labelledby"));
}
#[test]
fn watermark_escapes_long_adversarial_text() {
let text = "<&\"'>".repeat(20_000);
let html = render(rsx! { Watermark { content: text.clone(), "Body" } });
assert!(html.contains("--ui-watermark-image"));
assert!(!html.contains("<&"));
}