Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(testServer): Update example to simulate button click #3855

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions R/test-server.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,43 @@
#'
#'
#' # Testing a module --------------------------------------------------------
#' myModuleServer <- function(id, multiplier = 2, prefix = "I am ") {
#' # Testing the server function doesn't require a UI, but we've included it
#' # here for completeness. In this simple app, a user clicks a button to
#' # multiply a value by the module's multiplier argument. In the tests below,
#' # we'll make sure the value is 1, 2, 4, etc. with each button click.
#' multModuleUI <- function(id) {
#' ns <- NS(id)
#' tagList(
#' textOutput(ns("txt")),
#' actionButton(ns("multiply_it"), "Multiply It")
#' )
#' }
#'
#' multModuleServer <- function(id, multiplier = 2) {
#' moduleServer(id, function(input, output, session) {
#' myreactive <- reactive({
#' input$x * multiplier
#' the_value <- reactive({
#' max(input$multiply_it * multiplier, 1)
#' })
#' output$txt <- renderText({
#' paste0(prefix, myreactive())
#' paste("The value is", the_value())
#' })
#' })
#' }
#'
#' testServer(myModuleServer, args = list(multiplier = 2), {
#' session$setInputs(x = 1)
#' # You're also free to use third-party
#' # testing packages like testthat:
#' # expect_equal(myreactive(), 2)
#' stopifnot(myreactive() == 2)
#' stopifnot(output$txt == "I am 2")
#' testServer(multModuleServer, args = list(multiplier = 2), {
#' # Set the initial button value to 0
#' session$setInputs(multiply_it = 0)
#' stopifnot(the_value() == 1)
#' stopifnot(output$txt == "The value is 1")
#'
#' session$setInputs(x = 2)
#' stopifnot(myreactive() == 4)
#' stopifnot(output$txt == "I am 4")
#' # Any additional arguments, below, are passed along to the module.
#' # Simulate two button clicks
#' session$setInputs(multiply_it = 2)
#' stopifnot(the_value() == 4)
#' stopifnot(output$txt == "The value is 4")
#'
#' # Note: you're also free to use third-party
#' # testing packages like testthat:
#' # expect_equal(myreactive(), 1)
#' })
#' @export
testServer <- function(app = NULL, expr, args = list(), session = MockShinySession$new()) {
Expand Down
44 changes: 29 additions & 15 deletions man/testServer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.