filetestingelm

For testing update functions in Elm that use Files in their Msg, how can I create a File value to pass in test? Can you somehow create a File fuzzer?


For example, I have this model:

import File exposing (File)

type alias Model =
{ selectedFiles : Dict String File
, fileUploadInfoList : List FileUploadInfo
, modalSelectedFile : Maybe File
... 
}

type Msg
= SelectedFiles String (List File)
| Upload Int
| UploadedFiles (Result Http.Error (List FileInfo))
| DownloadedFile String (Result Http.Error Bytes.Bytes)
| DownloadFile FileInfo
| LoadedFiles (Result Http.Error (List FileInfo))
| OpenModal { startFocusOn : String, returnFocusTo : String } FileInfo
| ModalMsg Modal.Msg
| Focus String
| Dismiss
| DismissModal
| SelectModalFile (List File)
| UpdateFile
| UpdatedFile (Result Http.Error FileInfo)

And in update function:

update : Msg -> Model -> { token : String, apiBaseUrl : String } -> ( Model, Cmd Msg )
update msg model apiParams =
case msg of
    SelectedFiles key [ file ] ->
        ( { model | selectedFiles = Dict.insert key file model.selectedFiles }, Cmd.none )
    ... 

When I write tests for this update function how do I call it in the test? The dictionary value is of type File. How do I create a test file? What to put for [ file ] ?


Solution

  • File is an opaque type, meaning that it can't be created outside of the module where it's defined. In this case, there isn't a way to create a File value in a test environment, because the only ways that the package exposes to create a value are via a Cmd or via a Decoder, and that decoder won't work in node.js.

    If you want to be able to test this message, you will need to use a different type in the message. You can move your logic that converts a File into a FileInfo into your event handler, so that the message only deals with FileInfo.

    elm-program-test provides shim types to support testing Cmd values, but does not yet support File.