2022-09-09 22:45:28 +03:00
|
|
|
module Pages.RouteParamsTest exposing (..)
|
|
|
|
|
2022-09-12 21:59:36 +03:00
|
|
|
import Elm
|
2022-09-09 22:45:28 +03:00
|
|
|
import Elm.Annotation
|
2022-09-12 23:19:25 +03:00
|
|
|
import Elm.ToString
|
|
|
|
import Expect exposing (Expectation)
|
2022-09-09 22:45:28 +03:00
|
|
|
import Pages.Internal.RoutePattern as RoutePattern
|
|
|
|
import Test exposing (Test, describe, test)
|
|
|
|
|
|
|
|
|
|
|
|
suite : Test
|
|
|
|
suite =
|
|
|
|
describe "RouteParams"
|
|
|
|
[ test "no dynamic segments" <|
|
|
|
|
\() ->
|
|
|
|
RoutePattern.fromModuleName [ "No", "Dynamic", "Segments" ]
|
|
|
|
|> Maybe.map RoutePattern.toRouteParamsRecord
|
|
|
|
|> Expect.equal
|
|
|
|
(Just [])
|
|
|
|
, test "single dynamic segments" <|
|
|
|
|
\() ->
|
|
|
|
RoutePattern.fromModuleName [ "User", "Id_" ]
|
|
|
|
|> Maybe.map RoutePattern.toRouteParamsRecord
|
|
|
|
|> Expect.equal
|
|
|
|
(Just
|
|
|
|
[ ( "id", Elm.Annotation.string )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
, test "two dynamic segments" <|
|
|
|
|
\() ->
|
|
|
|
RoutePattern.fromModuleName [ "UserId_", "ProductId_" ]
|
|
|
|
|> Maybe.map RoutePattern.toRouteParamsRecord
|
|
|
|
|> Expect.equal
|
|
|
|
(Just
|
|
|
|
[ ( "userId", Elm.Annotation.string )
|
|
|
|
, ( "productId", Elm.Annotation.string )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
, test "splat ending" <|
|
|
|
|
\() ->
|
|
|
|
RoutePattern.fromModuleName [ "UserName_", "RepoName_", "Blob", "SPLAT_" ]
|
|
|
|
|> Maybe.map RoutePattern.toRouteParamsRecord
|
|
|
|
|> Expect.equal
|
|
|
|
(Just
|
|
|
|
[ ( "userName", Elm.Annotation.string )
|
|
|
|
, ( "repoName", Elm.Annotation.string )
|
|
|
|
, ( "splat"
|
|
|
|
, Elm.Annotation.tuple
|
|
|
|
Elm.Annotation.string
|
|
|
|
(Elm.Annotation.list Elm.Annotation.string)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
, test "optional splat ending" <|
|
|
|
|
\() ->
|
|
|
|
RoutePattern.fromModuleName [ "SPLAT__" ]
|
|
|
|
|> Maybe.map RoutePattern.toRouteParamsRecord
|
|
|
|
|> Expect.equal
|
|
|
|
(Just
|
|
|
|
[ ( "splat", Elm.Annotation.list Elm.Annotation.string )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
, test "ending with optional segment" <|
|
|
|
|
\() ->
|
|
|
|
RoutePattern.fromModuleName [ "Docs", "Section__" ]
|
|
|
|
|> Maybe.map RoutePattern.toRouteParamsRecord
|
|
|
|
|> Expect.equal
|
|
|
|
(Just
|
|
|
|
[ ( "section", Elm.Annotation.maybe Elm.Annotation.string )
|
|
|
|
]
|
|
|
|
)
|
2022-09-12 21:59:36 +03:00
|
|
|
, describe "toRouteVariant"
|
|
|
|
[ test "root route" <|
|
|
|
|
\() ->
|
2022-09-12 23:19:25 +03:00
|
|
|
[]
|
|
|
|
|> expectRouteDefinition
|
|
|
|
(Elm.variant "Index")
|
2022-09-12 21:59:36 +03:00
|
|
|
, test "static-only route" <|
|
|
|
|
\() ->
|
|
|
|
RoutePattern.fromModuleName [ "About" ]
|
|
|
|
|> Maybe.map RoutePattern.toVariant
|
|
|
|
|> Expect.equal
|
|
|
|
(Just (Elm.variant "About"))
|
2022-09-12 22:16:18 +03:00
|
|
|
, test "dynamic param" <|
|
|
|
|
\() ->
|
2022-09-12 23:19:25 +03:00
|
|
|
[ "User", "Id_" ]
|
|
|
|
|> expectRouteDefinition
|
|
|
|
(Elm.variantWith "User__Id_"
|
|
|
|
[ Elm.Annotation.record
|
|
|
|
[ ( "id", Elm.Annotation.string )
|
2022-09-12 22:16:18 +03:00
|
|
|
]
|
2022-09-12 23:19:25 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
, test "required splat" <|
|
|
|
|
\() ->
|
|
|
|
[ "Username_", "Repo_", "Blob", "SPLAT_" ]
|
|
|
|
|> expectRouteDefinition
|
|
|
|
(Elm.variantWith "Username___Repo___Blob__SPLAT_"
|
|
|
|
[ Elm.Annotation.record
|
|
|
|
[ ( "username", Elm.Annotation.string )
|
|
|
|
, ( "repo", Elm.Annotation.string )
|
|
|
|
, ( "splat"
|
|
|
|
, Elm.Annotation.tuple
|
|
|
|
Elm.Annotation.string
|
|
|
|
(Elm.Annotation.list Elm.Annotation.string)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
]
|
2022-09-12 22:16:18 +03:00
|
|
|
)
|
2022-09-12 23:20:44 +03:00
|
|
|
, test "optional splat" <|
|
|
|
|
\() ->
|
|
|
|
[ "SPLAT__" ]
|
|
|
|
|> expectRouteDefinition
|
|
|
|
(Elm.variantWith "SPLAT__"
|
|
|
|
[ Elm.Annotation.record
|
|
|
|
[ ( "splat"
|
|
|
|
, Elm.Annotation.list Elm.Annotation.string
|
|
|
|
)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
2022-09-12 21:59:36 +03:00
|
|
|
]
|
2022-09-09 22:45:28 +03:00
|
|
|
]
|
2022-09-12 23:19:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
expectRouteDefinition : Elm.Variant -> List String -> Expectation
|
|
|
|
expectRouteDefinition expected moduleName =
|
|
|
|
RoutePattern.fromModuleName moduleName
|
|
|
|
|> Maybe.map (RoutePattern.toVariant >> toString)
|
|
|
|
|> Maybe.withDefault "<ERROR>"
|
|
|
|
|> Expect.equal (expected |> toString)
|
|
|
|
|
|
|
|
|
|
|
|
toString : Elm.Variant -> String
|
|
|
|
toString variants =
|
|
|
|
Elm.customType "Route" [ variants ]
|
|
|
|
|> Elm.ToString.declaration
|
|
|
|
|> .body
|