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

147 lines
5.9 KiB
Elm
Raw Normal View History

module StaticHttpUnitTests exposing (all)
2021-04-20 17:31:19 +03:00
import DataSource
import DataSource.Http
2020-12-07 19:41:10 +03:00
import Dict
import Expect
2022-01-28 07:43:16 +03:00
import Json.Decode as Decode
2022-02-26 19:36:55 +03:00
import Json.Encode as Encode
import Pages.StaticHttp.Request as Request
import Pages.StaticHttpRequest as StaticHttpRequest
2022-02-26 19:36:55 +03:00
import RequestsAndPending
2020-12-07 19:41:10 +03:00
import Test exposing (Test, describe, test)
2021-06-04 03:59:58 +03:00
getWithoutSecrets : String -> Decode.Decoder a -> DataSource.DataSource a
getWithoutSecrets url =
2022-01-28 07:43:16 +03:00
DataSource.Http.get url
2021-06-04 03:59:58 +03:00
requestsDict : List ( Request.Request, b ) -> Dict.Dict String (Maybe b)
requestsDict requestMap =
requestMap
|> List.map
(\( request, response ) ->
( request |> Request.hash
, Just response
)
)
|> Dict.fromList
get : String -> Request.Request
get url =
{ method = "GET"
, url = url
2022-02-26 19:46:07 +03:00
-- TODO try to abstract away hardcoding of elm-pages-internal in test code
, headers = [ ( "elm-pages-internal", "ExpectJson" ) ]
, body = DataSource.Http.emptyBody
}
all : Test
all =
2021-04-01 05:55:28 +03:00
describe "Static Http Requests unit tests"
[ test "andThen" <|
\() ->
2022-01-28 07:43:16 +03:00
DataSource.Http.get "first" (Decode.succeed "NEXT")
2021-04-20 17:31:19 +03:00
|> DataSource.andThen
2021-04-03 00:44:40 +03:00
(\_ ->
getWithoutSecrets "NEXT" (Decode.succeed ())
)
|> (\request ->
2022-02-08 01:27:05 +03:00
StaticHttpRequest.resolveUrls
request
(requestsDict
2022-02-26 19:36:55 +03:00
[ ( get "first", RequestsAndPending.Response Nothing (RequestsAndPending.JsonBody Encode.null) )
, ( get "NEXT", RequestsAndPending.Response Nothing (RequestsAndPending.JsonBody Encode.null) )
]
)
|> Expect.equal [ getReq "first", getReq "NEXT" ]
)
, test "andThen staring with done" <|
\() ->
2021-04-20 17:31:19 +03:00
DataSource.succeed ()
|> DataSource.andThen
(\_ ->
getWithoutSecrets "NEXT" (Decode.succeed ())
)
|> (\request ->
2022-02-08 01:27:05 +03:00
StaticHttpRequest.resolveUrls
request
(requestsDict
2022-02-26 19:36:55 +03:00
[ ( get "NEXT", RequestsAndPending.Response Nothing (RequestsAndPending.JsonBody Encode.null) )
]
)
|> Expect.equal [ getReq "NEXT" ]
)
, test "map" <|
\() ->
getWithoutSecrets "first" (Decode.succeed "NEXT")
2021-04-20 17:31:19 +03:00
|> DataSource.andThen
2021-04-03 00:44:40 +03:00
(\_ ->
-- StaticHttp.get continueUrl (Decode.succeed ())
getWithoutSecrets "NEXT" (Decode.succeed ())
)
2021-04-20 17:31:19 +03:00
|> DataSource.map (\_ -> ())
|> (\request ->
2022-02-08 01:27:05 +03:00
StaticHttpRequest.resolveUrls
request
(requestsDict
2022-02-26 19:36:55 +03:00
[ ( get "first", RequestsAndPending.Response Nothing (RequestsAndPending.JsonBody Encode.null) )
, ( get "NEXT", RequestsAndPending.Response Nothing (RequestsAndPending.JsonBody Encode.null) )
]
)
|> Expect.equal [ getReq "first", getReq "NEXT" ]
)
, test "andThen chain with 1 response available and 1 pending" <|
\() ->
getWithoutSecrets "first" (Decode.succeed "NEXT")
2021-04-20 17:31:19 +03:00
|> DataSource.andThen
2021-04-03 00:44:40 +03:00
(\_ ->
getWithoutSecrets "NEXT" (Decode.succeed ())
)
|> (\request ->
2022-02-08 01:27:05 +03:00
StaticHttpRequest.resolveUrls
request
(requestsDict
2022-02-26 19:36:55 +03:00
[ ( get "first", RequestsAndPending.Response Nothing (RequestsAndPending.JsonBody Encode.null) )
]
)
|> Expect.equal [ getReq "first", getReq "NEXT" ]
)
, test "andThen chain with 1 response available and 2 pending" <|
\() ->
getWithoutSecrets "first" Decode.int
2021-04-20 17:31:19 +03:00
|> DataSource.andThen
2021-04-03 00:44:40 +03:00
(\_ ->
getWithoutSecrets "NEXT" Decode.string
2021-04-20 17:31:19 +03:00
|> DataSource.andThen
(\_ ->
getWithoutSecrets "LAST"
Decode.string
)
)
|> (\request ->
2022-02-08 01:27:05 +03:00
StaticHttpRequest.resolveUrls
request
(requestsDict
2022-02-26 19:36:55 +03:00
[ ( get "first", RequestsAndPending.Response Nothing (RequestsAndPending.JsonBody (Encode.int 1)) )
]
)
|> Expect.equal [ getReq "first", getReq "NEXT" ]
)
]
2019-11-11 23:40:38 +03:00
getReq : String -> DataSource.Http.RequestDetails
getReq url =
2020-01-04 00:22:53 +03:00
{ url = url
, method = "GET"
2022-02-26 19:46:07 +03:00
-- TODO try to abstract away hardcoding of elm-pages-internal in test code
, headers = [ ( "elm-pages-internal", "ExpectJson" ) ]
, body = DataSource.Http.emptyBody
2020-01-04 00:22:53 +03:00
}