webbhuset/elm-actor-model/2.0.0

NOTE: the latest version is 4.0.0

Webbhuset.Component.Sandbox

Sandbox

The sandbox module is helpful when developing components. It lets you run the component using elm reactor outside the system and define several test cases.

type alias SandboxProgram model msgIn msgOut =
Program () (Model model) (Msg msgIn msgOut)

The Program type of your main function.

Create Sandbox

Wrap a component in a sandbox application.

This will render each test case and log all messages. Create a test file with a main function where you declare all test cases.

A sandbox module example for YourComponent:

import YourComponent exposing (Model, MsgIn, MsgOut)


main : SandboxProgram Model MsgIn MsgOut
main =
    Sandbox.ui
        { title = "Title of your component"
        , component = YourComponent.component
        , cases =
            [ testCase1
            , testCase2
            ]
        , stringifyMsgIn = Debug.toString -- Or roll your own if you want prettier messages.
        , stringifyMsgOut = Debug.toString
        , wrapView = identity
        }
ui :
{ title : String
, component : UI model msgIn msgOut
, cases : List (TestCase msgIn msgOut)
, stringifyMsgIn : msgIn -> String
, stringifyMsgOut : msgOut -> String
, wrapView : Html msgIn -> Html msgIn
}
-> SandboxProgram model msgIn msgOut

Sandbox a UI Component

{ title : String
, component : Layout model msgIn msgOut (Msg msgIn msgOut)
, cases : List (TestCase msgIn msgOut)
, stringifyMsgIn : msgIn -> String
, stringifyMsgOut : msgOut -> String
, wrapView :
(msgIn -> Msg msgIn msgOut)
-> Html (Msg msgIn msgOut)
-> Html (Msg msgIn msgOut)
}
-> SandboxProgram model msgIn msgOut

Sandbox a Layout Component

{ title : String
, component : Service model msgIn msgOut
, cases : List (TestCase msgIn msgOut)
, view : model -> Html msgIn
, stringifyMsgIn : msgIn -> String
, stringifyMsgOut : msgOut -> String
}
-> SandboxProgram model msgIn msgOut

Sandbox a Service Component

You need to provied a view function which renders the model of your service component.

Create a Test Case

A Test Case is just a record with a title and description together with a list of Actions you want to perform on your sandboxed component. You can also the component's map out messages to actions to simulate the outside system.

type alias TestCase msgIn msgOut =
{ title : String
, desc : String
, init : List (Action msgIn)
, onMsgOut : msgOut -> List (Action msgIn)
}

A test case for the Component

testCase1 : Sandbox.TestCase MsgIn MsgOut
testCase1 =
    { title = "Test Case Title"
    , desc = "Test Case Description"
    , init =
        [ Sandbox.sendMsg YourComponent.Hello
        , Sandbox.spawnChild "Child Title" YourComponent.ReceiveChildPID
        ]
    , onMsgOut = \msgOut ->
        case msgOut of
            YourComponent.ObserveSomething id ->
                [ YourComponent.RecevieDataFor id "Some data"
                    |> Sandbox.sendMsg
                    |> Sandbox.delay 1000
                ]

Actions

type Action msgIn

An action to perform on your sandboxed component.

sendMsg : msgIn -> Action msgIn

Send a message to you sandboxed component

Sandbox.sendMsg YourComponent.SomeMessage
spawnChild : String -> (PID -> msgIn) -> Action msgIn

Spawn a child component and send the PID to your component.

You can provide a String which will be displayed when the child component is rendered (using renderPID in your layout component).

Sandbox.spawnChild "Hello child" YourComponent.ReceiveChild
delay : Float -> Action msgIn -> Action msgIn

Perform a delayed action on your sandboxed component. Delay in milliseconds.

Sandbox.sendMsg YourComponent.SomeMessage
    |> Sandbox.delay 1000
type alias Msg msgIn msgOut =
SysMsg ActorName (AppMsg msgIn msgOut)

Sadbox Msg