elm-pages-v3-beta/tests/ApiHandlerTests.elm

105 lines
3.8 KiB
Elm
Raw Normal View History

module ApiHandlerTests exposing (..)
2021-04-30 18:42:07 +03:00
import ApiHandler exposing (..)
import Expect
import Test exposing (describe, only, test)
all =
describe "api routes"
2021-05-01 02:03:47 +03:00
[ test "match top-level file with no extension" <|
\() ->
succeed
(\userId ->
{ body = "Data for user " ++ userId }
)
2021-05-01 06:19:39 +03:00
|> capture
2021-05-01 02:03:47 +03:00
|> tryMatch "123"
|> Expect.equal (Just { body = "Data for user 123" })
, test "file with extension" <|
\() ->
succeed
(\userId ->
{ body = "Data for user " ++ userId }
)
2021-05-01 06:19:39 +03:00
|> capture
|> literal ".json"
2021-05-01 02:03:47 +03:00
|> tryMatch "124.json"
|> Expect.equal (Just { body = "Data for user 124" })
, test "file path with multiple segments" <|
\() ->
succeed
(\userId ->
{ body = "Data for user " ++ userId }
)
2021-05-01 06:19:39 +03:00
|> literal "users"
2021-05-01 02:03:47 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
|> literal ".json"
2021-05-01 02:03:47 +03:00
|> tryMatch "users/123.json"
|> Expect.equal (Just { body = "Data for user 123" })
, test "routes" <|
\() ->
succeed
(\userId ->
{ body = "Data for user " ++ userId }
)
2021-05-01 06:19:39 +03:00
|> literal "users"
2021-05-01 02:03:47 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
|> literal ".json"
2021-05-01 02:03:47 +03:00
|> withRoutes
(\constructor ->
2021-05-01 03:03:57 +03:00
[ constructor "100"
, constructor "101"
]
2021-05-01 02:03:47 +03:00
)
|> Expect.equal
2021-05-01 03:03:57 +03:00
[ "users/100.json"
, "users/101.json"
]
2021-05-01 02:03:47 +03:00
, describe "multi-part"
[ test "multi-level routes" <|
\() ->
2021-05-01 03:04:34 +03:00
succeed
(\userName repoName ->
{ body = "Data for user" }
)
2021-05-01 06:19:39 +03:00
|> literal "repos"
2021-05-01 03:04:34 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
2021-05-01 03:04:34 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
|> literal ".json"
|> withRoutes
(\a ->
2021-05-01 03:03:57 +03:00
[ a "dillonkearns" "elm-pages"
, a "dillonkearns" "elm-markdown"
]
)
|> Expect.equal
2021-05-01 03:03:57 +03:00
[ "repos/dillonkearns/elm-pages.json"
, "repos/dillonkearns/elm-markdown.json"
]
, test "3-level route" <|
\() ->
2021-05-01 03:04:34 +03:00
succeed
(\username repo branch ->
{ body = [ username, repo, branch ] |> String.join " - " }
)
2021-05-01 06:19:39 +03:00
|> literal "repos"
2021-05-01 03:04:34 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
2021-05-01 03:04:34 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
2021-05-01 03:04:34 +03:00
|> slash
2021-05-01 06:19:39 +03:00
|> capture
|> withRoutes
(\constructor ->
[ constructor "dillonkearns" "elm-pages" "static-files" ]
)
|> Expect.equal
[ "repos/dillonkearns/elm-pages/static-files" ]
]
]