Created
December 23, 2020 13:40
-
-
Save brian-watkins/bec6351780ee985134609411b233b852 to your computer and use it in GitHub Desktop.
Example elm-spec spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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