tests/testthat/test-app-shiny.R

library(shiny)


hello_app <- shinyApp(
  ui = fluidPage(
    textInput("name", "What is your name?"),
    actionButton("greet", "Greet"),
    textOutput("greeting"),
    tags$br(),
    # consistenly sized box and consistent across OS
    tags$div(
      id = "custom_div",
      style = "width: 100px;height: 100px;"
    )
  ),
  server = function(input, output, session) {
    output$greeting <- renderText({
      shiny::req(input$greet)
      paste0("Hello ", shiny::isolate(input$name), "!")
    })
  }
)


test_that("AppDriver can receive a shiny.obj object", {

  app <- AppDriver$new(hello_app, name = "app-shiny_app", expect_values_screenshot_args = FALSE)

  app$set_inputs(name = "Barret")
  app$click("greet")

  expect_true(fs::path_has_parent(app$get_dir(), tempdir()))
})

test_that("AppDriver can receive a shinyAppDir object", {

  ex_app_dir <- shinyAppDir(system.file("examples/01_hello", package = "shiny"))

  app <- AppDriver$new(ex_app_dir, name = "app-shiny_dir", expect_values_screenshot_args = FALSE)

  app$set_inputs(bins = 20)

  il <- rlang::list2(bins = 15)
  app$set_inputs(!!!il)

  expect_true(fs::path_has_parent(app$get_dir(), tempdir()))
})

Try the shinytest2 package in your browser

Any scripts or data that you put into this service are public.

shinytest2 documentation built on June 24, 2024, 5:16 p.m.