| 1 |
#' Reactive testing for Shiny server functions and modules |
| 2 |
#' |
| 3 |
#' A way to test the reactive interactions in Shiny applications. Reactive |
| 4 |
#' interactions are defined in the server function of applications and in |
| 5 |
#' modules. |
| 6 |
#' @param app A server function (i.e. a function with `input`, `output`, |
| 7 |
#' and `session`), or a module function (i.e. a function with first |
| 8 |
#' argument `id` that calls [moduleServer()]. |
| 9 |
#' |
| 10 |
#' You can also provide an app, a path an app, or anything that |
| 11 |
#' [`as.shiny.appobj()`] can handle. |
| 12 |
#' @param expr Test code containing expectations. The objects from inside the |
| 13 |
#' server function environment will be made available in the environment of |
| 14 |
#' the test expression (this is done using a data mask with |
| 15 |
#' [rlang::eval_tidy()]). This includes the parameters of the server function |
| 16 |
#' (e.g. `input`, `output`, and `session`), along with any other values |
| 17 |
#' created inside of the server function. |
| 18 |
#' @param args Additional arguments to pass to the module function. If `app` is |
| 19 |
#' a module, and no `id` argument is provided, one will be generated and |
| 20 |
#' supplied automatically. |
| 21 |
#' @param session The [`MockShinySession`] object to use as the [reactive |
| 22 |
#' domain][shiny::domains]. The same session object is used as the domain both |
| 23 |
#' during invocation of the server or module under test and during evaluation |
| 24 |
#' of `expr`. |
| 25 |
#' @include mock-session.R |
| 26 |
#' @rdname testServer |
| 27 |
#' @examples |
| 28 |
#' # Testing a server function ---------------------------------------------- |
| 29 |
#' server <- function(input, output, session) { |
| 30 |
#' x <- reactive(input$a * input$b) |
| 31 |
#' } |
| 32 |
#' |
| 33 |
#' testServer(server, { |
| 34 |
#' session$setInputs(a = 2, b = 3) |
| 35 |
#' stopifnot(x() == 6) |
| 36 |
#' }) |
| 37 |
#' |
| 38 |
#' |
| 39 |
#' # Testing a module -------------------------------------------------------- |
| 40 |
#' myModuleServer <- function(id, multiplier = 2, prefix = "I am ") { |
| 41 |
#' moduleServer(id, function(input, output, session) { |
| 42 |
#' myreactive <- reactive({ |
| 43 |
#' input$x * multiplier |
| 44 |
#' }) |
| 45 |
#' output$txt <- renderText({ |
| 46 |
#' paste0(prefix, myreactive()) |
| 47 |
#' }) |
| 48 |
#' }) |
| 49 |
#' } |
| 50 |
#' |
| 51 |
#' testServer(myModuleServer, args = list(multiplier = 2), { |
| 52 |
#' session$setInputs(x = 1) |
| 53 |
#' # You're also free to use third-party |
| 54 |
#' # testing packages like testthat: |
| 55 |
#' # expect_equal(myreactive(), 2) |
| 56 |
#' stopifnot(myreactive() == 2) |
| 57 |
#' stopifnot(output$txt == "I am 2") |
| 58 |
#' |
| 59 |
#' session$setInputs(x = 2) |
| 60 |
#' stopifnot(myreactive() == 4) |
| 61 |
#' stopifnot(output$txt == "I am 4") |
| 62 |
#' # Any additional arguments, below, are passed along to the module. |
| 63 |
#' }) |
| 64 |
#' @export |
| 65 |
testServer <- function(app = NULL, expr, args = list(), session = MockShinySession$new()) { |
| 66 |
require(shiny) |
| 67 |
|
| 68 |
if (!is.null(getDefaultReactiveDomain())) |
| 69 |
stop("testServer() is for use only within tests and may not indirectly call itself.") |
| 70 |
|
| 71 |
on.exit(if (!session$isClosed()) session$close(), add = TRUE) |
| 72 |
quosure <- rlang::enquo(expr) |
| 73 |
|
| 74 |
if (isModuleServer(app)) { |
| 75 |
if (!("id" %in% names(args))) |
| 76 |
args[["id"]] <- session$genId() |
| 77 |
# app is presumed to be a module, and modules may take additional arguments, |
| 78 |
# so splice in any args. |
| 79 |
withMockContext(session, rlang::exec(app, !!!args)) |
| 80 |
|
| 81 |
# If app is a module, then we must use both the module function's immediate |
| 82 |
# environment and also its enclosing environment to construct the mask. |
| 83 |
parent_clone <- rlang::env_clone(parent.env(session$env)) |
| 84 |
clone <- rlang::env_clone(session$env, parent_clone) |
| 85 |
mask <- rlang::new_data_mask(clone, parent_clone) |
| 86 |
withMockContext(session, rlang::eval_tidy(quosure, mask, rlang::caller_env())) |
| 87 |
return(invisible()) |
| 88 |
} |
| 89 |
|
| 90 |
if (is.null(app)) { |
| 91 |
path <- findEnclosingApp(".") |
| 92 |
app <- shinyAppDir(path) |
| 93 |
} else if (isServer(app)) { |
| 94 |
app <- shinyApp(fluidPage(), app) |
| 95 |
} else { |
| 96 |
app <- as.shiny.appobj(app) |
| 97 |
} |
| 98 |
|
| 99 |
if (!is.null(app$onStart)) |
| 100 |
app$onStart() |
| 101 |
if (!is.null(app$onStop)) |
| 102 |
on.exit(app$onStop(), add = TRUE) |
| 103 |
|
| 104 |
server <- app$serverFuncSource() |
| 105 |
if (!"session" %in% names(formals(server))) |
| 106 |
stop("Tested application server functions must declare input, output, and session arguments.") |
| 107 |
if (length(args)) |
| 108 |
stop("Arguments were provided to a server function.") |
| 109 |
|
| 110 |
body(server) <- rlang::expr({ |
| 111 |
session$setEnv(base::environment()) |
| 112 |
!!body(server) |
| 113 |
}) |
| 114 |
withMockContext(session, |
| 115 |
server(input = session$input, output = session$output, session = session) |
| 116 |
) |
| 117 |
|
| 118 |
# # If app is a server, we use only the server function's immediate |
| 119 |
# # environment to construct the mask. |
| 120 |
mask <- rlang::new_data_mask(rlang::env_clone(session$env)) |
| 121 |
withMockContext(session, { |
| 122 |
rlang::eval_tidy(quosure, mask, rlang::caller_env()) |
| 123 |
}) |
| 124 |
invisible() |
| 125 |
} |
| 126 |
|
| 127 |
withMockContext <- function(session, expr) { |
| 128 |
isolate( |
| 129 |
withReactiveDomain(session, { |
| 130 |
withr::with_options(list(`shiny.allowoutputreads` = TRUE), { |
| 131 |
# Sets a cache for renderCachedPlot() with cache = "app" to use. |
| 132 |
shinyOptions("cache" = session$appcache) |
| 133 |
expr |
| 134 |
}) |
| 135 |
}) |
| 136 |
) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
# Helpers ----------------------------------------------------------------- |
| 141 |
|
| 142 |
isModuleServer <- function(x) { |
| 143 |
is.function(x) && names(formals(x))[[1]] == "id" |
| 144 |
} |
| 145 |
|
| 146 |
isServer <- function(x) { |
| 147 |
if (!is.function(x)) { |
| 148 |
return(FALSE) |
| 149 |
} |
| 150 |
|
| 151 |
if (length(formals(x)) < 3) { |
| 152 |
return(FALSE) |
| 153 |
} |
| 154 |
|
| 155 |
identical(names(formals(x))[1:3], c("input", "output", "session")) |
| 156 |
} |