Skip to content

Instantly share code, notes, and snippets.

@brian-watkins
Created December 23, 2020 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brian-watkins/bec6351780ee985134609411b233b852 to your computer and use it in GitHub Desktop.
Save brian-watkins/bec6351780ee985134609411b233b852 to your computer and use it in GitHub Desktop.
Example elm-spec spec
module Fun.RoleSpec exposing (main)
import Spec exposing (..)
import Spec.Setup as Setup
import Spec.Observer as Observer
import Spec.Claim as Claim exposing (specifyThat, isListWithLength, isTrue)
import Spec.Command as Command
import Spec.Report as Report
import Spec.Http
import Spec.Http.Stub as Stub
import Spec.Http.Route exposing (get)
import Runner
import Http
import Json.Encode as Encode
import Role
roleSpec =
describe "roles"
[ scenario "the server says the user is an admin" (
given (
Setup.initWithModel { roleResult = Nothing }
|> Setup.withUpdate testUpdate
|> Stub.serve [ serverStub "fun-user" "admin" ]
)
|> when "the role is requested"
[ Command.send <| Role.requestRole GotRole "fun-user"
]
|> observeThat
[ it "makes a request to fetch the role" (
Spec.Http.observeRequests (get "https://server.com/user/fun-user")
|> expect (isListWithLength 1)
)
, it "allows the user to delete the the database" (
observeUserRole
|> expect (specifyThat Role.canDeleteDatabase isTrue)
)
]
)
]
type TestMsg
= GotRole (Result Http.Error Role.Role)
serverStub userId role =
Stub.for (get <| "https://server.com/user/" ++ userId)
|> Stub.withBody (Stub.withJson (Encode.object [ ("role", Encode.string role) ]))
testUpdate msg model =
case msg of
GotRole roleResult ->
( { model | roleResult = Just roleResult }, Cmd.none )
observeUserRole =
Observer.observeModel (\model ->
model.roleResult
|> Maybe.map (Result.mapError <| always <| Report.note "Http Error!")
)
|> Observer.focus isSomethingWhere
|> Observer.observeResult
main =
Runner.browserProgram
[ roleSpec
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment