elm-pages-v3-beta/examples/routing/tests/RouteTest.elm

97 lines
2.7 KiB
Elm
Raw Normal View History

2021-04-28 18:28:56 +03:00
module RouteTest exposing (..)
import Expect
2021-04-28 20:49:00 +03:00
import Fuzz exposing (Fuzzer)
import Route exposing (Route)
import Test exposing (Test, describe, fuzz, test)
2021-04-28 23:04:20 +03:00
import Url
2021-04-28 18:28:56 +03:00
all : Test
all =
describe "routes"
[ test "test 1" <|
\() ->
{ path = "/cats/larry" }
|> Route.urlToRoute
|> Expect.equal
(Route.Cats__Name__
{ name = Just "larry" }
|> Just
)
2021-04-28 20:49:00 +03:00
, test "catch all route" <|
\() ->
{ path = "/date/1/2/3" }
|> Route.urlToRoute
|> Expect.equal
(Route.Date__SPLAT_
{ splat = ( "1", [ "2", "3" ] ) }
|> Just
)
, fuzz routeFuzzer "reversible" <|
\route ->
let
asUrl =
2021-05-23 20:20:09 +03:00
route
2021-04-28 20:49:00 +03:00
|> Route.routeToPath
|> (\path ->
{ path = path |> String.join "/" }
)
2021-04-28 18:28:56 +03:00
2021-04-28 20:49:00 +03:00
asRoute =
asUrl
|> Route.urlToRoute
in
{ route = asRoute
, path = asUrl.path
}
|> Expect.equal
{ route = Just route
, path = asUrl.path
}
]
routeFuzzer : Fuzzer Route
routeFuzzer =
Fuzz.oneOf
2021-04-28 23:04:20 +03:00
[ Fuzz.map
(\string -> Route.Cats__Name__ { name = Just string })
nonEmptyUrlEscapedString
2021-04-28 20:49:00 +03:00
, Fuzz.int
|> Fuzz.map
(\number ->
Route.Slide__Number_ { number = String.fromInt number }
)
, Fuzz.int
|> Fuzz.map
(\number ->
Route.Date__SPLAT_ { splat = ( String.fromInt number, [] ) }
)
, Fuzz.map2
(\first rest ->
Route.Date__SPLAT_
{ splat =
( String.fromInt first
, List.map String.fromInt rest
)
}
)
Fuzz.int
(Fuzz.list Fuzz.int)
2021-04-28 18:28:56 +03:00
]
2021-04-28 23:04:20 +03:00
nonEmptyUrlEscapedString : Fuzzer String
nonEmptyUrlEscapedString =
Fuzz.map2
(\c string ->
Url.percentEncode
(String.fromChar
c
++ string
)
)
Fuzz.char
Fuzz.string