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

383 lines
8.3 KiB
Rust
Raw Normal View History

2023-05-31 18:57:33 +00:00
#![allow(non_snake_case)]
2023-04-11 20:07:00 +00:00
use dioxus::prelude::*;
use dioxus_router::prelude::*;
2023-05-31 18:57:33 +00:00
use std::str::FromStr;
fn prepare<R: Routable>() -> String
where
<R as FromStr>::Err: std::fmt::Display,
{
let mut vdom = VirtualDom::new_with_props(
App,
AppProps::<R> {
phantom: std::marker::PhantomData,
},
);
2023-04-11 20:07:00 +00:00
let _ = vdom.rebuild();
return dioxus_ssr::render(&vdom);
#[derive(Props)]
2023-05-31 18:57:33 +00:00
struct AppProps<R: Routable> {
phantom: std::marker::PhantomData<R>,
2023-04-11 20:07:00 +00:00
}
2023-05-31 18:57:33 +00:00
impl<R: Routable> PartialEq for AppProps<R> {
2023-04-12 18:19:01 +00:00
fn eq(&self, _other: &Self) -> bool {
2023-04-11 20:07:00 +00:00
false
}
}
2023-05-31 18:57:33 +00:00
fn App<R: Routable>(cx: Scope<AppProps<R>>) -> Element
where
<R as FromStr>::Err: std::fmt::Display,
{
2023-04-11 20:07:00 +00:00
render! {
h1 { "App" }
2023-05-31 18:57:33 +00:00
GenericRouter::<R> {
config: RouterConfiguration {
history: Box::<MemoryHistory::<R>>::default(),
..Default::default()
}
}
2023-04-11 20:07:00 +00:00
}
}
}
#[test]
fn href_internal() {
2023-05-31 18:57:33 +00:00
#[derive(Routable, Clone)]
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
2023-04-11 20:07:00 +00:00
}
2023-05-31 18:57:33 +00:00
#[inline_props]
fn Test(cx: Scope) -> Element {
todo!()
}
2023-04-11 20:07:00 +00:00
2023-05-31 18:57:33 +00:00
#[inline_props]
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-05-31 18:57:33 +00:00
target: Route::Test {},
2023-04-11 20:07:00 +00:00
"Link"
}
}
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {id} {rel} {target}>Link</a>",
2023-05-31 18:57:33 +00:00
href = r#"href="/test""#,
2023-04-11 20:07:00 +00:00
default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="""#,
id = r#"id="""#,
rel = r#"rel="""#,
target = r#"target="""#
);
2023-05-31 18:57:33 +00:00
assert_eq!(prepare::<Route>(), expected);
2023-04-11 20:07:00 +00:00
}
#[test]
fn href_external() {
2023-05-31 18:57:33 +00:00
#[derive(Routable, Clone)]
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[inline_props]
fn Test(cx: Scope) -> Element {
todo!()
}
#[inline_props]
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
target: "https://dioxuslabs.com/",
"Link"
}
}
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {id} {rel} {target}>Link</a>",
href = r#"href="https://dioxuslabs.com/""#,
default = r#"dioxus-prevent-default="""#,
class = r#"class="""#,
id = r#"id="""#,
rel = r#"rel="noopener noreferrer""#,
target = r#"target="""#
);
2023-05-31 18:57:33 +00:00
assert_eq!(prepare::<Route>(), expected);
2023-04-11 20:07:00 +00:00
}
#[test]
fn with_class() {
2023-05-31 18:57:33 +00:00
#[derive(Routable, Clone)]
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[inline_props]
fn Test(cx: Scope) -> Element {
todo!()
}
#[inline_props]
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-05-31 18:57:33 +00:00
target: Route::Test {},
2023-04-11 20:07:00 +00:00
class: "test_class",
"Link"
}
}
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {id} {rel} {target}>Link</a>",
href = r#"href="/test""#,
default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="test_class""#,
id = r#"id="""#,
rel = r#"rel="""#,
target = r#"target="""#
);
2023-05-31 18:57:33 +00:00
assert_eq!(prepare::<Route>(), expected);
2023-04-11 20:07:00 +00:00
}
#[test]
fn with_active_class_active() {
2023-05-31 18:57:33 +00:00
#[derive(Routable, Clone)]
enum Route {
#[route("/")]
Root {},
}
#[inline_props]
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
target: "/",
active_class: "active_class",
class: "test_class",
"Link"
}
}
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {id} {rel} {target}>Link</a>",
href = r#"href="/""#,
default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="test_class active_class""#,
id = r#"id="""#,
rel = r#"rel="""#,
target = r#"target="""#
);
2023-05-31 18:57:33 +00:00
assert_eq!(prepare::<Route>(), expected);
2023-04-11 20:07:00 +00:00
}
#[test]
fn with_active_class_inactive() {
2023-05-31 18:57:33 +00:00
#[derive(Routable, Clone)]
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[inline_props]
fn Test(cx: Scope) -> Element {
todo!()
}
#[inline_props]
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-05-31 18:57:33 +00:00
target: Route::Test {},
2023-04-11 20:07:00 +00:00
active_class: "active_class",
class: "test_class",
"Link"
}
}
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {id} {rel} {target}>Link</a>",
href = r#"href="/test""#,
default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="test_class""#,
id = r#"id="""#,
rel = r#"rel="""#,
target = r#"target="""#
);
2023-05-31 18:57:33 +00:00
assert_eq!(prepare::<Route>(), expected);
2023-04-11 20:07:00 +00:00
}
#[test]
fn with_id() {
2023-05-31 18:57:33 +00:00
#[derive(Routable, Clone)]
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[inline_props]
fn Test(cx: Scope) -> Element {
todo!()
}
#[inline_props]
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-05-31 18:57:33 +00:00
target: Route::Test {},
2023-04-11 20:07:00 +00:00
id: "test_id",
"Link"
}
}
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {id} {rel} {target}>Link</a>",
href = r#"href="/test""#,
default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="""#,
id = r#"id="test_id""#,
rel = r#"rel="""#,
target = r#"target="""#
);
2023-05-31 18:57:33 +00:00
assert_eq!(prepare::<Route>(), expected);
2023-04-11 20:07:00 +00:00
}
#[test]
fn with_new_tab() {
2023-05-31 18:57:33 +00:00
#[derive(Routable, Clone)]
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[inline_props]
fn Test(cx: Scope) -> Element {
todo!()
}
#[inline_props]
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-05-31 18:57:33 +00:00
target: Route::Test {},
2023-04-11 20:07:00 +00:00
new_tab: true,
"Link"
}
}
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {id} {rel} {target}>Link</a>",
href = r#"href="/test""#,
default = r#"dioxus-prevent-default="""#,
class = r#"class="""#,
id = r#"id="""#,
rel = r#"rel="""#,
target = r#"target="_blank""#
);
2023-05-31 18:57:33 +00:00
assert_eq!(prepare::<Route>(), expected);
2023-04-11 20:07:00 +00:00
}
#[test]
fn with_new_tab_external() {
2023-05-31 18:57:33 +00:00
#[derive(Routable, Clone)]
enum Route {
#[route("/")]
Root {},
}
#[inline_props]
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
target: "https://dioxuslabs.com/",
new_tab: true,
"Link"
}
}
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {id} {rel} {target}>Link</a>",
href = r#"href="https://dioxuslabs.com/""#,
default = r#"dioxus-prevent-default="""#,
class = r#"class="""#,
id = r#"id="""#,
rel = r#"rel="noopener noreferrer""#,
target = r#"target="_blank""#
);
2023-05-31 18:57:33 +00:00
assert_eq!(prepare::<Route>(), expected);
2023-04-11 20:07:00 +00:00
}
#[test]
fn with_rel() {
2023-05-31 18:57:33 +00:00
#[derive(Routable, Clone)]
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[inline_props]
fn Test(cx: Scope) -> Element {
todo!()
}
#[inline_props]
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-05-31 18:57:33 +00:00
target: Route::Test {},
2023-04-11 20:07:00 +00:00
rel: "test_rel",
"Link"
}
}
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {id} {rel} {target}>Link</a>",
href = r#"href="/test""#,
default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="""#,
id = r#"id="""#,
rel = r#"rel="test_rel""#,
target = r#"target="""#
);
2023-05-31 18:57:33 +00:00
assert_eq!(prepare::<Route>(), expected);
2023-04-11 20:07:00 +00:00
}