Prepare some boilerplate for elm-program-test.

This commit is contained in:
Dillon Kearns 2019-10-21 18:49:46 -07:00
parent dee8761b8e
commit c2f8b5253a
3 changed files with 182 additions and 73 deletions

View File

@ -32,6 +32,8 @@
"noahzgordon/elm-color-extra": "1.0.2 <= v < 2.0.0"
},
"test-dependencies": {
"elm-explorations/test": "1.2.2 <= v < 2.0.0"
"avh4/elm-program-test": "3.1.0 <= v < 4.0.0",
"elm-explorations/test": "1.2.2 <= v < 2.0.0",
"jgrenat/elm-html-test-runner": "1.0.3 <= v < 2.0.0"
}
}

View File

@ -1,4 +1,15 @@
module Pages.Internal.Platform.Cli exposing (Content, Flags, Model, Msg, Page, Parser, cliApplication)
module Pages.Internal.Platform.Cli exposing
( Content
, Effect
, Flags
, Model
, Msg
, Page
, Parser
, cliApplication
, init
, update
)
import Browser.Navigation
import Dict exposing (Dict)
@ -17,6 +28,10 @@ import Pages.StaticHttpRequest as StaticHttpRequest
import Url exposing (Url)
type Effect
= None
type alias Page metadata view pathKey =
{ metadata : metadata
, path : PagePath pathKey
@ -112,77 +127,7 @@ cliApplication cliMsgConstructor narrowMsg toModel fromModel config =
)
in
Platform.worker
{ init =
\flags ->
( toModel Model
, case contentCache of
Ok _ ->
case contentCache |> ContentCache.pagesWithErrors of
Just pageErrors ->
let
requests =
siteMetadata
|> Result.andThen
(\metadata ->
staticResponseForPage metadata config.view
)
staticResponses : StaticResponses
staticResponses =
case requests of
Ok okRequests ->
staticResponsesInit okRequests
Err errors ->
Dict.empty
in
config.toJsPort
(Json.Encode.object
[ ( "errors", encodeErrors pageErrors )
, ( "manifest", Manifest.toJson config.manifest )
, ( "pages", encodeStaticResponses staticResponses )
]
)
|> Cmd.map never
--(Msg userMsg metadata view)
Nothing ->
let
requests =
siteMetadata
|> Result.andThen
(\metadata ->
staticResponseForPage metadata config.view
)
staticResponses : StaticResponses
staticResponses =
case requests of
Ok okRequests ->
staticResponsesInit okRequests
Err errors ->
Dict.empty
in
Cmd.batch
[ case requests of
Ok okRequests ->
performStaticHttpRequests okRequests
|> Cmd.map cliMsgConstructor
Err errors ->
Cmd.none
]
Err error ->
config.toJsPort
(Json.Encode.object
[ ( "errors", encodeErrors error )
, ( "manifest", Manifest.toJson config.manifest )
]
)
|> Cmd.map never
)
{ init = init toModel contentCache siteMetadata config cliMsgConstructor
, update =
\msg model ->
case narrowMsg msg of
@ -195,6 +140,78 @@ cliApplication cliMsgConstructor narrowMsg toModel fromModel config =
}
init toModel contentCache siteMetadata config cliMsgConstructor () =
( toModel Model
, case contentCache of
Ok _ ->
case contentCache |> ContentCache.pagesWithErrors of
Just pageErrors ->
let
requests =
siteMetadata
|> Result.andThen
(\metadata ->
staticResponseForPage metadata config.view
)
staticResponses : StaticResponses
staticResponses =
case requests of
Ok okRequests ->
staticResponsesInit okRequests
Err errors ->
Dict.empty
in
config.toJsPort
(Json.Encode.object
[ ( "errors", encodeErrors pageErrors )
, ( "manifest", Manifest.toJson config.manifest )
, ( "pages", encodeStaticResponses staticResponses )
]
)
|> Cmd.map never
--(Msg userMsg metadata view)
Nothing ->
let
requests =
siteMetadata
|> Result.andThen
(\metadata ->
staticResponseForPage metadata config.view
)
staticResponses : StaticResponses
staticResponses =
case requests of
Ok okRequests ->
staticResponsesInit okRequests
Err errors ->
Dict.empty
in
Cmd.batch
[ case requests of
Ok okRequests ->
performStaticHttpRequests okRequests
|> Cmd.map cliMsgConstructor
Err errors ->
Cmd.none
]
Err error ->
config.toJsPort
(Json.Encode.object
[ ( "errors", encodeErrors error )
, ( "manifest", Manifest.toJson config.manifest )
]
)
|> Cmd.map never
)
update siteMetadata config msg model =
case msg of
GotStaticHttpResponse { url, response } ->

View File

@ -0,0 +1,90 @@
module StaticHttpRequestsTests exposing (..)
import Dict
import Html
import Json.Decode as Decode
import Pages.ContentCache as ContentCache
import Pages.Document as Document
import Pages.ImagePath as ImagePath
import Pages.Internal.Platform.Cli as Main
import Pages.Manifest as Manifest
import Pages.PagePath as PagePath
import Pages.StaticHttp as StaticHttp
import ProgramTest exposing (ProgramTest)
start : ProgramTest Main.Model Main.Msg Main.Effect
start =
let
document =
Document.fromList []
content =
[]
contentCache =
ContentCache.init document content
siteMetadata =
contentCache
|> Result.map
(\cache -> cache |> ContentCache.extractMetadata PathKey)
|> Result.mapError
(\error ->
error
|> Dict.toList
|> List.map (\( path, errorString ) -> errorString)
)
config =
{ toJsPort = toJsPort
, manifest = manifest
, view =
\allFrontmatter page ->
StaticHttp.withData "https://api.github.com/repos/dillonkearns/elm-pages"
(Decode.field "stargazers_count" Decode.int)
(\staticData ->
{ view =
\model viewForPage ->
{ title = "Title"
, body =
"elm-pages 's: "
++ String.fromInt staticData
|> Html.text
}
, head = []
}
)
}
in
ProgramTest.createDocument
{ init = Main.init identity contentCache siteMetadata config identity
, update = Main.update siteMetadata config
, view = \_ -> { title = "", body = [ Html.text "" ] }
}
-- |> ProgramTest.withSimulatedEffects simulateEffects
|> ProgramTest.start ()
toJsPort foo =
Cmd.none
type PathKey
= PathKey
manifest : Manifest.Config PathKey
manifest =
{ backgroundColor = Nothing
, categories = []
, displayMode = Manifest.Standalone
, orientation = Manifest.Portrait
, description = "elm-pages - A statically typed site generator."
, iarcRatingId = Nothing
, name = "elm-pages docs"
, themeColor = Nothing
, startUrl = PagePath.build PathKey []
, shortName = Just "elm-pages"
, sourceIcon = ImagePath.build PathKey []
}