Get a simple test case passing for JSON reducers!

This commit is contained in:
Dillon Kearns 2019-11-03 10:07:20 -08:00
parent b66b9a3ad1
commit a04f26bc99
3 changed files with 94 additions and 3 deletions

View File

@ -32,6 +32,7 @@
"elm-community/result-extra": "2.2.1 <= v < 3.0.0",
"lukewestby/elm-string-interpolate": "1.0.4 <= v < 2.0.0",
"mdgriffith/elm-markup": "3.0.1 <= v < 4.0.0",
"mgold/elm-nonempty-list": "4.0.2 <= v < 5.0.0",
"miniBill/elm-codec": "1.2.0 <= v < 2.0.0",
"noahzgordon/elm-color-extra": "1.0.2 <= v < 2.0.0",
"tripokey/elm-fuzzy": "5.2.1 <= v < 6.0.0"

View File

@ -1,6 +1,6 @@
module StaticHttp exposing
( Request
, jsonRequest, jsonRequestWithSecrets
, jsonRequest, jsonRequestWithSecrets, reducedJsonRequest
, map, succeed
, andThen
, map2, map3, map4, map5, map6, map7, map8, map9
@ -9,7 +9,7 @@ module StaticHttp exposing
{-| TODO
@docs Request
@docs jsonRequest, jsonRequestWithSecrets
@docs jsonRequest, jsonRequestWithSecrets, reducedJsonRequest
@docs map, succeed
@docs andThen
@ -23,6 +23,7 @@ import Dict exposing (Dict)
import Head
import Html exposing (Html)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Exploration
import Pages.Internal.Secrets
import Pages.StaticHttpRequest exposing (Request(..))
import Secrets exposing (Secrets)
@ -217,7 +218,10 @@ jsonRequest url decoder =
case maybeResponse of
Just rawResponse ->
-- @@@@@ TODO reduce raw responses
Ok ( rawResponseDict, rawResponse )
Ok
( rawResponseDict
, rawResponse
)
Nothing ->
Err <| Pages.StaticHttpRequest.MissingHttpResponse url
@ -234,6 +238,60 @@ jsonRequest url decoder =
)
{-| TODO
-}
reducedJsonRequest : String -> Json.Decode.Exploration.Decoder a -> Request a
reducedJsonRequest url decoder =
Request
( [ Pages.Internal.Secrets.urlWithoutSecrets url ]
, \rawResponseDict ->
rawResponseDict
|> Dict.get url
|> (\maybeResponse ->
case maybeResponse of
Just rawResponse ->
-- @@@@@ TODO reduce raw responses
Ok
( rawResponseDict
-- |> Dict.update url (\maybeValue -> Just """{"fake": 123}""")
, rawResponse
)
Nothing ->
Err <| Pages.StaticHttpRequest.MissingHttpResponse url
)
|> Result.andThen
(\( strippedResponses, rawResponse ) ->
let
reduced =
Json.Decode.Exploration.stripString decoder rawResponse
|> Result.withDefault "TODO"
in
rawResponse
|> Json.Decode.Exploration.decodeString decoder
-- |> Result.mapError Json.Decode.Exploration.errorsToString
|> (\decodeResult ->
case decodeResult |> Debug.log "decodeResult" of
Json.Decode.Exploration.BadJson ->
Pages.StaticHttpRequest.DecoderError "" |> Err
Json.Decode.Exploration.Errors errors ->
Pages.StaticHttpRequest.DecoderError "" |> Err
Json.Decode.Exploration.WithWarnings warnings a ->
-- Pages.StaticHttpRequest.DecoderError "" |> Err
Ok a
Json.Decode.Exploration.Success a ->
Ok a
)
-- |> Result.mapError Pages.StaticHttpRequest.DecoderError
|> Result.map Done
|> Result.map (\finalRequest -> ( strippedResponses |> Dict.insert url reduced, finalRequest ))
)
)
{-| TODO
-}
jsonRequestWithSecrets : (Secrets -> Result BuildError String) -> Decoder a -> Request a

View File

@ -5,6 +5,7 @@ import Dict exposing (Dict)
import Expect
import Html
import Json.Decode as Decode
import Json.Decode.Exploration as Reduce
import Pages.ContentCache as ContentCache
import Pages.Document as Document
import Pages.ImagePath as ImagePath
@ -142,6 +143,37 @@ all =
}
]
)
, only <|
test "reduced JSON is sent out" <|
\() ->
start
[ ( []
, StaticHttp.reducedJsonRequest "https://api.github.com/repos/dillonkearns/elm-pages" (Reduce.field "stargazer_count" Reduce.int)
)
]
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86, "unused_field": 123 }"""
|> ProgramTest.expectOutgoingPortValues
"toJsPort"
(Codec.decoder Main.toJsCodec)
(Expect.equal
[ Main.Success
{ pages =
Dict.fromList
[ ( "/"
, Dict.fromList
[ ( "https://api.github.com/repos/dillonkearns/elm-pages"
, """{"stargazer_count":86}"""
)
]
)
]
, manifest = manifest
}
]
)
, test "the port sends out even if there are no http requests" <|
\() ->
start