avh4 / elm-program-test / SimulatedEffect.Ports

This module provides functions that allow you to create SimulatedEffects that parallel Elm ports used in your real program. This is meant to be used to help you implement the function to provide when using ProgramTest.withSimulatedEffects and ProgramTest.withSimulatedSubscriptions.

For a detailed example, see the “Testing programs with ports” guidebook.

send : String -> Json.Encode.Value -> ProgramTest.SimulatedEffect msg

Creates a SimulatedEffect that parallels using an outgoing Elm port.

For example, if your production code uses a port like this:

port logMessage : String -> Cmd msg

logMessage "hello"

Then the corresponding SimulatedEffect would be:

SimulatedEffect.Ports.send "logMessage" (Json.Encode.string "hello")

subscribe : String -> Json.Decode.Decoder a -> (a -> msg) -> ProgramTest.SimulatedSub msg

Creates a SimulatedSub that parallels using an incoming Elm port.

For example, if your production code uses a port like this:

port activeUsers : (List String -> msg) -> Sub msg

subscriptions : Model -> Sub Msg
subscriptions model =
    activeUsers OnActiveUsersLoaded

Then the corresponding SimulatedSub would be:

simulatedSubscriptions : Model -> SimulatedSub Msg
simulatedSubscriptions model =
    SimulatedEffect.Ports.subscribe
        "activeUsers"
        (Json.Decode.list Json.Decode.string)
        OnActiveUsersLoaded