brian-watkins / elm-spec / Spec.File

Observe and make claims about files during a spec.

Here's an exmaple that describes selecting a text file, and downloading its content:

Spec.describe "modifying a file"
[ Spec.scenario "a text file" (
    Spec.given (
      Spec.Setup.init (App.init testFlags)
        |> Spec.Setup.withView App.view
        |> Spec.Setup.withUpdate App.update
    )
    |> Spec.when "a file is selected"
      [ Spec.Markup.target << by [ id "file-input" ]
      , Spec.Markup.Event.click
      , Spec.File.select
        [ Spec.File.withText "my-file.txt" "Some text content!"
        ]
      ]
    |> Spec.when "the modified version is downloaded"
      [ Spec.Markup.target << by [ id "download-button" ]
      , Spec.Markup.Event.click
      ]
    |> Spec.it "downloads the expected content" (
      Spec.File.observeDownloads
        |> Spec.expect (Spec.Claim.isListWhere
          [ Spec.File.text <|
              Spec.Claim.isStringContaining 1 "Some text content!"
          ]
        )
    )
  )
]

Select Files


type FileFixture

Represents a file.

select : List FileFixture -> Spec.Step.Step model msg

A step that selects a file as input.

Spec.when "a File is uploaded"
  [ Spec.Markup.target << by [ tag "input", attribute ("type", "file") ]
  , Spec.Markup.Event.click
  , Spec.File.select
    [ Spec.File.atPath "./fixtures/myFile.txt"
    ]
  ]

A previous step must open a file selector, either by clicking an input element of type file or by taking some action that results in a File.Select.file command.

You may select multiple files.

atPath : String -> FileFixture

Create a FileFixture by loading a file from the local filesystem.

The path is typically relative to the current working directory of the elm-spec runner (but check the docs for the runner you are using).

withBytes : String -> Bytes -> FileFixture

Create a FileFixture with the given name and bytes.

withText : String -> String -> FileFixture

Create a FileFixture with the given name and text content.

withMimeType : String -> FileFixture -> FileFixture

Update a FileFixture to have the given MIME type.

For example, create a PNG FileFixture like so:

Spec.File.atPath "./fixtures/my-image.png"
  |> Spec.File.withMimeType "image/png"

withLastModified : Basics.Int -> FileFixture -> FileFixture

Update a FileFixture to have the given last modified date, specified in milliseconds since the UNIX epoch.

Observe Downloads


type Download

Represents a file downloaded in the course of a scenario.

observeDownloads : Spec.Observer.Observer model (List Download)

Observe downloads that occurred during a scenario.

For example, here's a claim about the name of a downloaded file:

Spec.it "names the downloaded file as expected" (
  Spec.File.observeDownloads
    |> Spec.expect (Spec.Claim.isListWhere
      [ Spec.File.name <|
          Spec.Claim.isStringContaining 1 "cool-file.txt"
      ]
    )
)

Make Claims about Downloads

name : Spec.Claim.Claim String -> Spec.Claim.Claim Download

Claim that the name of a downloaded file satisfies the given claim.

text : Spec.Claim.Claim String -> Spec.Claim.Claim Download

Claim that the text content of a downloaded file satisfies the given claim.

Note that this claim will fail if the download was created by downloading a URL.

bytes : Spec.Claim.Claim Bytes -> Spec.Claim.Claim Download

Claim that the bytes of a downloaded file satisfy the given claim.

Note that this claim will fail if the download was created by downloading a URL.

downloadedUrl : Spec.Claim.Claim String -> Spec.Claim.Claim Download

Claim that the downloaded URL satisfies the given claim.

Note that this claim will fail if the download was not created by downloading a URL.