avh4/elm-program-test/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

TestContext

A TestContext simulates the execution of an Elm program.

Creating

type TestContext msg model effect

A TestContext represents an Elm program, a current state for that program, and a log of any errors that have occurred while simulating interaction with the program.

{ init : ( model, effect )
, update : msg -> model -> ( model, effect )
, view : model -> Html msg
}
-> TestContext msg model effect

Creates a TestContext from the parts of a standard Html.program.

See other create* functions below if the program you want to test uses flags or nagivation.

{ init : flags -> ( model, effect )
, update : msg -> model -> ( model, effect )
, view : model -> Html msg
}
-> flags
-> TestContext msg model effect

Creates a TestContext from the parts of a standard with flags (Html.programWithFlags).

If your program uses Json.Encode.Value as its flags type, you may find createWithJsonStringFlags useful.

If your program does not use flags, see create.

See other create* functions below if the program you want to test uses nagivation.

Decoder flags
-> { init : flags -> ( model, effect )
, update : msg -> model -> ( model, effect )
, view : model -> Html msg
}
-> String
-> TestContext msg model effect

A simplified way to create a TestContext for a program that decodes its flags with a JSON decoder.

If your program does not use Json.Encode.Value as its flags type, or you want more control over how the flags are provided in your tests, see createWithFlags.

If your program does not use flags, see create.

See other create* functions below if the program you want to test uses nagivation.

(Location -> msg)
-> { init : Location -> ( model, effect )
, update : msg -> model -> ( model, effect )
, view : model -> Html msg
}
-> String
-> TestContext msg model effect

Creates a TestContext from the parts of a Navigation.program.

See other create* functions above and below if the program you want to test uses flags or does not use nagivation.

(Location -> msg)
-> { init : flags -> Location -> ( model, effect )
, update : msg -> model -> ( model, effect )
, view : model -> Html msg
}
-> String
-> flags
-> TestContext msg model effect

Creates a TestContext from the parts of a program with navigation and flags (Navigation.programWithFlags).

If your program uses Json.Encode.Value as its flags type, you may find createWithNavigationAndJsonStringFlags useful.

If your program does not use flags, see createWithNavigation.

If your program does not use navigation, see createWithFlags.

Decoder flags
-> (Location -> msg)
-> { init : flags -> Location -> ( model, effect )
, update : msg -> model -> ( model, effect )
, view : model -> Html msg
}
-> String
-> String
-> TestContext msg model effect

A simplified way to create a TestContext for a program with navigation that decodes its flags with a JSON decoder.

If your program does not use Json.Encode.Value as its flags type, or you want more control over how the flags are provided in your tests, see createWithNavigationAndFlags.

If your program does not use flags, see createWithNavigation.

If your program does not use navigation, see createWithJsonStringFlags.

Simulating user input

String
-> TestContext msg model effect
-> TestContext msg model effect

Simulates clicking a button.

Currently, this function will find and click a <button> HTML node containing the given buttonText.

NOTE: In the future, this function will be generalized to find buttons with accessibility attributes matching the given buttonText.

(Single msg -> Single msg)
-> ( String, Value )
-> TestContext msg model effect
-> TestContext msg model effect

Simulates a custom DOM event.

NOTE: If there is another, more specific function (see Simulating user input that does what you want, prefer that instead, as you will get the benefit of better error messages.

Parameters:

  • findTarget: A function to find the HTML element that responds to the event (typically this will be a call to Test.Html.Query.find [ ...some selector... ])
  • ( eventName, eventValue ): The event to simulate (see Test.Html.Event "Event Builders")
String
-> TestContext msg model effect
-> TestContext msg model effect

url may be an absolute URL or relative URL

Directly sending Msgs

update : msg -> TestContext msg model effect -> TestContext msg model effect

Advances the state of the TestContext's program by using the TestContext's program's update function with the given msg.

This can be used to simulate events that can only be triggered by commands (Cmd) and subscriptions (Sub) (i.e., that cannot be triggered by user interaction with the view).

NOTE: When possible, you should prefer Simulating user input, as doing so will make your tests more robust to changes in your program's implementation details.

Final assertions

expectViewHas : List Selector -> TestContext msg model effect -> Expectation

A simpler way to assert that a TestContext's view matches a given selector.

expectViewHas [...selector...] is the same as expectView (Test.Html.Query.has [...selector...]).

(Single msg -> Expectation)
-> TestContext msg model effect
-> Expectation

Makes an assertion about the current state of a TestContext's view.

(effect -> Expectation)
-> TestContext msg model effect
-> Expectation

Makes an assertion about the last effect produced by a TestContext's program.

expectModel : (model -> Expectation) -> TestContext msg model effect -> Expectation

Make an assertion about the current state of a TestContext's model.

Intermediate assertions

These functions can be used to make assertions on a TestContext without ending the test.

List Selector
-> TestContext msg model effect
-> TestContext msg model effect

shouldHave [...selector...] is equivalent to shouldHaveView (Test.Html.Query.has [...selector...])

List Selector
-> TestContext msg model effect
-> TestContext msg model effect

shouldNotHave [...selector...] is equivalent to shouldHaveView (Test.Html.Query.hasNot [...selector...])

(Single msg -> Expectation)
-> TestContext msg model effect
-> TestContext msg model effect

Validates the the current state of a TestContext's view without ending the TestContext.

(effect -> Expectation)
-> TestContext msg model effect
-> TestContext msg model effect

Validates the last effect produced by a TestContext's program without ending the TestContext.

To end a TestContext without using a final assertion, use the following function:

done : TestContext msg model effect -> Expectation

Ends a TestContext, reporting any errors that occurred.

NOTE: You should prefer using a final assertion to end your test over using done, as doing so will make the intent of your test more clear.

Custom assertions

These functions may be useful if you are writing your own custom assertion functions.

String
-> String
-> TestContext msg model effect
-> TestContext msg model effect

fail can be used to report custom errors if you are writing your own convenience functions to deal with test contexts.

Example (this is a function that checks for a particular structure in the program's view, but will also fail the TestContext if the expectedCount parameter is invalid):

expectNotificationCount : Int -> TestContext Msg Model effect -> TestContext Msg Model effect
expectNotificationCount expectedCount testContext =
    if expectedCount <= 0 then
        testContext
            |> TestContext.fail "expectNotificationCount"
                ("expectedCount must be positive, but was: " ++ toString expectedCount)
    else
        testContext
            |> shouldHave
                [ Test.Html.Selector.class "notifications"
                , Test.Html.Selector.text (toString expectedCount)
                ]