Skip to content

Commit

Permalink
Make Route.staticTotal fully type-safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturaz committed Aug 15, 2024
1 parent 6b3e117 commit 4f6f4b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
30 changes: 22 additions & 8 deletions js/src/test/scala/com/raquo/waypoint/BasePathSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ class BasePathSpec extends UnitSpec {

describe(s"basePath = `$basePath`") {

val homeRoute: Route.Total[HomePage.type, Unit] = Route.staticTotal(
val homeRoute: Route.Partial[HomePage.type, Unit] = Route.static(
HomePage,
pattern = root / endOfSegments,
basePath = basePath
)

val homeRouteTotal: Route.Total[HomePage.type, Unit] = Route.staticTotal(
HomePage,
pattern = root / endOfSegments,
basePath = basePath
Expand Down Expand Up @@ -137,6 +143,7 @@ class BasePathSpec extends UnitSpec {
textRoute,
noteRoute,
homeRoute,
homeRouteTotal,
searchRoute,
bigLegalRoute,
bigNumRoute,
Expand All @@ -156,13 +163,18 @@ class BasePathSpec extends UnitSpec {
)

it("segment routes - parse urls - match") {
if (basePath.nonEmpty) {
expectPageRelative(homeRoute, origin, s"$basePath", Some(HomePage))
} else {
// (basePath + url) must be a relative URL, so if basePath is empty, url can't also be empty, it must start with `/`
expectPageRelativeFailure(homeRoute, origin, s"$basePath")
def testHomeRoute(homeRoute: Route[HomePage.type, Unit]) = {
if (basePath.nonEmpty) {
expectPageRelative(homeRoute, origin, s"$basePath", Some(HomePage))
} else {
// (basePath + url) must be a relative URL, so if basePath is empty, url can't also be empty, it must start with `/`
expectPageRelativeFailure(homeRoute, origin, s"$basePath")
}
expectPageRelative(homeRoute, origin, s"$basePath/", Some(HomePage))
}
expectPageRelative(homeRoute, origin, s"$basePath/", Some(HomePage))

testHomeRoute(homeRoute)
testHomeRoute(homeRouteTotal)

expectPageRelative(libraryRoute, origin, s"$basePath/app/library/1234", Some(LibraryPage(libraryId = 1234)))
expectPageRelative(libraryRoute, origin, s"$basePath/app/library/100?hello=world", Some(LibraryPage(libraryId = 100)))
Expand Down Expand Up @@ -190,12 +202,14 @@ class BasePathSpec extends UnitSpec {

expectPageRelative(libraryRoute, origin, "/app/library/1234", if (basePath.isEmpty) Some(LibraryPage(1234)) else None)

{
def testHomeRoute(homeRoute: Route[HomePage.type, Unit]) = {
if (Utils.basePathHasEmptyFragment(basePath)) {
val basePathWithoutFragment = Utils.basePathWithoutFragment(basePath)
expectPageRelative(homeRoute, origin, s"$basePathWithoutFragment", Some(HomePage))
}
}
testHomeRoute(homeRoute)
testHomeRoute(homeRouteTotal)

// @TODO[API] I mean... these are not absolute urls...
expectPageAbsoluteFailure(libraryRoute, origin, "//app/library/1234")
Expand Down
20 changes: 4 additions & 16 deletions shared/src/main/scala/com/raquo/waypoint/Route.scala
Original file line number Diff line number Diff line change
Expand Up @@ -477,27 +477,15 @@ object Route {

/** Create a route for a static page that does not encode any data in the URL.
*
* Note that this is only a total route when the [[Page]] type refers to an object type. For example, this is OK:
* {{{
* object Foo extends AppPage
*
* val route: Total[Foo.type, Unit] = Route.staticTotal(Foo, root / "foo" / endOfSegments)
* }}}
*
* However, it is possible to create a route which is not actually total:
* {{{
* object Foo extends AppPage
*
* val route: Total[AppPage, Unit] = Route.staticTotal[AppPage](Foo, root / "foo" / endOfSegments)
* }}}
*
* This will think that it can route any `AppPage`, however it will not. Use [[static]] for those cases instead.
* This only allows using singleton types, like `object Foo`.
*
* @see [[ValueOf]]
* */
def staticTotal[Page](
staticPage: Page,
pattern: PathSegment[Unit, DummyError],
basePath: String = ""
): Total[Page, Unit] = {
)(implicit valueOf: ValueOf[Page]): Total[Page, Unit] = {
Total[Page, Unit](
encode = _ => (),
decode = _ => staticPage,
Expand Down

0 comments on commit 4f6f4b6

Please sign in to comment.