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

146 lines
5.6 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
import OptimizedDecoder as Decode
import Pages.Internal.ApplicationType as ApplicationType
import Pages.StaticHttp.Request as Request
import Pages.StaticHttpRequest as StaticHttpRequest
2019-11-12 04:48:08 +03:00
import Secrets
2020-12-07 19:41:10 +03:00
import Test exposing (Test, describe, test)
getWithoutSecrets url =
DataSource.Http.get (Secrets.succeed url)
requestsDict requestMap =
requestMap
|> List.map
(\( request, response ) ->
( request |> Request.hash
, Just response
)
)
|> Dict.fromList
get : String -> Request.Request
get url =
{ method = "GET"
, url = url
, headers = []
2021-04-20 17:31:19 +03:00
, body = DataSource.emptyBody
}
all : Test
all =
2021-04-01 05:55:28 +03:00
describe "Static Http Requests unit tests"
[ test "andThen" <|
\() ->
DataSource.Http.get (Secrets.succeed "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 ->
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" <|
\() ->
2021-04-20 17:31:19 +03:00
DataSource.succeed ()
|> DataSource.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")
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 ->
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")
2021-04-20 17:31:19 +03:00
|> DataSource.andThen
2021-04-03 00:44:40 +03:00
(\_ ->
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
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 ->
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
getReq : String -> DataSource.Http.RequestDetails
getReq url =
2020-01-04 00:22:53 +03:00
{ url = url
, method = "GET"
, headers = []
2021-04-20 17:31:19 +03:00
, body = DataSource.emptyBody
2020-01-04 00:22:53 +03:00
}