Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type-safety for the total routes. #19

Merged
merged 8 commits into from
Nov 9, 2024
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ js/yarn.lock
.DS_Store

/.bsp

/.metals
# This is autogenerated
metals.sbt
.bloop
12 changes: 6 additions & 6 deletions js/src/test/scala/com/raquo/waypoint/BasePathSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,41 @@ class BasePathSpec extends UnitSpec {

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

val homeRoute: Route[HomePage.type, Unit] = Route.static(
val homeRoute: Route.Total[HomePage.type, Unit] = Route.staticTotal(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've replaced static with staticTotal here, but that leaves static untested. Since static is the primary method, it should retain its tests, and staticTotal tests should be added either in this file or separately.

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,
basePath = basePath
)

val textRoute: Route[TextPage, String] = Route(
val textRoute: Route.Total[TextPage, String] = Route(
encode = _.text,
decode = arg => TextPage(text = arg),
pattern = root / "app" / "test" / segment[String] / endOfSegments,
basePath = basePath
)

val noteRoute: Route[NotePage, (Int, Int)] = Route(
val noteRoute: Route.Total[NotePage, (Int, Int)] = Route(
encode = page => (page.libraryId, page.noteId),
decode = args => NotePage(libraryId = args._1, noteId = args._2, scrollPosition = 0),
pattern = root / "app" / "library" / segment[Int] / "note" / segment[Int] / endOfSegments,
basePath = basePath
)

val searchRoute: Route[SearchPage, String] = Route.onlyQuery(
val searchRoute: Route.Total[SearchPage, String] = Route.onlyQuery(
encode = page => page.query,
decode = arg => SearchPage(arg),
pattern = (root / "search" / endOfSegments) ? param[String]("query"),
basePath = basePath
)

val bigLegalRoute: Route[BigLegalPage, FragmentPatternArgs[String, Unit, String]] = Route.withFragment(
val bigLegalRoute: Route.Total[BigLegalPage, FragmentPatternArgs[String, Unit, String]] = Route.withFragment(
encode = page => FragmentPatternArgs(path = page.page, (), fragment = page.section),
decode = args => BigLegalPage(page = args.path, section = args.fragment),
pattern = (root / "legal" / segment[String] / endOfSegments) withFragment fragment[String],
Expand Down
16 changes: 8 additions & 8 deletions js/src/test/scala/com/raquo/waypoint/DynamicRouteSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,49 @@ class DynamicRouteSpec extends UnitSpec {

val origin = "http://example.com"

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
)

val textRoute: Route[TextPage, String] = Route(
val textRoute: Route.Total[TextPage, String] = Route(
encode = _.text,
decode = arg => TextPage(text = arg),
pattern = root / "app" / "test" / segment[String] / endOfSegments
)

val noteRoute: Route[NotePage, (Int, Int)] = Route(
val noteRoute: Route.Total[NotePage, (Int, Int)] = Route(
encode = page => (page.libraryId, page.noteId),
decode = args => NotePage(libraryId = args._1, noteId = args._2, scrollPosition = 0),
pattern = root / "app" / "library" / segment[Int] / "note" / segment[Int] / endOfSegments
)

val searchRoute: Route[SearchPage, String] = Route.onlyQuery(
val searchRoute: Route.Total[SearchPage, String] = Route.onlyQuery(
encode = page => page.query,
decode = arg => SearchPage(arg),
pattern = (root / "search" / endOfSegments) ? param[String]("query")
)

val workspaceSearchRoute: Route[WorkspaceSearchPage, PatternArgs[String, String]] = Route.withQuery(
val workspaceSearchRoute: Route.Total[WorkspaceSearchPage, PatternArgs[String, String]] = Route.withQuery(
encode = page => PatternArgs(page.workspaceId, page.query),
decode = args => WorkspaceSearchPage(workspaceId = args.path, query = args.params),
pattern = (root / "workspace" / segment[String] / endOfSegments) ? param[String]("query")
)

val legalRoute: Route[LegalPage, String] = Route.onlyFragment(
val legalRoute: Route.Total[LegalPage, String] = Route.onlyFragment(
encode = page => page.section,
decode = arg => LegalPage(arg),
pattern = (root / "legal" / endOfSegments) withFragment fragment[String]
)

val bigLegalRoute: Route[BigLegalPage, FragmentPatternArgs[String, Unit, String]] = Route.withFragment(
val bigLegalRoute: Route.Total[BigLegalPage, FragmentPatternArgs[String, Unit, String]] = Route.withFragment(
encode = page => FragmentPatternArgs(path = page.page, (), fragment = page.section),
decode = args => BigLegalPage(page = args.path, section = args.fragment),
pattern = (root / "legal" / segment[String] / endOfSegments) withFragment fragment[String]
)

val hugeLegalRoute: Route[HugeLegalPage, FragmentPatternArgs[String, Int, String]] = Route.withQueryAndFragment(
val hugeLegalRoute: Route.Total[HugeLegalPage, FragmentPatternArgs[String, Int, String]] = Route.withQueryAndFragment(
encode = page => FragmentPatternArgs(path = page.page, query = page.version, fragment = page.section),
decode = args => HugeLegalPage(page = args.path, version = args.query, section = args.fragment),
pattern = (root / "legal" / segment[String] / endOfSegments) ? param[Int]("version") withFragment fragment[String]
Expand Down
Loading