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

146 lines
5.7 KiB
Elm
Raw Normal View History

module StaticHttpUnitTests exposing (all)
import Dict exposing (Dict)
import Expect
import Json.Decode.Exploration
import OptimizedDecoder as Decode
import Pages.Internal.ApplicationType as ApplicationType
import Pages.StaticHttp as StaticHttp
import Pages.StaticHttp.Request as Request
import Pages.StaticHttpRequest as StaticHttpRequest
2019-11-12 04:48:08 +03:00
import Secrets
import Test exposing (Test, describe, only, test)
getWithoutSecrets url =
StaticHttp.get (Secrets.succeed url)
requestsDict requestMap =
requestMap
|> List.map
(\( request, response ) ->
( request |> Request.hash
, response
)
)
|> Dict.fromList
get : String -> Request.Request
get url =
{ method = "GET"
, url = url
, headers = []
, body = StaticHttp.emptyBody
}
all : Test
all =
describe "Static Http Requests"
[ test "andThen" <|
\() ->
StaticHttp.get (Secrets.succeed "first") (Decode.succeed "NEXT")
|> StaticHttp.andThen
(\continueUrl ->
getWithoutSecrets "NEXT" (Decode.succeed ())
)
|> (\request ->
StaticHttpRequest.resolveUrls ApplicationType.Cli
request
(requestsDict
[ ( get "first", "null" )
, ( get "NEXT", "null" )
]
)
2019-11-12 04:48:08 +03:00
|> Tuple.mapSecond (List.map Secrets.maskedLookup)
|> Expect.equal ( True, [ getReq "first", getReq "NEXT" ] )
)
, test "andThen staring with done" <|
\() ->
StaticHttp.succeed ()
|> StaticHttp.andThen
(\_ ->
getWithoutSecrets "NEXT" (Decode.succeed ())
)
|> (\request ->
StaticHttpRequest.resolveUrls ApplicationType.Cli
request
(requestsDict
[ ( get "NEXT", "null" )
]
)
2019-11-12 04:48:08 +03:00
|> Tuple.mapSecond (List.map Secrets.maskedLookup)
|> Expect.equal ( True, [ getReq "NEXT" ] )
)
, test "map" <|
\() ->
getWithoutSecrets "first" (Decode.succeed "NEXT")
|> StaticHttp.andThen
(\continueUrl ->
-- StaticHttp.get continueUrl (Decode.succeed ())
getWithoutSecrets "NEXT" (Decode.succeed ())
)
|> StaticHttp.map (\_ -> ())
|> (\request ->
StaticHttpRequest.resolveUrls ApplicationType.Cli
request
(requestsDict
[ ( get "first", "null" )
, ( get "NEXT", "null" )
]
)
2019-11-12 04:48:08 +03:00
|> Tuple.mapSecond (List.map Secrets.maskedLookup)
|> Expect.equal ( True, [ getReq "first", getReq "NEXT" ] )
)
, test "andThen chain with 1 response available and 1 pending" <|
\() ->
getWithoutSecrets "first" (Decode.succeed "NEXT")
|> StaticHttp.andThen
(\continueUrl ->
getWithoutSecrets "NEXT" (Decode.succeed ())
)
|> (\request ->
StaticHttpRequest.resolveUrls ApplicationType.Cli
request
(requestsDict
[ ( get "first", "null" )
]
)
2019-11-12 04:48:08 +03:00
|> Tuple.mapSecond (List.map Secrets.maskedLookup)
|> Expect.equal ( False, [ getReq "first", getReq "NEXT" ] )
)
, test "andThen chain with 1 response available and 2 pending" <|
\() ->
getWithoutSecrets "first" Decode.int
|> StaticHttp.andThen
(\continueUrl ->
getWithoutSecrets "NEXT" Decode.string
|> StaticHttp.andThen
(\_ ->
getWithoutSecrets "LAST"
Decode.string
)
)
|> (\request ->
StaticHttpRequest.resolveUrls ApplicationType.Cli
request
(requestsDict
[ ( get "first", "1" )
]
)
2019-11-12 04:48:08 +03:00
|> Tuple.mapSecond (List.map Secrets.maskedLookup)
|> Expect.equal ( False, [ getReq "first", getReq "NEXT" ] )
)
]
2019-11-11 23:40:38 +03:00
2020-01-04 00:37:50 +03:00
getReq : String -> StaticHttp.RequestDetails
getReq url =
2020-01-04 00:22:53 +03:00
{ url = url
, method = "GET"
, headers = []
, body = StaticHttp.emptyBody
2020-01-04 00:22:53 +03:00
}