mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 06:44:17 +00:00
chore: update tests and doctests
This commit is contained in:
parent
8beb988ecc
commit
84ac64284c
6 changed files with 89 additions and 181 deletions
|
@ -292,7 +292,7 @@ pub fn Route<Segments, View>(
|
|||
where
|
||||
View: ChooseView<Dom>,
|
||||
{
|
||||
NestedRoute::new(path, view, ssr)
|
||||
NestedRoute::new(path, view).ssr_mode(ssr)
|
||||
}
|
||||
|
||||
#[component]
|
||||
|
@ -306,7 +306,7 @@ where
|
|||
View: ChooseView<Dom>,
|
||||
{
|
||||
let children = children.into_inner();
|
||||
NestedRoute::new(path, view, ssr).child(children)
|
||||
NestedRoute::new(path, view).ssr_mode(ssr).child(children)
|
||||
}
|
||||
|
||||
#[component]
|
||||
|
@ -343,7 +343,7 @@ where
|
|||
})
|
||||
.into_any()
|
||||
};
|
||||
NestedRoute::new(path, view, ssr)
|
||||
NestedRoute::new(path, view).ssr_mode(ssr)
|
||||
}
|
||||
|
||||
#[component]
|
||||
|
@ -388,7 +388,7 @@ where
|
|||
})
|
||||
.into_any()
|
||||
};
|
||||
NestedRoute::new(path, view, ssr).child(children)
|
||||
NestedRoute::new(path, view).ssr_mode(ssr).child(children)
|
||||
}
|
||||
|
||||
/// Redirects the user to a new URL, whether on the client side or on the server
|
||||
|
|
|
@ -246,13 +246,10 @@ pub(crate) fn use_resolved_path(
|
|||
/// server rendering.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use leptos::{request_animation_frame, create_runtime};
|
||||
/// # let runtime = create_runtime();
|
||||
/// # if false { // can't actually navigate, no <Router/>
|
||||
/// let navigate = leptos_router::use_navigate();
|
||||
/// let navigate = leptos_router::hooks::use_navigate();
|
||||
/// navigate("/", Default::default());
|
||||
/// # }
|
||||
/// # runtime.dispose();
|
||||
/// ```
|
||||
#[track_caller]
|
||||
pub fn use_navigate() -> impl Fn(&str, NavigateOptions) + Clone {
|
||||
|
|
|
@ -54,19 +54,21 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
pub fn should_parse_url_without_origin() {
|
||||
let url = RequestUrl::parse("/foo/bar").unwrap();
|
||||
let url = RequestUrl::new("/foo/bar").parse().unwrap();
|
||||
assert_eq!(url.path(), "/foo/bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn should_not_parse_url_without_slash() {
|
||||
let url = RequestUrl::parse("foo/bar").unwrap();
|
||||
let url = RequestUrl::new("foo/bar").parse().unwrap();
|
||||
assert_eq!(url.path(), "/foo/bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn should_parse_with_base() {
|
||||
let url = RequestUrl::parse("https://www.example.com/foo/bar").unwrap();
|
||||
let url = RequestUrl::new("https://www.example.com/foo/bar")
|
||||
.parse()
|
||||
.unwrap();
|
||||
assert_eq!(url.origin(), "https://www.example.com");
|
||||
assert_eq!(url.path(), "/foo/bar");
|
||||
}
|
||||
|
|
|
@ -102,7 +102,7 @@ mod tests {
|
|||
assert_eq!(matched.matched(), "/foo");
|
||||
assert_eq!(matched.remaining(), "");
|
||||
let params = matched.params().collect::<Vec<_>>();
|
||||
assert_eq!(params[0], ("a", "foo"));
|
||||
assert_eq!(params[0], ("a".into(), "foo".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -113,7 +113,7 @@ mod tests {
|
|||
assert_eq!(matched.matched(), "/foo");
|
||||
assert_eq!(matched.remaining(), "/");
|
||||
let params = matched.params().collect::<Vec<_>>();
|
||||
assert_eq!(params[0], ("a", "foo"));
|
||||
assert_eq!(params[0], ("a".into(), "foo".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -124,8 +124,8 @@ mod tests {
|
|||
assert_eq!(matched.matched(), "/foo/bar");
|
||||
assert_eq!(matched.remaining(), "");
|
||||
let params = matched.params().collect::<Vec<_>>();
|
||||
assert_eq!(params[0], ("a", "foo"));
|
||||
assert_eq!(params[1], ("b", "bar"));
|
||||
assert_eq!(params[0], ("a".into(), "foo".into()));
|
||||
assert_eq!(params[1], ("b".into(), "bar".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -140,6 +140,6 @@ mod tests {
|
|||
assert_eq!(matched.matched(), "/foo/bar/////");
|
||||
assert_eq!(matched.remaining(), "");
|
||||
let params = matched.params().collect::<Vec<_>>();
|
||||
assert_eq!(params[0], ("rest", "////"));
|
||||
assert_eq!(params[0], ("rest".into(), "////".into()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -141,7 +141,7 @@ where
|
|||
) -> impl IntoIterator<Item = GeneratedRouteData> + '_;
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
#[derive(Default, Debug, PartialEq)]
|
||||
pub struct GeneratedRouteData {
|
||||
pub segments: Vec<PathSegment>,
|
||||
pub ssr_mode: SsrMode,
|
||||
|
@ -150,49 +150,40 @@ pub struct GeneratedRouteData {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{NestedRoute, ParamSegment, Routes};
|
||||
use crate::{MatchInterface, PathSegment, StaticSegment, WildcardSegment};
|
||||
use std::marker::PhantomData;
|
||||
use crate::{
|
||||
matching::MatchParams, MatchInterface, PathSegment, StaticSegment,
|
||||
WildcardSegment,
|
||||
};
|
||||
use tachys::renderer::dom::Dom;
|
||||
|
||||
#[test]
|
||||
pub fn matches_single_root_route() {
|
||||
let routes = Routes::<_, Dom>::new(NestedRoute {
|
||||
segments: StaticSegment("/"),
|
||||
children: (),
|
||||
data: (),
|
||||
view: |_| (),
|
||||
rndr: PhantomData,
|
||||
});
|
||||
let routes =
|
||||
Routes::<_, Dom>::new(NestedRoute::new(StaticSegment("/"), || ()));
|
||||
let matched = routes.match_route("/");
|
||||
assert!(matched.is_some());
|
||||
let matched = routes.match_route("");
|
||||
assert!(matched.is_some());
|
||||
let (base, paths) = routes.generate_routes();
|
||||
assert_eq!(base, None);
|
||||
let paths = paths.into_iter().collect::<Vec<_>>();
|
||||
let paths = paths.into_iter().map(|g| g.segments).collect::<Vec<_>>();
|
||||
assert_eq!(paths, vec![vec![PathSegment::Static("/".into())]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn matches_nested_route() {
|
||||
let routes = Routes::new(NestedRoute {
|
||||
segments: StaticSegment(""),
|
||||
children: NestedRoute {
|
||||
segments: (StaticSegment("author"), StaticSegment("contact")),
|
||||
children: (),
|
||||
data: (),
|
||||
view: |_| "Contact Me",
|
||||
rndr: PhantomData,
|
||||
},
|
||||
data: (),
|
||||
view: |_| "Home",
|
||||
rndr: PhantomData,
|
||||
});
|
||||
let routes: Routes<_, Dom> =
|
||||
Routes::new(NestedRoute::new(StaticSegment(""), || "Home").child(
|
||||
NestedRoute::new(
|
||||
(StaticSegment("author"), StaticSegment("contact")),
|
||||
|| "Contact Me",
|
||||
),
|
||||
));
|
||||
|
||||
// route generation
|
||||
let (base, paths) = routes.generate_routes();
|
||||
assert_eq!(base, None);
|
||||
let paths = paths.into_iter().collect::<Vec<_>>();
|
||||
let paths = paths.into_iter().map(|g| g.segments).collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
paths,
|
||||
vec![vec![
|
||||
|
@ -203,86 +194,47 @@ mod tests {
|
|||
);
|
||||
|
||||
let matched = routes.match_route("/author/contact").unwrap();
|
||||
assert_eq!(matched.matched(), "");
|
||||
assert_eq!(matched.to_child().unwrap().matched(), "/author/contact");
|
||||
|
||||
let view = matched.to_view();
|
||||
assert_eq!(*view, "Home");
|
||||
assert_eq!(*matched.to_child().unwrap().to_view(), "Contact Me");
|
||||
assert_eq!(MatchInterface::<Dom>::as_matched(&matched), "");
|
||||
let (_, child) = MatchInterface::<Dom>::into_view_and_child(matched);
|
||||
assert_eq!(
|
||||
MatchInterface::<Dom>::as_matched(&child.unwrap()),
|
||||
"/author/contact"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn does_not_match_incomplete_route() {
|
||||
let routes = Routes::new(NestedRoute {
|
||||
segments: StaticSegment(""),
|
||||
children: NestedRoute {
|
||||
segments: (StaticSegment("author"), StaticSegment("contact")),
|
||||
children: (),
|
||||
data: (),
|
||||
view: "Contact Me",
|
||||
rndr: PhantomData,
|
||||
},
|
||||
data: (),
|
||||
view: "Home",
|
||||
rndr: PhantomData,
|
||||
});
|
||||
let routes: Routes<_, Dom> =
|
||||
Routes::new(NestedRoute::new(StaticSegment(""), || "Home").child(
|
||||
NestedRoute::new(
|
||||
(StaticSegment("author"), StaticSegment("contact")),
|
||||
|| "Contact Me",
|
||||
),
|
||||
));
|
||||
let matched = routes.match_route("/");
|
||||
assert!(matched.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn chooses_between_nested_routes() {
|
||||
let routes = Routes::new((
|
||||
NestedRoute {
|
||||
segments: StaticSegment("/"),
|
||||
children: (
|
||||
NestedRoute {
|
||||
segments: StaticSegment(""),
|
||||
children: (),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
NestedRoute {
|
||||
segments: StaticSegment("about"),
|
||||
children: (),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
let routes: Routes<_, Dom> = Routes::new((
|
||||
NestedRoute::new(StaticSegment("/"), || ()).child((
|
||||
NestedRoute::new(StaticSegment(""), || ()),
|
||||
NestedRoute::new(StaticSegment("about"), || ()),
|
||||
)),
|
||||
NestedRoute::new(StaticSegment("/blog"), || ()).child((
|
||||
NestedRoute::new(StaticSegment(""), || ()),
|
||||
NestedRoute::new(
|
||||
(StaticSegment("post"), ParamSegment("id")),
|
||||
|| (),
|
||||
),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
NestedRoute {
|
||||
segments: StaticSegment("/blog"),
|
||||
children: (
|
||||
NestedRoute {
|
||||
segments: StaticSegment(""),
|
||||
children: (),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
NestedRoute {
|
||||
segments: (StaticSegment("post"), ParamSegment("id")),
|
||||
children: (),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
)),
|
||||
));
|
||||
|
||||
// generates routes correctly
|
||||
let (base, paths) = routes.generate_routes();
|
||||
assert_eq!(base, None);
|
||||
let paths = paths.into_iter().collect::<Vec<_>>();
|
||||
let paths = paths.into_iter().map(|g| g.segments).collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
paths,
|
||||
vec![
|
||||
|
@ -314,77 +266,29 @@ mod tests {
|
|||
assert!(params.is_empty());
|
||||
let matched = routes.match_route("/blog/post/42").unwrap();
|
||||
let params = matched.to_params().collect::<Vec<_>>();
|
||||
assert_eq!(params, vec![("id", "42")]);
|
||||
assert_eq!(params, vec![("id".into(), "42".into())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn arbitrary_nested_routes() {
|
||||
let routes = Routes::new_with_base(
|
||||
let routes: Routes<_, Dom> = Routes::new_with_base(
|
||||
(
|
||||
NestedRoute {
|
||||
segments: StaticSegment("/"),
|
||||
children: (
|
||||
NestedRoute {
|
||||
segments: StaticSegment("/"),
|
||||
children: (),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
NestedRoute {
|
||||
segments: StaticSegment("about"),
|
||||
children: (),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
NestedRoute::new(StaticSegment("/"), || ()).child((
|
||||
NestedRoute::new(StaticSegment("/"), || ()),
|
||||
NestedRoute::new(StaticSegment("about"), || ()),
|
||||
)),
|
||||
NestedRoute::new(StaticSegment("/blog"), || ()).child((
|
||||
NestedRoute::new(StaticSegment(""), || ()),
|
||||
NestedRoute::new(StaticSegment("category"), || ()),
|
||||
NestedRoute::new(
|
||||
(StaticSegment("post"), ParamSegment("id")),
|
||||
|| (),
|
||||
),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
NestedRoute {
|
||||
segments: StaticSegment("/blog"),
|
||||
children: (
|
||||
NestedRoute {
|
||||
segments: StaticSegment(""),
|
||||
children: (),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
NestedRoute {
|
||||
segments: StaticSegment("category"),
|
||||
children: (),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
NestedRoute {
|
||||
segments: (
|
||||
StaticSegment("post"),
|
||||
ParamSegment("id"),
|
||||
),
|
||||
children: (),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
NestedRoute {
|
||||
segments: (
|
||||
StaticSegment("/contact"),
|
||||
WildcardSegment("any"),
|
||||
),
|
||||
children: (),
|
||||
data: (),
|
||||
view: || (),
|
||||
rndr: PhantomData,
|
||||
},
|
||||
)),
|
||||
NestedRoute::new(
|
||||
(StaticSegment("/contact"), WildcardSegment("any")),
|
||||
|| (),
|
||||
),
|
||||
),
|
||||
"/portfolio",
|
||||
);
|
||||
|
@ -402,15 +306,15 @@ mod tests {
|
|||
|
||||
let matched = routes.match_route("/portfolio/blog/post/42").unwrap();
|
||||
let params = matched.to_params().collect::<Vec<_>>();
|
||||
assert_eq!(params, vec![("id", "42")]);
|
||||
assert_eq!(params, vec![("id".into(), "42".into())]);
|
||||
|
||||
let matched = routes.match_route("/portfolio/contact").unwrap();
|
||||
let params = matched.to_params().collect::<Vec<_>>();
|
||||
assert_eq!(params, vec![("any", "")]);
|
||||
assert_eq!(params, vec![("any".into(), "".into())]);
|
||||
|
||||
let matched = routes.match_route("/portfolio/contact/foobar").unwrap();
|
||||
let params = matched.to_params().collect::<Vec<_>>();
|
||||
assert_eq!(params, vec![("any", "foobar")]);
|
||||
assert_eq!(params, vec![("any".into(), "foobar".into())]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,12 +22,12 @@ static ROUTE_ID: AtomicU16 = AtomicU16::new(1);
|
|||
#[derive(Debug, Copy, PartialEq, Eq)]
|
||||
pub struct NestedRoute<Segments, Children, Data, View, R> {
|
||||
id: u16,
|
||||
pub segments: Segments,
|
||||
pub children: Option<Children>,
|
||||
pub data: Data,
|
||||
pub view: View,
|
||||
pub rndr: PhantomData<R>,
|
||||
pub ssr_mode: SsrMode,
|
||||
segments: Segments,
|
||||
children: Option<Children>,
|
||||
data: Data,
|
||||
view: View,
|
||||
rndr: PhantomData<R>,
|
||||
ssr_mode: SsrMode,
|
||||
}
|
||||
|
||||
impl<Segments, Children, Data, View, R> Clone
|
||||
|
@ -52,7 +52,7 @@ where
|
|||
}
|
||||
|
||||
impl<Segments, View, R> NestedRoute<Segments, (), (), View, R> {
|
||||
pub fn new(path: Segments, view: View, ssr_mode: SsrMode) -> Self
|
||||
pub fn new(path: Segments, view: View) -> Self
|
||||
where
|
||||
View: ChooseView<R>,
|
||||
R: Renderer + 'static,
|
||||
|
@ -64,7 +64,7 @@ impl<Segments, View, R> NestedRoute<Segments, (), (), View, R> {
|
|||
data: (),
|
||||
view,
|
||||
rndr: PhantomData,
|
||||
ssr_mode,
|
||||
ssr_mode: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -93,6 +93,11 @@ impl<Segments, Data, View, R> NestedRoute<Segments, (), Data, View, R> {
|
|||
rndr,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ssr_mode(mut self, ssr_mode: SsrMode) -> Self {
|
||||
self.ssr_mode = ssr_mode;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
|
|
Loading…
Reference in a new issue