dioxus/packages/router/tests/via_ssr/outlet.rs

147 lines
3.1 KiB
Rust
Raw Normal View History

#![allow(unused)]
2023-05-31 18:57:33 +00:00
2022-12-13 17:28:36 +00:00
use dioxus::prelude::*;
2023-05-31 22:12:52 +00:00
use dioxus_router::prelude::*;
2022-12-13 17:28:36 +00:00
fn prepare(path: impl Into<String>) -> VirtualDom {
let mut vdom = VirtualDom::new_with_props(App, AppProps { path: path.into() });
let _ = vdom.rebuild();
return vdom;
2023-06-01 18:13:50 +00:00
#[derive(Routable, Clone)]
2023-05-31 18:57:33 +00:00
#[rustfmt::skip]
enum Route {
#[route("/")]
RootIndex {},
#[nest("/fixed")]
#[layout(Fixed)]
#[route("/")]
FixedIndex {},
#[route("/fixed")]
FixedFixed {},
#[end_layout]
#[end_nest]
#[nest("/:id")]
#[layout(Parameter)]
#[route("/")]
ParameterIndex { id: u8 },
#[route("/fixed")]
ParameterFixed { id: u8 },
}
2022-12-13 17:28:36 +00:00
#[derive(Debug, Props, PartialEq)]
struct AppProps {
path: String,
}
#[component]
2022-12-13 17:28:36 +00:00
fn App(cx: Scope<AppProps>) -> Element {
render! {
h1 { "App" }
2023-07-26 01:14:48 +00:00
Router::<Route> {
2023-06-01 18:13:50 +00:00
config: {
2023-06-02 17:33:47 +00:00
let path = cx.props.path.parse().unwrap();
move || RouterConfig::default().history(MemoryHistory::with_initial_path(path))
2023-06-01 18:13:50 +00:00
}
2023-05-31 18:57:33 +00:00
}
2022-12-13 17:28:36 +00:00
}
}
#[component]
2022-12-13 17:28:36 +00:00
fn RootIndex(cx: Scope) -> Element {
render! {
h2 { "Root Index" }
}
}
#[component]
2022-12-13 17:28:36 +00:00
fn Fixed(cx: Scope) -> Element {
render! {
h2 { "Fixed" }
2023-07-26 01:14:48 +00:00
Outlet::<Route> { }
2022-12-13 17:28:36 +00:00
}
}
#[component]
2022-12-13 17:28:36 +00:00
fn FixedIndex(cx: Scope) -> Element {
render! {
h3 { "Fixed - Index" }
}
}
#[component]
2022-12-13 17:28:36 +00:00
fn FixedFixed(cx: Scope) -> Element {
render! {
h3 { "Fixed - Fixed"}
}
}
#[component]
2023-05-31 18:57:33 +00:00
fn Parameter(cx: Scope, id: u8) -> Element {
2022-12-13 17:28:36 +00:00
render! {
2023-05-31 18:57:33 +00:00
h2 { "Parameter {id}" }
2023-07-26 01:14:48 +00:00
Outlet::<Route> { }
2022-12-13 17:28:36 +00:00
}
}
#[component]
2023-05-31 18:57:33 +00:00
fn ParameterIndex(cx: Scope, id: u8) -> Element {
2022-12-13 17:28:36 +00:00
render! {
h3 { "Parameter - Index" }
}
}
#[component]
2023-05-31 18:57:33 +00:00
fn ParameterFixed(cx: Scope, id: u8) -> Element {
2022-12-13 17:28:36 +00:00
render! {
h3 { "Parameter - Fixed" }
}
}
}
#[test]
fn root_index() {
let vdom = prepare("/");
let html = dioxus_ssr::render(&vdom);
assert_eq!(html, "<h1>App</h1><h2>Root Index</h2>");
}
#[test]
fn fixed() {
let vdom = prepare("/fixed");
let html = dioxus_ssr::render(&vdom);
assert_eq!(html, "<h1>App</h1><h2>Fixed</h2><h3>Fixed - Index</h3>");
}
#[test]
fn fixed_fixed() {
let vdom = prepare("/fixed/fixed");
let html = dioxus_ssr::render(&vdom);
assert_eq!(html, "<h1>App</h1><h2>Fixed</h2><h3>Fixed - Fixed</h3>");
}
#[test]
fn parameter() {
let vdom = prepare("/18");
let html = dioxus_ssr::render(&vdom);
assert_eq!(
html,
"<h1>App</h1><h2>Parameter 18</h2><h3>Parameter - Index</h3>"
);
}
#[test]
fn parameter_fixed() {
let vdom = prepare("/18/fixed");
let html = dioxus_ssr::render(&vdom);
assert_eq!(
html,
"<h1>App</h1><h2>Parameter 18</h2><h3>Parameter - Fixed</h3>"
);
}