Skip to content

upload_file respects multiple argument in fileInput (unlike browsers) #370

@TymekDev

Description

@TymekDev

Context

Just recently I have learned that multiple = TRUE in fileInput is just a hint and is entirely enforced by the browser. For example, here is a short example of bypassing this limitation by dragging files:

CleanShot.2023-12-13.at.16.46.37.mp4

I have implemented a server-side validation to ensure that user uploads only a single file. If there is more than one file, then an error message is displayed. I want to write a test using {shinytest2} to check if a correct message is displayed.

Issue

Given a fileInput("f", multiple = FALSE) and the following {shinytest2} code only first file will make it to input$f during testing.

library(shinytest2)
app <- AppDriver$new()
app$upload_file(f = c("file1", "file2"))

However, if we provide multiple = TRUE, then both files will appear in input$f.

Reprex

tymek@maczek ~/w/r/reprex > Rscript test-shinytest2.R
Loading required package: testthat
   name size type datapath
1 file1    0             0

tymek@maczek ~/w/r/reprex > MULTIPLE=TRUE Rscript test-shinytest2.R
Loading required package: testthat
   name size type datapath
1 file1    0             0
2 file2    0             1

app.R

library(shiny)

shinyApp(
  tagList(
    fileInput("f", "", multiple = as.logical(Sys.getenv("MULTIPLE", FALSE))),
    verbatimTextOutput("t")
  ),
  function(input, output, session) {
    output$t <- renderPrint({
      print(input$f)
    })
  }
)

test-shinytest2.R

library(shinytest2)

app <- AppDriver$new(".")
app$upload_file(f = c("~/Documents/untitled folder/file1", "~/Documents/untitled folder/file2"))
print(app$get_value(input = "f"))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions