123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- use dioxus::prelude::*;
- use dioxus_history::{History, MemoryHistory};
- use dioxus_router::components::HistoryProvider;
- use std::{rc::Rc, str::FromStr};
- fn prepare<R: Routable>() -> String
- where
- <R as FromStr>::Err: std::fmt::Display,
- {
- prepare_at::<R>("/")
- }
- fn prepare_at<R: Routable>(at: impl ToString) -> String
- where
- <R as FromStr>::Err: std::fmt::Display,
- {
- let mut vdom = VirtualDom::new_with_props(
- App,
- AppProps::<R> {
- at: at.to_string(),
- phantom: std::marker::PhantomData,
- },
- );
- vdom.rebuild_in_place();
- return dioxus_ssr::render(&vdom);
- #[derive(Props)]
- struct AppProps<R: Routable> {
- at: String,
- phantom: std::marker::PhantomData<R>,
- }
- impl<R: Routable> Clone for AppProps<R> {
- fn clone(&self) -> Self {
- Self {
- at: self.at.clone(),
- phantom: std::marker::PhantomData,
- }
- }
- }
- impl<R: Routable> PartialEq for AppProps<R> {
- fn eq(&self, _other: &Self) -> bool {
- false
- }
- }
- #[allow(non_snake_case)]
- fn App<R: Routable>(props: AppProps<R>) -> Element
- where
- <R as FromStr>::Err: std::fmt::Display,
- {
- rsx! {
- h1 { "App" }
- HistoryProvider {
- history: move |_| Rc::new(MemoryHistory::with_initial_path(props.at.clone())) as Rc<dyn History>,
- Router::<R> {}
- }
- }
- }
- }
- #[test]
- fn href_internal() {
- #[derive(Routable, Clone)]
- enum Route {
- #[route("/")]
- Root {},
- #[route("/test")]
- Test {},
- }
- #[component]
- fn Test() -> Element {
- unimplemented!()
- }
- #[component]
- fn Root() -> Element {
- rsx! {
- Link {
- to: Route::Test {},
- "Link"
- }
- }
- }
- let expected = format!("<h1>App</h1><a {href}>Link</a>", href = r#"href="/test""#,);
- assert_eq!(prepare::<Route>(), expected);
- }
- #[test]
- fn href_external() {
- #[derive(Routable, Clone)]
- enum Route {
- #[route("/")]
- Root {},
- #[route("/test")]
- Test {},
- }
- #[component]
- fn Test() -> Element {
- unimplemented!()
- }
- #[component]
- fn Root() -> Element {
- rsx! {
- Link {
- to: "https://dioxuslabs.com/",
- "Link"
- }
- }
- }
- let expected = format!(
- "<h1>App</h1><a {href} {rel}>Link</a>",
- href = r#"href="https://dioxuslabs.com/""#,
- rel = r#"rel="noopener noreferrer""#,
- );
- assert_eq!(prepare::<Route>(), expected);
- }
- #[test]
- fn with_class() {
- #[derive(Routable, Clone)]
- enum Route {
- #[route("/")]
- Root {},
- #[route("/test")]
- Test {},
- }
- #[component]
- fn Test() -> Element {
- unimplemented!()
- }
- #[component]
- fn Root() -> Element {
- rsx! {
- Link {
- to: Route::Test {},
- class: "test_class",
- "Link"
- }
- }
- }
- let expected = format!(
- "<h1>App</h1><a {href} {class}>Link</a>",
- href = r#"href="/test""#,
- class = r#"class="test_class""#,
- );
- assert_eq!(prepare::<Route>(), expected);
- }
- #[test]
- fn with_active_class_active() {
- #[derive(Routable, Clone)]
- enum Route {
- #[route("/")]
- Root {},
- }
- #[component]
- fn Root() -> Element {
- rsx! {
- Link {
- to: Route::Root {},
- active_class: "active_class".to_string(),
- class: "test_class",
- "Link"
- }
- }
- }
- let expected = format!(
- "<h1>App</h1><a {href} {class} {aria}>Link</a>",
- href = r#"href="/""#,
- class = r#"class="test_class active_class""#,
- aria = r#"aria-current="page""#,
- );
- assert_eq!(prepare::<Route>(), expected);
- }
- #[test]
- fn with_active_class_inactive() {
- #[derive(Routable, Clone)]
- enum Route {
- #[route("/")]
- Root {},
- #[route("/test")]
- Test {},
- }
- #[component]
- fn Test() -> Element {
- unimplemented!()
- }
- #[component]
- fn Root() -> Element {
- rsx! {
- Link {
- to: Route::Test {},
- active_class: "active_class".to_string(),
- class: "test_class",
- "Link"
- }
- }
- }
- let expected = format!(
- "<h1>App</h1><a {href} {class}>Link</a>",
- href = r#"href="/test""#,
- class = r#"class="test_class""#,
- );
- assert_eq!(prepare::<Route>(), expected);
- }
- #[test]
- fn with_id() {
- #[derive(Routable, Clone)]
- enum Route {
- #[route("/")]
- Root {},
- #[route("/test")]
- Test {},
- }
- #[component]
- fn Test() -> Element {
- unimplemented!()
- }
- #[component]
- fn Root() -> Element {
- rsx! {
- Link {
- to: Route::Test {},
- id: "test_id",
- "Link"
- }
- }
- }
- let expected = format!(
- "<h1>App</h1><a {href} {id}>Link</a>",
- href = r#"href="/test""#,
- id = r#"id="test_id""#,
- );
- assert_eq!(prepare::<Route>(), expected);
- }
- #[test]
- fn with_new_tab() {
- #[derive(Routable, Clone)]
- enum Route {
- #[route("/")]
- Root {},
- #[route("/test")]
- Test {},
- }
- #[component]
- fn Test() -> Element {
- unimplemented!()
- }
- #[component]
- fn Root() -> Element {
- rsx! {
- Link {
- to: Route::Test {},
- new_tab: true,
- "Link"
- }
- }
- }
- let expected = format!(
- "<h1>App</h1><a {href} {target}>Link</a>",
- href = r#"href="/test""#,
- target = r#"target="_blank""#
- );
- assert_eq!(prepare::<Route>(), expected);
- }
- #[test]
- fn with_new_tab_external() {
- #[derive(Routable, Clone)]
- enum Route {
- #[route("/")]
- Root {},
- }
- #[component]
- fn Root() -> Element {
- rsx! {
- Link {
- to: "https://dioxuslabs.com/",
- new_tab: true,
- "Link"
- }
- }
- }
- let expected = format!(
- "<h1>App</h1><a {href} {rel} {target}>Link</a>",
- href = r#"href="https://dioxuslabs.com/""#,
- rel = r#"rel="noopener noreferrer""#,
- target = r#"target="_blank""#
- );
- assert_eq!(prepare::<Route>(), expected);
- }
- #[test]
- fn with_rel() {
- #[derive(Routable, Clone)]
- enum Route {
- #[route("/")]
- Root {},
- #[route("/test")]
- Test {},
- }
- #[component]
- fn Test() -> Element {
- unimplemented!()
- }
- #[component]
- fn Root() -> Element {
- rsx! {
- Link {
- to: Route::Test {},
- rel: "test_rel".to_string(),
- "Link"
- }
- }
- }
- let expected = format!(
- "<h1>App</h1><a {href} {rel}>Link</a>",
- href = r#"href="/test""#,
- rel = r#"rel="test_rel""#,
- );
- assert_eq!(prepare::<Route>(), expected);
- }
- #[test]
- fn with_child_route() {
- #[derive(Routable, Clone, PartialEq, Debug)]
- enum ChildRoute {
- #[route("/")]
- ChildRoot {},
- #[route("/:not_static")]
- NotStatic { not_static: String },
- }
- #[derive(Routable, Clone, PartialEq, Debug)]
- enum Route {
- #[route("/")]
- Root {},
- #[route("/test")]
- Test {},
- #[child("/child")]
- Nested { child: ChildRoute },
- }
- #[component]
- fn Test() -> Element {
- unimplemented!()
- }
- #[component]
- fn Root() -> Element {
- rsx! {
- Link {
- to: Route::Test {},
- "Parent Link"
- }
- Link {
- to: Route::Nested { child: ChildRoute::NotStatic { not_static: "this-is-a-child-route".to_string() } },
- "Child Link"
- }
- }
- }
- #[component]
- fn ChildRoot() -> Element {
- rsx! {
- Link {
- to: Route::Test {},
- "Parent Link"
- }
- Link {
- to: ChildRoute::NotStatic { not_static: "this-is-a-child-route".to_string() },
- "Child Link 1"
- }
- Link {
- to: Route::Nested { child: ChildRoute::NotStatic { not_static: "this-is-a-child-route".to_string() } },
- "Child Link 2"
- }
- }
- }
- #[component]
- fn NotStatic(not_static: String) -> Element {
- unimplemented!()
- }
- assert_eq!(
- prepare_at::<Route>("/"),
- "<h1>App</h1><a href=\"/test\">Parent Link</a><a href=\"/child/this-is-a-child-route\">Child Link</a>"
- );
- assert_eq!(
- prepare_at::<Route>("/child"),
- "<h1>App</h1><a href=\"/test\">Parent Link</a><a href=\"/child/this-is-a-child-route\">Child Link 1</a><a href=\"/child/this-is-a-child-route\">Child Link 2</a>"
- );
- }
|