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

379 lines
8.1 KiB
Rust
Raw Normal View History

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;
2023-06-01 18:13:50 +00:00
fn prepare<R: Routable>() -> String
2023-05-31 18:57:33 +00:00
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
}
}
#[component]
2023-06-01 18:13:50 +00:00
fn App<R: Routable>(cx: Scope<AppProps<R>>) -> Element
2023-05-31 18:57:33 +00:00
where
<R as FromStr>::Err: std::fmt::Display,
{
2023-04-11 20:07:00 +00:00
render! {
h1 { "App" }
2023-07-26 01:14:48 +00:00
Router::<R> {
2023-06-02 17:33:47 +00:00
config: || RouterConfig::default().history(MemoryHistory::default())
2023-05-31 18:57:33 +00:00
}
2023-04-11 20:07:00 +00:00
}
}
}
#[test]
fn href_internal() {
2023-06-01 18:13:50 +00:00
#[derive(Routable, Clone)]
2023-05-31 18:57:33 +00:00
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
2023-04-11 20:07:00 +00:00
}
#[component]
fn Test(_cx: Scope) -> Element {
2023-05-31 18:57:33 +00:00
todo!()
}
2023-04-11 20:07:00 +00:00
#[component]
2023-05-31 18:57:33 +00:00
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-07-24 18:57:56 +00:00
to: 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-06-01 18:13:50 +00:00
#[derive(Routable, Clone)]
2023-05-31 18:57:33 +00:00
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[component]
fn Test(_cx: Scope) -> Element {
2023-05-31 18:57:33 +00:00
todo!()
}
#[component]
2023-05-31 18:57:33 +00:00
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-07-26 01:14:48 +00:00
to: "https://dioxuslabs.com/",
2023-04-11 20:07:00 +00:00
"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-06-01 18:13:50 +00:00
#[derive(Routable, Clone)]
2023-05-31 18:57:33 +00:00
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[component]
fn Test(_cx: Scope) -> Element {
2023-05-31 18:57:33 +00:00
todo!()
}
#[component]
2023-05-31 18:57:33 +00:00
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-07-24 18:57:56 +00:00
to: 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-06-01 18:13:50 +00:00
#[derive(Routable, Clone)]
2023-05-31 18:57:33 +00:00
enum Route {
#[route("/")]
Root {},
}
#[component]
2023-05-31 18:57:33 +00:00
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-07-24 18:57:56 +00:00
to: Route::Root {},
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="/""#,
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-06-01 18:13:50 +00:00
#[derive(Routable, Clone)]
2023-05-31 18:57:33 +00:00
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[component]
fn Test(_cx: Scope) -> Element {
2023-05-31 18:57:33 +00:00
todo!()
}
#[component]
2023-05-31 18:57:33 +00:00
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-07-24 18:57:56 +00:00
to: 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-06-01 18:13:50 +00:00
#[derive(Routable, Clone)]
2023-05-31 18:57:33 +00:00
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[component]
fn Test(_cx: Scope) -> Element {
2023-05-31 18:57:33 +00:00
todo!()
}
#[component]
2023-05-31 18:57:33 +00:00
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-07-24 18:57:56 +00:00
to: 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-06-01 18:13:50 +00:00
#[derive(Routable, Clone)]
2023-05-31 18:57:33 +00:00
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[component]
fn Test(_cx: Scope) -> Element {
2023-05-31 18:57:33 +00:00
todo!()
}
#[component]
2023-05-31 18:57:33 +00:00
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-07-24 18:57:56 +00:00
to: 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-06-01 18:13:50 +00:00
#[derive(Routable, Clone)]
2023-05-31 18:57:33 +00:00
enum Route {
#[route("/")]
Root {},
}
#[component]
2023-05-31 18:57:33 +00:00
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-07-26 01:14:48 +00:00
to: "https://dioxuslabs.com/",
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="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-06-01 18:13:50 +00:00
#[derive(Routable, Clone)]
2023-05-31 18:57:33 +00:00
enum Route {
#[route("/")]
Root {},
#[route("/test")]
Test {},
}
#[component]
fn Test(_cx: Scope) -> Element {
2023-05-31 18:57:33 +00:00
todo!()
}
#[component]
2023-05-31 18:57:33 +00:00
fn Root(cx: Scope) -> Element {
2023-04-11 20:07:00 +00:00
render! {
Link {
2023-07-24 18:57:56 +00:00
to: 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
}