Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@brian-watkins
brian-watkins / RoleSpec.elm
Created December 23, 2020 13:40
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
@brian-watkins
brian-watkins / SampleSpec.elm
Last active October 31, 2020 00:04
Sample spec describing an app that listens for a keydown event
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)
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
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" <|
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
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 ->
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" <|
module App exposing
( Model
, defaultModel
, view
, update
, subscriptions
)
import Html exposing (Html)
import Html.Attributes as Attr
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
module App exposing
( Model
, defaultModel
, view
, update
)
import Html exposing (Html)
import Html.Attributes as Attr
import Html.Events as Event