use_shiny_testers: Use Shiny Testers

View source: R/use_shiny_testers.R

use_shiny_testersR Documentation

Use Shiny Testers

Description

Enable 'update' functions in the Shiny or Shiny extension package to be mocked in tests.

Usage

use_shiny_testers(..., .package = "shiny", .env = rlang::caller_env())

with_shiny_testers(code, ..., .package = "shiny")

Arguments

...

Arguments passed to create_test_update_fns

.package

Character string of the package that the update functions exist in. Default is '"shiny"'

.env

Environment that defines effect scope. For expert use only.

code

Code to execute with specified bindings.

Value

Implicit return of the updated functions in the supplied package within the specified environment.

Examples

library(shiny)
library(testthat)

example_server_fn <- function(input, output, session) {
  observeEvent(input$trigger, {
      updateTextInput(
      inputId = "result",
      label = "New Label",
      value = NULL,
      placeholder = "New placeholder"
    )
  })
}

test_that("Check that text input gets updated", {
  use_shiny_testers()

  shiny::testServer(
    app = example_server_fn,
    expr = {
      session$setInputs(result = "Example text")
      session$setInputs(trigger = 1L)

      expect_identical(input$result, "Example text")
      expect_identical(input$result.label, "New Label")
      expect_identical(input$result.placeholder, "New placeholder")
    }
  )
})


shinytesters documentation built on Sept. 1, 2025, 5:10 p.m.