Skip to content

Instantly share code, notes, and snippets.

@brian-watkins
brian-watkins / RoleSpec.elm
Created Dec 23, 2020
Example elm-spec spec
View RoleSpec.elm
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
@brian-watkins
brian-watkins / SampleSpec.elm
Last active Oct 31, 2020
Sample spec describing an app that listens for a keydown event
View SampleSpec.elm
module SampleSpec exposing (main)
import Spec exposing (..)
import Spec.Step as Step
import Spec.Setup as Setup
import Spec.Markup as Markup
import Spec.Markup.Selector exposing (..)
import Spec.Markup.Event as Event
import Spec.Claim exposing (isSomethingWhere)
import Spec.Extra exposing (equals)
View di_in_elm_5.elm
executeWith : String -> ScoresTagger msg -> Score -> Cmd msg
executeWith uri tagger score =
let
tagHighScores = highScoreTagger tagger
storeScore = storeScoreTask uri
getScores = getScoresTask uri
in
storeScore score
|> Task.andThen (\_ -> getScores)
|> Task.attempt tagHighScores
View di_in_elm_4.elm
persistScoreTests : Test
persistScoreTests =
describe "when a score is persisted"
[ describe "when the request is successful" <|
let
state =
Headless.givenCommand (\_ -> HttpPersistScore.executeWith "http://fake-server/scores" ScoreTagger 87)
|> Spy.use [ Http.serve [ storeScoreStub 87, scoreRequestStub [ 81, 87, 98, 123 ] ] ]
in
[ test "it POSTs the new score to the score service" <|
View di_in_elm_3.elm
persistScoresTests : Test
persistScoresTests =
describe "when a score is persisted" <|
let
testState =
Headless.givenCommand (\_ -> LocalStoragePersistScore.execute <| 217)
|> Spy.use [ saveScoreSpy, getScoresSpy ]
|> Subscription.with (\() -> (\_ -> LocalStoragePersistScore.subscriptions ScoreTagger))
|> Subscription.send "scores-sub" [ 190, 217, 218, 332 ]
in
View di_in_elm_2.elm
scoresTagger : ScoresTagger Msg
scoresTagger =
ReceivedScores
updateWith : PersistScore Msg -> Msg -> Model -> (Model, Cmd Msg)
updateWith persistScore msg model =
case msg of
GameOver ->
(model, persistScore 4)
ReceivedScores scores ->
View di_in_elm_1.elm
gameOverTests : Test
gameOverTests =
describe "when the game is over" <|
let
state =
Elmer.given App.defaultModel App.view (App.updateWith <| Spy.callable "fake-persist-score")
|> Elmer.Spy.Use [ persistScoreSpy ]
|> Elmer.Html.Events.click "#game-over-button"
in
[ test "it saves the score" <|
View final-app.elm
module App exposing
( Model
, defaultModel
, view
, update
, subscriptions
)
import Html exposing (Html)
import Html.Attributes as Attr
View third-test.elm
module WebSocketTests exposing (..)
import Test exposing (..)
import Expect
import Elmer exposing (atIndex, (<&&>))
import Elmer.Html as Markup
import Elmer.Html.Matchers exposing (elements, hasText)
import Elmer.Spy as Spy exposing (Spy, andCallFake)
import Elmer.Platform.Subscription as Subscription
import WebSocket
View second-app.elm
module App exposing
( Model
, defaultModel
, view
, update
)
import Html exposing (Html)
import Html.Attributes as Attr
import Html.Events as Event