Skip to content

Commit

Permalink
Fix Some Router Behaviour (#2107)
Browse files Browse the repository at this point in the history
* Add Redirect Comp.

* Fix router behaviour.

* Fix output.

* Fix pr-flow.

* Remove Redirect.

* Readd 77b46bf.
  • Loading branch information
futursolo authored Oct 16, 2021
1 parent f2a0d61 commit 35e1ba6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
21 changes: 11 additions & 10 deletions packages/yew-router/src/macro_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use crate::utils::{base_url, strip_slash_suffix};
use crate::Routable;

Expand All @@ -9,16 +11,14 @@ pub fn build_router<R: Routable>() -> Router {
let base = base_url();
let mut router = Router::new();
R::routes().iter().for_each(|path| {
match &base {
Some(base) => {
let route = format!("{}{}", base, path);
let route = strip_slash_suffix(&route);
router.add(route, path.to_string());
}
_ => {
router.add(path, path.to_string());
}
let route = match base {
Some(ref base) => Cow::from(format!("{}{}", base, path)),
None => (*path).into(),
};

let stripped_route = strip_slash_suffix(&route);

router.add(stripped_route, path.to_string());
});

router
Expand All @@ -30,7 +30,8 @@ pub fn recognize_with_router<R: Routable>(router: &Router, pathname: &str) -> Op
let matched = router.recognize(pathname);

match matched {
Ok(matched) => R::from_path(matched.handler(), &matched.params().into_iter().collect()),
Ok(matched) => R::from_path(matched.handler(), &matched.params().into_iter().collect())
.or_else(R::not_found_route),
Err(_) => R::not_found_route(),
}
}
50 changes: 50 additions & 0 deletions packages/yew-router/tests/router_unit_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use yew_router::prelude::*;

wasm_bindgen_test_configure!(run_in_browser);

#[test]
fn router_always_404() {
#[derive(Routable, Debug, Clone, PartialEq)]
enum AppRoute {
#[at("/")]
Home,
#[at("/:id")]
Article { id: u64 },
#[at("/404")]
#[not_found]
NotFound,
}

assert_eq!(
Some(AppRoute::NotFound),
AppRoute::recognize("/not/matched/route")
);
assert_eq!(
Some(AppRoute::NotFound),
AppRoute::recognize("/not-matched-route")
);
}

#[test]
fn router_trailing_slash() {
#[derive(Routable, Debug, Clone, PartialEq)]
enum AppRoute {
#[at("/")]
Home,
#[at("/category/:name/")]
Category { name: String },
#[at("/:id")]
Article { id: u64 },
#[at("/404")]
#[not_found]
NotFound,
}

assert_eq!(
Some(AppRoute::Category {
name: "cooking-recipes".to_string()
}),
AppRoute::recognize("/category/cooking-recipes/")
);
}

0 comments on commit 35e1ba6

Please sign in to comment.