plexus-ui-engine 0.2.2

The plexus-ui engine — signals, live() over Plexus streams, the view tree over a renderer, Rhai folds, sections, hot-swap
Documentation
//! UI-6: the router.

use super::*;
use serde_json::json;

/// `#/ticket/:id` gives the prop `id`; the more specific pattern wins; an
/// empty hash is `#/`; a hash no pattern fits is none.
#[test]
fn a_route_names_its_component_and_its_segments_are_props() {
    let routes = BTreeMap::from([
        ("#/".to_string(), "page-shell".to_string()),
        ("#/ticket/:id".to_string(), "ticket-page".to_string()),
        ("#/ticket/new".to_string(), "ticket-new".to_string()),
        ("#/:epic/:id".to_string(), "epic-ticket".to_string()),
    ]);
    let r = Router::new(&routes);
    let at = |h: &str| r.resolve(h).map(|x| (x.component, Value::Object(x.props)));
    assert_eq!(at("#/ticket/T-1"), Some(("ticket-page".into(), json!({ "id": "T-1" }))));
    assert_eq!(at("#/ticket/new"), Some(("ticket-new".into(), json!({}))), "the literal pattern wins over :id");
    assert_eq!(at("#/kiln/T-1"), Some(("epic-ticket".into(), json!({ "epic": "kiln", "id": "T-1" }))));
    assert_eq!(at(""), Some(("page-shell".into(), json!({}))), "an empty hash is #/");
    assert_eq!(at("#/"), Some(("page-shell".into(), json!({}))));
    assert_eq!(at("#/a/b/c"), None);
}

/// UI-8: a `:name` segment is the text, percent-decoded (`#/f/status%20Ready`
/// → `status Ready`); a stray `%` is kept.
#[test]
fn a_segment_is_percent_decoded() {
    let routes = BTreeMap::from([("#/f/:filter".to_string(), "board-layout".to_string())]);
    let r = Router::new(&routes);
    let at = |h: &str| r.resolve(h).map(|x| Value::Object(x.props));
    assert_eq!(at("#/f/status%20Ready"), Some(json!({ "filter": "status Ready" })));
    assert_eq!(at("#/f/a%2Fb%25c"), Some(json!({ "filter": "a/b%c" })));
    assert_eq!(at("#/f/100%"), Some(json!({ "filter": "100%" })));
    assert_eq!(at("#/f/%E2%86%92"), Some(json!({ "filter": "→" })));
}

/// STUDIO-3: the service's rows over the manifest's routes — a row replaces
/// the manifest's route for its pattern or adds one (its `:name` segments
/// props as any); a row naming a component the version lacks is passed
/// over, the manifest's route standing; the most specific pattern still
/// wins across the two.
#[test]
fn a_route_row_draws_over_the_manifest() {
    let routes = BTreeMap::from([
        ("#/".to_string(), "board-layout".to_string()),
        ("#/ticket/:id".to_string(), "ticket-page".to_string()),
        ("#/gone".to_string(), "old-page".to_string()),
    ]);
    let rows = BTreeMap::from([
        ("#/".to_string(), "two-col".to_string()),
        ("#/two/:filter".to_string(), "two-col".to_string()),
        ("#/ticket/new".to_string(), "ticket-new".to_string()),
        ("#/gone".to_string(), "not-held".to_string()),
    ]);
    let held = ["board-layout", "ticket-page", "old-page", "two-col", "ticket-new"];
    let r = Router::merged(&routes, &rows, |c| held.contains(&c));
    let at = |h: &str| r.resolve(h).map(|x| (x.component, Value::Object(x.props)));
    assert_eq!(at("#/"), Some(("two-col".into(), json!({}))), "a row over the manifest's own");
    assert_eq!(at("#/two/open"), Some(("two-col".into(), json!({ "filter": "open" }))), "a new pattern, its segment a prop");
    assert_eq!(at("#/ticket/new"), Some(("ticket-new".into(), json!({}))), "a row's literal over the manifest's :id");
    assert_eq!(at("#/ticket/T-1"), Some(("ticket-page".into(), json!({ "id": "T-1" }))), "the manifest's own, untouched");
    assert_eq!(at("#/gone"), Some(("old-page".into(), json!({}))), "a row whose component is not held is passed over");
    let none = Router::merged(&routes, &BTreeMap::new(), |_| true);
    assert_eq!(none.resolve("#/").map(|x| x.component), Some("board-layout".into()), "no rows: the manifest's");
}