dillonkearns/elm-snapshot/1.1.0

NOTE: the latest version is 1.1.1

Snapshot.ProgramTest

High-level snapshot testing for ProgramTest programs.

module Snapshots exposing (run)


import Html exposing (Html, button, div, span, text)
import Html.Events exposing (onClick)
import Pages.Script exposing (Script)
import ProgramTest exposing (ProgramTest)
import Snapshot
import Snapshot.ProgramTest
import Test.Html.Selector as Selector


startCounterApp : ProgramTest { count : Int } () ()
startCounterApp =
    ProgramTest.createSandbox
        { init = { count = 0 }
        , update = \() model -> { model | count = model.count + 1 }
        , view =
            \model ->
                div []
                    [ button [ onClick () ] [ text "++" ]
                    , span [] [ text (String.fromInt model.count) ]
                    ]
        }
        |> ProgramTest.start ()


run : Script
run =
    Snapshot.run "Snapshots"
        [ Snapshot.ProgramTest.view [] "initial view" <|
            \() -> startCounterApp
        , Snapshot.ProgramTest.view [ Selector.tag "span" ] "counter display" <|
            \() ->
                startCounterApp
                    |> ProgramTest.clickButton "++"
        , Snapshot.ProgramTest.model Debug.toString "model after click" <|
            \() ->
                startCounterApp
                    |> ProgramTest.clickButton "++"
        ]

After running elm-pages run src/Snapshots.elm --approve=all, you will have these files on disk:

initial_view.approved.html:

<div>
    <button>++</button>
    <span>0</span>
</div>

counter_display.approved.html:

<span>1</span>

model_after_click.approved.elm:

{ count = 1 }
view : List Selector -> String -> (() -> ProgramTest model msg effect) -> Test

Create a snapshot test of the rendered view HTML from a ProgramTest.

The first argument is a list of selectors to scope the snapshot to a subtree of the view. Pass an empty list to snapshot the entire view.

Snapshot.ProgramTest.view [] "full view" <|
    \() -> myProgram


Snapshot.ProgramTest.view [ Selector.tag "span" ] "counter display" <|
    \() ->
        myProgram
            |> ProgramTest.clickButton "++"
model : (a -> String) -> String -> (() -> ProgramTest a msg effect) -> Test

Create a snapshot test of the model from a ProgramTest.

The model is pretty-printed as formatted Elm syntax using Printer.elm, producing .elm snapshot files with proper syntax highlighting.

The first argument must be Debug.toString (published packages cannot call it directly, so you must pass it in).

Snapshot.ProgramTest.model Debug.toString "model state" <|
    \() ->
        myProgram
            |> ProgramTest.clickButton "++"