Fix test error.

This commit is contained in:
Dillon Kearns 2022-12-29 14:03:10 -07:00
parent 12935c577b
commit ac153f6494

View File

@ -7,6 +7,7 @@ import Codec
import DataSource exposing (DataSource) import DataSource exposing (DataSource)
import DataSource.Http import DataSource.Http
import Dict import Dict
import Exception exposing (Throwable)
import Expect import Expect
import Html import Html
import Json.Decode as JD exposing (Decoder) import Json.Decode as JD exposing (Decoder)
@ -34,7 +35,7 @@ all =
[ test "initial requests are sent out" <| [ test "initial requests are sent out" <|
\() -> \() ->
startSimple [] startSimple []
(DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" starDecoder) (DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" starDecoder |> DataSource.throw)
|> simulateHttp |> simulateHttp
(get "https://api.github.com/repos/dillonkearns/elm-pages") (get "https://api.github.com/repos/dillonkearns/elm-pages")
(JsonBody (JsonBody
@ -45,7 +46,7 @@ all =
\() -> \() ->
startSimple startSimple
[ "post-1" ] [ "post-1" ]
(DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" starDecoder) (DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" starDecoder |> DataSource.throw)
|> simulateHttp |> simulateHttp
(get "https://api.github.com/repos/dillonkearns/elm-pages") (get "https://api.github.com/repos/dillonkearns/elm-pages")
(JsonBody (JsonBody
@ -56,7 +57,7 @@ all =
[ test "single pages that are pre-rendered" <| [ test "single pages that are pre-rendered" <|
\() -> \() ->
startSimple [ "post-1" ] startSimple [ "post-1" ]
(DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" starDecoder) (DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" starDecoder |> DataSource.throw)
|> simulateHttp |> simulateHttp
(get "https://api.github.com/repos/dillonkearns/elm-pages") (get "https://api.github.com/repos/dillonkearns/elm-pages")
(JsonBody (JsonBody
@ -85,9 +86,11 @@ all =
(DataSource.map2 Tuple.pair (DataSource.map2 Tuple.pair
(DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages"
(JD.field "stargazer_count" JD.int) (JD.field "stargazer_count" JD.int)
|> DataSource.throw
) )
(DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages"
(JD.field "language" JD.string) (JD.field "language" JD.string)
|> DataSource.throw
) )
) )
|> simulateHttp |> simulateHttp
@ -105,9 +108,11 @@ all =
startSimple startSimple
[ "elm-pages" ] [ "elm-pages" ]
(DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (JD.succeed ()) (DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (JD.succeed ())
|> DataSource.throw
|> DataSource.andThen |> DataSource.andThen
(\_ -> (\_ ->
DataSource.Http.get "NEXT-REQUEST" (JD.succeed ()) DataSource.Http.get "NEXT-REQUEST" (JD.succeed ())
|> DataSource.throw
) )
) )
|> simulateHttp |> simulateHttp
@ -225,7 +230,9 @@ all =
, test "reduced JSON is sent out" <| , test "reduced JSON is sent out" <|
\() -> \() ->
startSimple [] startSimple []
(DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (JD.field "stargazer_count" JD.int)) (DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (JD.field "stargazer_count" JD.int)
|> DataSource.throw
)
|> simulateHttp |> simulateHttp
(get "https://api.github.com/repos/dillonkearns/elm-pages") (get "https://api.github.com/repos/dillonkearns/elm-pages")
(jsonBody """{ "stargazer_count": 86, "unused_field": 123 }""") (jsonBody """{ "stargazer_count": 86, "unused_field": 123 }""")
@ -242,6 +249,7 @@ all =
(DataSource.Http.expectJson (DataSource.Http.expectJson
(JD.field "stargazer_count" JD.int) (JD.field "stargazer_count" JD.int)
) )
|> DataSource.throw
) )
|> simulateHttp |> simulateHttp
(get "https://api.github.com/repos/dillonkearns/elm-pages") (get "https://api.github.com/repos/dillonkearns/elm-pages")
@ -257,6 +265,7 @@ all =
, body = DataSource.Http.emptyBody , body = DataSource.Http.emptyBody
} }
DataSource.Http.expectString DataSource.Http.expectString
|> DataSource.throw
) )
|> simulateHttp |> simulateHttp
{ method = "GET" { method = "GET"
@ -278,6 +287,7 @@ all =
, body = DataSource.Http.emptyBody , body = DataSource.Http.emptyBody
} }
DataSource.Http.expectString DataSource.Http.expectString
|> DataSource.throw
|> DataSource.map |> DataSource.map
(\string -> (\string ->
if String.toUpper string == string then if String.toUpper string == string then
@ -286,7 +296,7 @@ all =
else else
Err "String was not uppercased" Err "String was not uppercased"
) )
|> DataSource.andThen DataSource.fromResult |> DataSource.andThen (\result -> result |> Result.mapError Exception.fromString |> DataSource.fromResult)
) )
|> simulateHttp |> simulateHttp
(get "https://example.com/file.txt") (get "https://example.com/file.txt")
@ -313,6 +323,7 @@ I ran into a call to `DataSource.fail` with message: String was not uppercased""
(DataSource.Http.expectJson (DataSource.Http.expectJson
(JD.field "stargazer_count" JD.int) (JD.field "stargazer_count" JD.int)
) )
|> DataSource.throw
) )
|> simulateHttp |> simulateHttp
(post "https://api.github.com/repos/dillonkearns/elm-pages") (post "https://api.github.com/repos/dillonkearns/elm-pages")
@ -326,6 +337,7 @@ I ran into a call to `DataSource.fail` with message: String was not uppercased""
(\_ -> (\_ ->
DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages-starter" (JD.field "stargazer_count" JD.int) DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages-starter" (JD.field "stargazer_count" JD.int)
) )
|> DataSource.throw
) )
|> simulateHttp |> simulateHttp
(get "https://api.github.com/repos/dillonkearns/elm-pages") (get "https://api.github.com/repos/dillonkearns/elm-pages")
@ -338,8 +350,8 @@ I ran into a call to `DataSource.fail` with message: String was not uppercased""
\() -> \() ->
startSimple [] startSimple []
(DataSource.map2 (\_ _ -> ()) (DataSource.map2 (\_ _ -> ())
(DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (JD.field "stargazer_count" JD.int)) (DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (JD.field "stargazer_count" JD.int) |> DataSource.throw)
(DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages-starter" (JD.field "stargazer_count" JD.int)) (DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages-starter" (JD.field "stargazer_count" JD.int) |> DataSource.throw)
) )
|> simulateMultipleHttp |> simulateMultipleHttp
[ ( get "https://api.github.com/repos/dillonkearns/elm-pages" [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
@ -362,8 +374,8 @@ I ran into a call to `DataSource.fail` with message: String was not uppercased""
\() -> \() ->
startSimple [] startSimple []
(DataSource.map2 (\_ _ -> ()) (DataSource.map2 (\_ _ -> ())
(DataSource.Http.get "http://example.com" (JD.succeed ())) (DataSource.Http.get "http://example.com" (JD.succeed ()) |> DataSource.throw)
(DataSource.Http.get "http://example.com" (JD.succeed ())) (DataSource.Http.get "http://example.com" (JD.succeed ()) |> DataSource.throw)
) )
|> simulateHttp |> simulateHttp
(get "http://example.com") (get "http://example.com")
@ -372,7 +384,7 @@ I ran into a call to `DataSource.fail` with message: String was not uppercased""
, test "an error is sent out for decoder failures" <| , test "an error is sent out for decoder failures" <|
\() -> \() ->
startSimple [ "elm-pages" ] startSimple [ "elm-pages" ]
(DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (JD.fail "The user should get this message from the CLI.")) (DataSource.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (JD.fail "The user should get this message from the CLI.") |> DataSource.throw)
|> simulateHttp |> simulateHttp
(get "https://api.github.com/repos/dillonkearns/elm-pages") (get "https://api.github.com/repos/dillonkearns/elm-pages")
(jsonBody """{ "stargazer_count": 86 }""") (jsonBody """{ "stargazer_count": 86 }""")
@ -397,14 +409,14 @@ type Route
= Route String = Route String
start : List ( List String, DataSource a ) -> ProgramTest (Model Route) Msg Effect start : List ( List String, DataSource Throwable a ) -> ProgramTest (Model Route) Msg Effect
start pages = start pages =
startWithHttpCache [] pages startWithHttpCache [] pages
startWithHttpCache : startWithHttpCache :
List ( Request.Request, String ) List ( Request.Request, String )
-> List ( List String, DataSource a ) -> List ( List String, DataSource Throwable a )
-> ProgramTest (Model Route) Msg Effect -> ProgramTest (Model Route) Msg Effect
startWithHttpCache = startWithHttpCache =
startLowLevel [] startLowLevel []
@ -413,7 +425,7 @@ startWithHttpCache =
startLowLevel : startLowLevel :
List (ApiRoute.ApiRoute ApiRoute.Response) List (ApiRoute.ApiRoute ApiRoute.Response)
-> List ( Request.Request, String ) -> List ( Request.Request, String )
-> List ( List String, DataSource a ) -> List ( List String, DataSource Throwable a )
-> ProgramTest (Model Route) Msg Effect -> ProgramTest (Model Route) Msg Effect
startLowLevel apiRoutes staticHttpCache pages = startLowLevel apiRoutes staticHttpCache pages =
let let
@ -489,12 +501,12 @@ site =
} }
startSimple : List String -> DataSource a -> ProgramTest (Model Route) Msg Effect startSimple : List String -> DataSource Throwable a -> ProgramTest (Model Route) Msg Effect
startSimple route dataSources = startSimple route dataSources =
startWithRoutes route [ route ] [] [ ( route, dataSources ) ] startWithRoutes route [ route ] [] [ ( route, dataSources ) ]
config : List (ApiRoute.ApiRoute ApiRoute.Response) -> List ( List String, DataSource a ) -> ProgramConfig Msg () Route () () () Effect mappedMsg () config : List (ApiRoute.ApiRoute ApiRoute.Response) -> List ( List String, DataSource Throwable a ) -> ProgramConfig Msg () Route () () () Effect mappedMsg ()
config apiRoutes pages = config apiRoutes pages =
{ toJsPort = toJsPort { toJsPort = toJsPort
, fromJsPort = fromJsPort , fromJsPort = fromJsPort
@ -515,7 +527,7 @@ config apiRoutes pages =
, data = , data =
\_ (Route pageRoute) -> \_ (Route pageRoute) ->
let let
thing : Maybe (DataSource a) thing : Maybe (DataSource Throwable a)
thing = thing =
pages pages
|> Dict.fromList |> Dict.fromList
@ -535,7 +547,7 @@ config apiRoutes pages =
, view = , view =
\_ _ _ page _ _ _ _ -> \_ _ _ page _ _ _ _ ->
let let
thing : Maybe (DataSource a) thing : Maybe (DataSource Throwable a)
thing = thing =
pages pages
|> Dict.fromList |> Dict.fromList
@ -569,7 +581,7 @@ config apiRoutes pages =
, notFoundRoute = Route "not-found" , notFoundRoute = Route "not-found"
, internalError = \_ -> () , internalError = \_ -> ()
, errorPageToData = \_ -> () , errorPageToData = \_ -> ()
, action = \_ _ -> DataSource.fail "No action." , action = \_ _ -> DataSource.fail (Exception.fromString "No action.")
, encodeAction = \_ -> Bytes.Encode.signedInt8 0 , encodeAction = \_ -> Bytes.Encode.signedInt8 0
} }
@ -578,7 +590,7 @@ startWithRoutes :
List String List String
-> List (List String) -> List (List String)
-> List ( Request.Request, String ) -> List ( Request.Request, String )
-> List ( List String, DataSource a ) -> List ( List String, DataSource Throwable a )
-> ProgramTest (Model Route) Msg Effect -> ProgramTest (Model Route) Msg Effect
startWithRoutes pageToLoad _ staticHttpCache pages = startWithRoutes pageToLoad _ staticHttpCache pages =
let let