Skip to content

Commit

Permalink
Test: Exercise total route methods
Browse files Browse the repository at this point in the history
  • Loading branch information
raquo committed Nov 9, 2024
1 parent a26499b commit 0004a59
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
15 changes: 12 additions & 3 deletions js/src/test/scala/com/raquo/waypoint/BasePathSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ class BasePathSpec extends UnitSpec {

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

val homeRoute: Route.Partial[HomePage.type, Unit] = Route.static(
// #TODO[Test] homeRoute and homeRouteTotal should not be in the same test,
// they are overlapping – that is not a real life condition.
// - Add a bool parameter to `runTest` that would add one or the other to routes?

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

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

val libraryRoute: Route[LibraryPage, Int] = Route(
val libraryRoute: Route.Total[LibraryPage, Int] = Route(
encode = _.libraryId,
decode = arg => LibraryPage(libraryId = arg),
pattern = root / "app" / "library" / segment[Int] / endOfSegments,
Expand Down Expand Up @@ -279,6 +283,11 @@ class BasePathSpec extends UnitSpec {
urlForPage(DocsPage(NumPage(50))) shouldBe None
}

it("total routes - total APIs") {
homeRouteTotal.relativeUrlForPage(HomePage) shouldBe basePath + "/"
libraryRoute.argsFromPageTotal(LibraryPage(123)) shouldBe 123
}

it("should not compile with non-singleton type for a staticTotal route") {
assertTypeError(
"""|Route.staticTotal(
Expand Down
12 changes: 8 additions & 4 deletions shared/src/main/scala/com/raquo/waypoint/Route.scala
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,11 @@ object Route {

/** Create a route for a static page that does not encode any data in the URL.
*
* Take note that this returns a [[Partial]] route and you might want to use [[staticTotal]] instead.
* */
* This returns a [[Partial]] route. If you want a [[Total]] route,
* use [[staticTotal]] instead. They behave the same, but the total version
* offers a couple extra methods, but it requires that the `staticPage` is
* a singleton (e.g. `object HomePage`).
*/
def static[Page](
staticPage: Page,
pattern: PathSegment[Unit, DummyError],
Expand All @@ -469,10 +472,11 @@ object Route {

/** Create a route for a static page that does not encode any data in the URL.
*
* This only allows using singleton types, like `object Foo`.
* This version only allows using singleton types, like `object Foo`.
* See [[static]] for a more relaxed version.
*
* @see [[ValueOf]] - evidence that `Page` is a singleton type
* */
*/
def staticTotal[Page: ValueOf: ClassTag](
staticPage: Page,
pattern: PathSegment[Unit, DummyError],
Expand Down

0 comments on commit 0004a59

Please sign in to comment.