| 1 |
skip_if_not_installed("future") |
| 2 |
library(future, warn.conflicts = FALSE) |
| 3 |
|
| 4 |
test_that("handles observers", { |
| 5 |
server <- function(input, output, session) { |
| 6 |
rv <- reactiveValues(x = 0, y = 0) |
| 7 |
observe({ |
| 8 |
rv$x <- input$x * 2 |
| 9 |
}) |
| 10 |
observe({ |
| 11 |
rv$y <- rv$x |
| 12 |
}) |
| 13 |
output$txt <- renderText({ |
| 14 |
paste0("Value: ", rv$x) |
| 15 |
}) |
| 16 |
} |
| 17 |
|
| 18 |
testServer(server, { |
| 19 |
session$setInputs(x=1) |
| 20 |
expect_equal(rv$y, 2) |
| 21 |
expect_equal(rv$x, 2) |
| 22 |
expect_equal(output$txt, "Value: 2") |
| 23 |
|
| 24 |
session$setInputs(x=2) |
| 25 |
expect_equal(rv$x, 4) |
| 26 |
expect_equal(rv$y, 4) |
| 27 |
expect_equal(output$txt, "Value: 4") |
| 28 |
}) |
| 29 |
}) |
| 30 |
|
| 31 |
test_that("inputs aren't directly assignable", { |
| 32 |
server <- function(input, output, session) {} |
| 33 |
|
| 34 |
testServer(server, { |
| 35 |
session$setInputs(x = 0) |
| 36 |
expect_error({ input$x <- 1 }) |
| 37 |
expect_error({ input$y <- 1 }) |
| 38 |
}) |
| 39 |
}) |
| 40 |
|
| 41 |
test_that("setInputs dots are dynamic", { |
| 42 |
server <- function(input, output, session) {} |
| 43 |
|
| 44 |
inputs_initial <- list(x=1, y=2) |
| 45 |
input_y <- "y" |
| 46 |
|
| 47 |
testServer(server, { |
| 48 |
session$setInputs(!!!inputs_initial) |
| 49 |
expect_equal(input$x, 1) |
| 50 |
expect_equal(input$y, 2) |
| 51 |
session$setInputs(!!input_y := 3) |
| 52 |
expect_equal(input$y, 3) |
| 53 |
|
| 54 |
# Duplicate names are an error |
| 55 |
expect_error(session$setInputs(x = 1, x = 2)) |
| 56 |
}) |
| 57 |
}) |
| 58 |
|
| 59 |
test_that("handles more complex expressions", { |
| 60 |
server <- function(input, output, session) { |
| 61 |
output$txt <- renderText({ |
| 62 |
input$x |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
testServer(server, { |
| 67 |
for (i in 1:5){ |
| 68 |
session$setInputs(x=i) |
| 69 |
expect_equal(output$txt, as.character(i)) |
| 70 |
} |
| 71 |
expect_equal(output$txt, "5") |
| 72 |
|
| 73 |
if(TRUE){ |
| 74 |
session$setInputs(x="abc") |
| 75 |
expect_equal(output$txt, "abc") |
| 76 |
} |
| 77 |
}) |
| 78 |
}) |
| 79 |
|
| 80 |
test_that("handles reactiveVal", { |
| 81 |
server <- function(input, output, session) { |
| 82 |
x <- reactiveVal(0) |
| 83 |
observe({ |
| 84 |
x(input$y + input$z) |
| 85 |
}) |
| 86 |
} |
| 87 |
|
| 88 |
testServer(server, { |
| 89 |
session$setInputs(y=1, z=2) |
| 90 |
|
| 91 |
expect_equal(x(), 3) |
| 92 |
|
| 93 |
session$setInputs(z=3) |
| 94 |
expect_equal(x(), 4) |
| 95 |
|
| 96 |
session$setInputs(y=5) |
| 97 |
expect_equal(x(), 8) |
| 98 |
}) |
| 99 |
}) |
| 100 |
|
| 101 |
test_that("handles reactives with complex dependency tree", { |
| 102 |
server <- function(input, output, session) { |
| 103 |
x <- reactiveValues(x=1) |
| 104 |
r <- reactive({ |
| 105 |
x$x + input$a + input$b |
| 106 |
}) |
| 107 |
r2 <- reactive({ |
| 108 |
r() + input$c |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
testServer(server, { |
| 113 |
session$setInputs(a=1, b=2, c=3) |
| 114 |
expect_equal(r(), 4) |
| 115 |
expect_equal(r2(), 7) |
| 116 |
|
| 117 |
session$setInputs(a=2) |
| 118 |
expect_equal(r(), 5) |
| 119 |
expect_equal(r2(), 8) |
| 120 |
|
| 121 |
session$setInputs(b=0) |
| 122 |
expect_equal(r2(), 6) |
| 123 |
expect_equal(r(), 3) |
| 124 |
|
| 125 |
session$setInputs(c=4) |
| 126 |
expect_equal(r(), 3) |
| 127 |
expect_equal(r2(), 7) |
| 128 |
}) |
| 129 |
}) |
| 130 |
|
| 131 |
test_that("handles reactivePoll", { |
| 132 |
server <- function(input, output, session) { |
| 133 |
rv <- reactiveValues(x = 0) |
| 134 |
rp <- reactivePoll(50, session, function(){ rnorm(1) }, function(){ |
| 135 |
isolate(rv$x <- rv$x + 1) |
| 136 |
rnorm(1) |
| 137 |
}) |
| 138 |
|
| 139 |
observe({rp()}) |
| 140 |
} |
| 141 |
|
| 142 |
testServer(server, { |
| 143 |
session$flushReact() |
| 144 |
expect_equal(rv$x, 1) |
| 145 |
|
| 146 |
for (i in 1:4){ |
| 147 |
session$elapse(50) |
| 148 |
} |
| 149 |
|
| 150 |
expect_equal(rv$x, 5) |
| 151 |
}) |
| 152 |
}) |
| 153 |
|
| 154 |
test_that("handles reactiveTimer", { |
| 155 |
server <- function(input, output, session) { |
| 156 |
rv <- reactiveValues(x = 0) |
| 157 |
|
| 158 |
rp <- reactiveTimer(50) |
| 159 |
observe({ |
| 160 |
rp() |
| 161 |
isolate(rv$x <- rv$x + 1) |
| 162 |
}) |
| 163 |
} |
| 164 |
|
| 165 |
testServer(server, { |
| 166 |
session$flushReact() |
| 167 |
expect_equal(rv$x, 1) |
| 168 |
|
| 169 |
session$elapse(200) |
| 170 |
|
| 171 |
expect_equal(rv$x, 5) |
| 172 |
}) |
| 173 |
}) |
| 174 |
|
| 175 |
test_that("handles debounce/throttle", { |
| 176 |
server <- function(input, output, session) { |
| 177 |
rv <- reactiveValues(t = 0, d = 0) |
| 178 |
react <- reactive({ |
| 179 |
input$y |
| 180 |
}) |
| 181 |
rt <- throttle(react, 100) |
| 182 |
rd <- debounce(react, 100) |
| 183 |
|
| 184 |
observe({ |
| 185 |
rt() # Invalidate this block on the timer |
| 186 |
isolate(rv$t <- rv$t + 1) |
| 187 |
}) |
| 188 |
|
| 189 |
observe({ |
| 190 |
rd() |
| 191 |
isolate(rv$d <- rv$d + 1) |
| 192 |
}) |
| 193 |
} |
| 194 |
|
| 195 |
testServer(server, { |
| 196 |
session$setInputs(y = TRUE) |
| 197 |
expect_equal(rv$d, 1) |
| 198 |
for (i in 2:5){ |
| 199 |
session$setInputs(y = FALSE) |
| 200 |
session$elapse(51) |
| 201 |
session$setInputs(y = TRUE) |
| 202 |
expect_equal(rv$t, i-1) |
| 203 |
session$elapse(51) # TODO: we usually don't have to pad by a ms, but here we do. Investigate. |
| 204 |
expect_equal(rv$t, i) |
| 205 |
} |
| 206 |
# Never sufficient time to debounce. Not incremented |
| 207 |
expect_equal(rv$d, 1) |
| 208 |
session$elapse(50) |
| 209 |
|
| 210 |
# Now that 100ms has passed since the last update, debounce should have triggered |
| 211 |
expect_equal(rv$d, 2) |
| 212 |
}) |
| 213 |
}) |
| 214 |
|
| 215 |
test_that("wraps output in an observer", { |
| 216 |
testthat::skip("I'm not sure of a great way to test this without timers.") |
| 217 |
# And honestly it's so foundational in what we're doing now that it might not be necessary to test? |
| 218 |
|
| 219 |
module <- function(id) { |
| 220 |
moduleServer(id, function(input, output, session) { |
| 221 |
rv <- reactiveValues(x=0) |
| 222 |
rp <- reactiveTimer(50) |
| 223 |
output$txt <- renderText({ |
| 224 |
rp() |
| 225 |
isolate(rv$x <- rv$x + 1) |
| 226 |
}) |
| 227 |
}) |
| 228 |
} |
| 229 |
|
| 230 |
testServer(module, { |
| 231 |
session$setInputs(x=1) |
| 232 |
# Timers only tick if they're being observed. If the output weren't being |
| 233 |
# wrapped in an observer, we'd see the value of rv$x initialize to zero and |
| 234 |
# only increment when we evaluated the output. e.g.: |
| 235 |
# |
| 236 |
# expect_equal(rv$x, 0) |
| 237 |
# Sys.sleep(1) |
| 238 |
# expect_equal(rv$x, 0) |
| 239 |
# output$txt() |
| 240 |
# expect_equal(rv$x, 1) |
| 241 |
|
| 242 |
expect_equal(rv$x, 1) |
| 243 |
expect_equal(output$txt, "1") |
| 244 |
Sys.sleep(.05) |
| 245 |
Sys.sleep(.05) |
| 246 |
expect_gt(rv$x, 1) |
| 247 |
expect_equal(output$txt, as.character(rv$x)) |
| 248 |
}) |
| 249 |
|
| 250 |
# FIXME: |
| 251 |
# - Do we want the output to be accessible natively, or some $get() on the output? If we do a get() we could |
| 252 |
# do more helpful spy-type things around exec count. |
| 253 |
# - plots and such? |
| 254 |
}) |
| 255 |
|
| 256 |
test_that("works with async", { |
| 257 |
server <- function(input, output, session) { |
| 258 |
output$txt <- renderText({ |
| 259 |
val <- input$x |
| 260 |
future({ val }) |
| 261 |
}) |
| 262 |
|
| 263 |
output$error <- renderText({ |
| 264 |
future({ stop("error here") }) |
| 265 |
}) |
| 266 |
|
| 267 |
output$sync <- renderText({ |
| 268 |
# No promises here |
| 269 |
"abc" |
| 270 |
}) |
| 271 |
} |
| 272 |
|
| 273 |
testServer(server, { |
| 274 |
session$setInputs(x=1) |
| 275 |
expect_equal(output$txt, "1") |
| 276 |
expect_equal(output$sync, "abc") |
| 277 |
|
| 278 |
# Error gets thrown repeatedly |
| 279 |
expect_error(output$error, "error here") |
| 280 |
expect_error(output$error, "error here") |
| 281 |
|
| 282 |
# Responds reactively |
| 283 |
session$setInputs(x=2) |
| 284 |
expect_equal(output$txt, "2") |
| 285 |
# Error still thrown |
| 286 |
expect_error(output$error, "error here") |
| 287 |
}) |
| 288 |
}) |
| 289 |
|
| 290 |
test_that("works with multiple promises in parallel", { |
| 291 |
# This test is inherently about timing which is against CRAN's policy. |
| 292 |
testthat::skip_on_cran() |
| 293 |
|
| 294 |
server <- function(input, output, session) { |
| 295 |
output$txt1 <- renderText({ |
| 296 |
future({ |
| 297 |
Sys.sleep(1) |
| 298 |
1 |
| 299 |
}) |
| 300 |
}) |
| 301 |
|
| 302 |
output$txt2 <- renderText({ |
| 303 |
future({ |
| 304 |
Sys.sleep(1) |
| 305 |
2 |
| 306 |
}) |
| 307 |
}) |
| 308 |
} |
| 309 |
|
| 310 |
testServer(server, { |
| 311 |
# As we enter this test code, the promises will still be running in the background. |
| 312 |
# We'll need to give them ~2s (plus overhead) to complete |
| 313 |
startMS <- as.numeric(Sys.time()) * 1000 |
| 314 |
expect_equal(output$txt1, "1") # This first call will block waiting for the promise to return |
| 315 |
expect_equal(output$txt2, "2") |
| 316 |
expect_equal(output$txt2, "2") # Now that we have the values, access should not incur a 1s delay. |
| 317 |
expect_equal(output$txt1, "1") |
| 318 |
expect_equal(output$txt1, "1") |
| 319 |
expect_equal(output$txt2, "2") |
| 320 |
endMS <- as.numeric(Sys.time()) * 1000 |
| 321 |
|
| 322 |
# We'll pad quite a bit because promises can introduce some lag. But the point we're trying |
| 323 |
# to prove is that we're not hitting a 1s delay for each output access, which = 6000ms. If we're |
| 324 |
# under that, then things are likely working. |
| 325 |
expect_lt(endMS - startMS, 4000) |
| 326 |
}) |
| 327 |
}) |
| 328 |
|
| 329 |
test_that("handles async errors", { |
| 330 |
server <- function(input, output, session, arg1, arg2){ |
| 331 |
output$err <- renderText({ |
| 332 |
future({ "my error"}) %...>% |
| 333 |
stop() %...>% |
| 334 |
print() # Extra steps after the error |
| 335 |
}) |
| 336 |
|
| 337 |
output$safe <- renderText({ |
| 338 |
future({ safeError("my safe error") }) %...>% |
| 339 |
stop() |
| 340 |
}) |
| 341 |
} |
| 342 |
|
| 343 |
testServer(server, { |
| 344 |
expect_error(output$err, "my error") |
| 345 |
# TODO: helper for safe errors so users don't have to learn "shiny.custom.error"? |
| 346 |
expect_error(output$safe, "my safe error", class="shiny.custom.error") |
| 347 |
}) |
| 348 |
}) |
| 349 |
|
| 350 |
test_that("captures htmlwidgets", { |
| 351 |
# TODO: use a simple built-in htmlwidget instead of something complex like dygraph |
| 352 |
skip_if_not_installed("dygraphs") |
| 353 |
skip_if_not_installed("jsonlite") |
| 354 |
|
| 355 |
server <- function(input, output, session){ |
| 356 |
output$dy <- dygraphs::renderDygraph({ |
| 357 |
dygraphs::dygraph(data.frame(outcome=0:5, year=2000:2005)) |
| 358 |
}) |
| 359 |
} |
| 360 |
|
| 361 |
testServer(server, { |
| 362 |
# Really, this test should be specific to each htmlwidget. Here, we don't want to bind ourselves |
| 363 |
# to the current JSON structure of dygraphs, so we'll just check one element to see that the raw |
| 364 |
# JSON was exposed and is accessible in tests. |
| 365 |
d <- jsonlite::fromJSON(output$dy)$x$data |
| 366 |
expect_equal(d[1,], 0:5) |
| 367 |
expect_equal(d[2,], 2000:2005) |
| 368 |
}) |
| 369 |
}) |
| 370 |
|
| 371 |
test_that("captures renderUI", { |
| 372 |
server <- function(input, output, session){ |
| 373 |
output$ui <- renderUI({ |
| 374 |
tags$a(href="https://rstudio.com", "hello!") |
| 375 |
}) |
| 376 |
} |
| 377 |
|
| 378 |
testServer(server, { |
| 379 |
expect_equal(output$ui$deps, list()) |
| 380 |
expect_equal(as.character(output$ui$html), "<a href=\"https://rstudio.com\">hello!</a>") |
| 381 |
}) |
| 382 |
}) |
| 383 |
|
| 384 |
test_that("captures base graphics outputs", { |
| 385 |
server <- function(input, output, session){ |
| 386 |
output$fixed <- renderPlot({ |
| 387 |
plot(1,1) |
| 388 |
}, width=300, height=350) |
| 389 |
|
| 390 |
output$dynamic <- renderPlot({ |
| 391 |
plot(1,1) |
| 392 |
}) |
| 393 |
} |
| 394 |
|
| 395 |
testServer(server, { |
| 396 |
# We aren't yet able to create reproducible graphics, so this test is intentionally pretty |
| 397 |
# limited. |
| 398 |
expect_equal(output$fixed$width, 300) |
| 399 |
expect_equal(output$fixed$height, 350) |
| 400 |
expect_match(output$fixed$src, "^data:image/png;base64,") |
| 401 |
|
| 402 |
# Ensure that the plot defaults to a reasonable size. |
| 403 |
expect_equal(output$dynamic$width, 600) |
| 404 |
expect_equal(output$dynamic$height, 400) |
| 405 |
expect_match(output$dynamic$src, "^data:image/png;base64,") |
| 406 |
|
| 407 |
# TODO: how do you customize automatically inferred plot sizes? |
| 408 |
# session$setPlotMeta("dynamic", width=600, height=300) ? |
| 409 |
}) |
| 410 |
}) |
| 411 |
|
| 412 |
test_that("captures ggplot2 outputs", { |
| 413 |
skip_if_not_installed("ggplot2") |
| 414 |
|
| 415 |
server <- function(input, output, session){ |
| 416 |
output$fixed <- renderPlot({ |
| 417 |
withr::with_namespace("ggplot2", { ggplot(iris) + geom_point(aes(Sepal.Length, Sepal.Width)) }) |
| 418 |
}, width=300, height=350) |
| 419 |
|
| 420 |
output$dynamic <- renderPlot({ |
| 421 |
withr::with_namespace("ggplot2", { ggplot(iris) + geom_point(aes(Sepal.Length, Sepal.Width)) }) |
| 422 |
}) |
| 423 |
} |
| 424 |
|
| 425 |
testServer(server, { |
| 426 |
expect_equal(output$fixed$width, 300) |
| 427 |
expect_equal(output$fixed$height, 350) |
| 428 |
expect_match(output$fixed$src, "^data:image/png;base64,") |
| 429 |
|
| 430 |
# Ensure that the plot defaults to a reasonable size. |
| 431 |
expect_equal(output$dynamic$width, 600) |
| 432 |
expect_equal(output$dynamic$height, 400) |
| 433 |
expect_match(output$dynamic$src, "^data:image/png;base64,") |
| 434 |
}) |
| 435 |
}) |
| 436 |
|
| 437 |
|
| 438 |
test_that("handles synchronous errors", { |
| 439 |
server <- function(input, output, session){ |
| 440 |
output$err <- renderText({ |
| 441 |
stop("my error") |
| 442 |
}) |
| 443 |
|
| 444 |
output$safe <- renderText({ |
| 445 |
stop(safeError("my safe error")) |
| 446 |
}) |
| 447 |
} |
| 448 |
|
| 449 |
testServer(server, { |
| 450 |
expect_error(output$err, "my error") |
| 451 |
# TODO: helper for safe errors so users don't have to learn "shiny.custom.error"? |
| 452 |
expect_error(output$safe, "my safe error", class="shiny.custom.error") |
| 453 |
}) |
| 454 |
}) |
| 455 |
|
| 456 |
test_that("accessing a non-existent output gives an informative message", { |
| 457 |
server <- function(input, output, session){} |
| 458 |
|
| 459 |
testServer(server, { |
| 460 |
expect_error(output$dontexist, "hasn't been defined yet") |
| 461 |
}) |
| 462 |
}) |
| 463 |
|
| 464 |
test_that("handles invalidateLater", { |
| 465 |
server <- function(input, output, session) { |
| 466 |
rv <- reactiveValues(x = 0) |
| 467 |
observe({ |
| 468 |
isolate(rv$x <- rv$x + 1) |
| 469 |
# We're only testing one invalidation |
| 470 |
if (isolate(rv$x) <= 1){ |
| 471 |
invalidateLater(50) |
| 472 |
} |
| 473 |
}) |
| 474 |
} |
| 475 |
|
| 476 |
testServer(server, { |
| 477 |
session$flushReact() |
| 478 |
# Should have run once |
| 479 |
expect_equal(rv$x, 1) |
| 480 |
|
| 481 |
session$elapse(49) |
| 482 |
expect_equal(rv$x, 1) |
| 483 |
|
| 484 |
session$elapse(1) |
| 485 |
# Should have been incremented now |
| 486 |
expect_equal(rv$x, 2) |
| 487 |
}) |
| 488 |
}) |
| 489 |
|
| 490 |
test_that("session ended handlers work", { |
| 491 |
server <- function(input, output, session){} |
| 492 |
|
| 493 |
testServer(server, { |
| 494 |
rv <- reactiveValues(closed = FALSE) |
| 495 |
session$onEnded(function(){ |
| 496 |
rv$closed <- TRUE |
| 497 |
}) |
| 498 |
|
| 499 |
expect_equal(session$isEnded(), FALSE) |
| 500 |
expect_equal(session$isClosed(), FALSE) |
| 501 |
expect_false(rv$closed) |
| 502 |
|
| 503 |
session$close() |
| 504 |
|
| 505 |
expect_equal(session$isEnded(), TRUE) |
| 506 |
expect_equal(session$isClosed(), TRUE) |
| 507 |
expect_true(rv$closed) |
| 508 |
}) |
| 509 |
}) |
| 510 |
|
| 511 |
test_that("session$unhandledError() handles unhandled errors", { |
| 512 |
caught <- NULL |
| 513 |
clear <- onUnhandledError(function(error) { |
| 514 |
caught <<- error |
| 515 |
stop("bad user error handler") |
| 516 |
}) |
| 517 |
on.exit(clear()) |
| 518 |
|
| 519 |
server <- function(input, output, session) { |
| 520 |
observe({ |
| 521 |
req(input$boom > 1) # This signals an error that shiny handles |
| 522 |
stop("unhandled error") # This error is *not* and brings down the app |
| 523 |
}) |
| 524 |
} |
| 525 |
|
| 526 |
testServer(server, { |
| 527 |
session$setInputs(boom = 1) |
| 528 |
# validation errors *are* handled and don't trigger the unhandled error |
| 529 |
expect_null(caught) |
| 530 |
expect_false(session$isEnded()) |
| 531 |
expect_false(session$isClosed()) |
| 532 |
|
| 533 |
# All errors are caught, even the error from the unhandled error handler |
| 534 |
# And these errors are converted to warnings |
| 535 |
expect_no_error( |
| 536 |
expect_warning( |
| 537 |
expect_warning( |
| 538 |
# Setting input$boom = 2 throws two errors that become warnings: |
| 539 |
# 1. The unhandled error in the observe |
| 540 |
# 2. The error thrown by the user error handler |
| 541 |
capture.output( |
| 542 |
session$setInputs(boom = 2), |
| 543 |
type = "message" |
| 544 |
), |
| 545 |
"unhandled error" |
| 546 |
), |
| 547 |
"bad user error handler" |
| 548 |
) |
| 549 |
) |
| 550 |
|
| 551 |
expect_s3_class(caught, "error") |
| 552 |
expect_s3_class(caught, "shiny.error.fatal") |
| 553 |
expect_equal(conditionMessage(caught), "unhandled error") |
| 554 |
expect_true(session$isEnded()) |
| 555 |
expect_true(session$isClosed()) |
| 556 |
}) |
| 557 |
}) |
| 558 |
|
| 559 |
test_that("session$unhandledError() handles unhandled render errors too", { |
| 560 |
caught <- NULL |
| 561 |
clear <- onUnhandledError(function(error) { |
| 562 |
caught <<- error |
| 563 |
}) |
| 564 |
on.exit(clear()) |
| 565 |
|
| 566 |
server <- function(input, output, session) { |
| 567 |
output$text <- renderText({ |
| 568 |
req(input$boom > 1) # This signals an error that shiny handles |
| 569 |
stop("unhandled error") # This error shows up in the UI, doesn't crash app |
| 570 |
}) |
| 571 |
} |
| 572 |
|
| 573 |
testServer(server, { |
| 574 |
expect_error( |
| 575 |
{ |
| 576 |
session$setInputs(boom = 1) |
| 577 |
output$text |
| 578 |
}, |
| 579 |
class = "shiny.silent.error" |
| 580 |
) |
| 581 |
# validation errors are considered handled, aren't passed to unhandledError |
| 582 |
expect_null(caught) |
| 583 |
expect_false(session$isEnded()) |
| 584 |
expect_false(session$isClosed()) |
| 585 |
|
| 586 |
# Setting input$boom = 2 and calling output$text throws an error that isn't |
| 587 |
# handled by shiny and is passed to unhandledError() |
| 588 |
expect_error( |
| 589 |
capture.output( |
| 590 |
{ |
| 591 |
session$setInputs(boom = 2) |
| 592 |
output$text |
| 593 |
}, |
| 594 |
type = "message" |
| 595 |
), |
| 596 |
"unhandled error" |
| 597 |
) |
| 598 |
|
| 599 |
expect_s3_class(caught, "error") |
| 600 |
expect_false("shiny.error.fatal" %in% class(caught)) |
| 601 |
expect_equal(conditionMessage(caught), "unhandled error") |
| 602 |
expect_false(session$isEnded()) |
| 603 |
expect_false(session$isClosed()) |
| 604 |
}) |
| 605 |
}) |
| 606 |
|
| 607 |
test_that("session flush handlers work", { |
| 608 |
server <- function(input, output, session) { |
| 609 |
rv <- reactiveValues(x = 0, flushCounter = 0, flushedCounter = 0, |
| 610 |
flushOnceCounter = 0, flushedOnceCounter = 0) |
| 611 |
|
| 612 |
onFlush(function(){rv$flushCounter <- rv$flushCounter + 1}, once=FALSE) |
| 613 |
onFlushed(function(){rv$flushedCounter <- rv$flushedCounter + 1}, once=FALSE) |
| 614 |
onFlushed(function(){rv$flushOnceCounter <- rv$flushOnceCounter + 1}, once=TRUE) |
| 615 |
onFlushed(function(){rv$flushedOnceCounter <- rv$flushedOnceCounter + 1}, once=TRUE) |
| 616 |
|
| 617 |
observe({ |
| 618 |
rv$x <- input$x * 2 |
| 619 |
}) |
| 620 |
} |
| 621 |
|
| 622 |
testServer(server, { |
| 623 |
session$setInputs(x=1) |
| 624 |
expect_equal(rv$x, 2) |
| 625 |
# We're not concerned with the exact values here -- only that they increase |
| 626 |
fc <- rv$flushCounter |
| 627 |
fdc <- rv$flushedCounter |
| 628 |
|
| 629 |
session$setInputs(x=2) |
| 630 |
expect_gt(rv$flushCounter, fc) |
| 631 |
expect_gt(rv$flushedCounter, fdc) |
| 632 |
|
| 633 |
# These should have only run once |
| 634 |
expect_equal(rv$flushOnceCounter, 1) |
| 635 |
expect_equal(rv$flushedOnceCounter, 1) |
| 636 |
|
| 637 |
}) |
| 638 |
}) |
| 639 |
|
| 640 |
test_that("can't test within test", { |
| 641 |
server <- function(input, output, session) { |
| 642 |
testServer() |
| 643 |
} |
| 644 |
expect_error(testServer(server, {}), "only within tests") |
| 645 |
}) |
| 646 |
|
| 647 |
test_that("validates server function", { |
| 648 |
server <- function(input, output, session) {} |
| 649 |
expect_error( |
| 650 |
testServer(server, {}, args = list(an_arg = 123)), |
| 651 |
"Arguments were provided" |
| 652 |
) |
| 653 |
|
| 654 |
app <- shinyApp(fluidPage(), function(x, y, z) {}) |
| 655 |
expect_error(testServer(app, {}), "must declare") |
| 656 |
}) |
| 657 |
|
| 658 |
# Provided an instance of an R6 object and its generator, returns a list with |
| 659 |
# `methods` and `fields`. `methods` contains a character vector of names of |
| 660 |
# public methods. `fields` is a character vector of public fields. Any active |
| 661 |
# bindings are considered `fields`. |
| 662 |
get_mocked_publics <- function(instance, generator) { |
| 663 |
publics <- ls(instance, all.names = TRUE) |
| 664 |
actives <- names(generator$active) %||% character(0) |
| 665 |
# Active bindings are considered fields. |
| 666 |
methods_or_fields <- publics[!(publics %in% actives)] |
| 667 |
methods <- character(0) |
| 668 |
fields <- actives |
| 669 |
for (name in methods_or_fields) { |
| 670 |
if (is.function(instance[[name]])) { |
| 671 |
methods <- c(methods, name) |
| 672 |
} else { |
| 673 |
fields <- c(fields, name) |
| 674 |
} |
| 675 |
} |
| 676 |
list(methods = methods, fields = fields) |
| 677 |
} |
| 678 |
|
| 679 |
test_that("MockShinySession has all public ShinySession methods and fields", { |
| 680 |
real_methods <- names(ShinySession$public_methods) |
| 681 |
real_fields <- c(names(ShinySession$public_fields), names(ShinySession$active)) |
| 682 |
|
| 683 |
# Here we must instantiate a MockShinySession because methods are added to the |
| 684 |
# instance in the constructor. |
| 685 |
mock_session <- MockShinySession$new() |
| 686 |
mocked <- get_mocked_publics(mock_session, MockShinySession) |
| 687 |
|
| 688 |
expect_equal(intersect(real_methods, mocked$methods), real_methods) |
| 689 |
expect_equal(intersect(real_fields, mocked$fields), real_fields) |
| 690 |
}) |
| 691 |
|
| 692 |
test_that("downloadHandler() works", { |
| 693 |
data <- mtcars |
| 694 |
tmpd <- NULL |
| 695 |
|
| 696 |
server <- function(input, output, session) { |
| 697 |
filename <- reactive({ |
| 698 |
paste0(input$name, ".", input$extension) |
| 699 |
}) |
| 700 |
output$downloadData <- downloadHandler( |
| 701 |
filename = filename(), |
| 702 |
content = function(file) { |
| 703 |
tmpd <<- dirname(file) |
| 704 |
saveRDS(data, file) |
| 705 |
} |
| 706 |
) |
| 707 |
} |
| 708 |
|
| 709 |
testServer(server, { |
| 710 |
session$setInputs(name = "mtcars", extension = "rds") |
| 711 |
f <- output$downloadData |
| 712 |
expect_equal(basename(f), "mtcars.rds") |
| 713 |
expect_equal(readRDS(f), data) |
| 714 |
}) |
| 715 |
|
| 716 |
# Ensure the temp file was closed when the session ended. |
| 717 |
expect_false(file.exists(tmpd)) |
| 718 |
}) |
| 719 |
|
| 720 |
test_that("getOutputInfo() returns current output name", { |
| 721 |
savedOutputInfo <- NULL |
| 722 |
|
| 723 |
server <- function(input, output, session) { |
| 724 |
output$txt <- renderText({ |
| 725 |
savedOutputInfo <<- getCurrentOutputInfo() |
| 726 |
"some text" |
| 727 |
}) |
| 728 |
} |
| 729 |
|
| 730 |
testServer(server, { |
| 731 |
expect_equal(savedOutputInfo, NULL) |
| 732 |
# savedOutputInfo is not set until output$txt is accessed |
| 733 |
expect_equal(output$txt, "some text") |
| 734 |
expect_equal(savedOutputInfo, list(name = "txt")) |
| 735 |
expect_equal(getCurrentOutputInfo(), NULL) |
| 736 |
}) |
| 737 |
}) |
| 738 |
|
| 739 |
test_that("promise chains evaluate in correct order", { |
| 740 |
messages <- list() |
| 741 |
clearMessages <- function() { |
| 742 |
messages <<- list() |
| 743 |
} |
| 744 |
pushMessage <- function(msg) { |
| 745 |
messages <<- c(messages, msg) |
| 746 |
} |
| 747 |
|
| 748 |
server <- function(input, output, session) { |
| 749 |
r1 <- reactive({ |
| 750 |
promises::promise(function(resolve, reject) { |
| 751 |
pushMessage("promise 1") |
| 752 |
resolve(input$go) |
| 753 |
})$then(function(value) { |
| 754 |
pushMessage(paste("promise 1 then", value)) |
| 755 |
paste("r1", input$go) |
| 756 |
}) |
| 757 |
}) |
| 758 |
r2 <- reactive({ |
| 759 |
promises::promise(function(resolve, reject) { |
| 760 |
pushMessage("promise 2") |
| 761 |
resolve(input$go) |
| 762 |
})$then(function(value) { |
| 763 |
pushMessage(paste("promise 2 then", value)) |
| 764 |
paste("r2", input$go) |
| 765 |
}) |
| 766 |
}) |
| 767 |
output$text1 <- renderText({ |
| 768 |
pushMessage("output$text1") |
| 769 |
r1() |
| 770 |
}) |
| 771 |
output$text2 <- renderText({ |
| 772 |
pushMessage("output$text2") |
| 773 |
input$go |
| 774 |
r2() |
| 775 |
}) |
| 776 |
} |
| 777 |
|
| 778 |
testServer(server, { |
| 779 |
expect_length(messages, 0) |
| 780 |
session$setInputs(go = 1) |
| 781 |
expect_equal(output$text1, "r1 1") |
| 782 |
expect_equal(output$text2, "r2 1") |
| 783 |
expect_equal(messages, list( |
| 784 |
"output$text1", |
| 785 |
"promise 1", |
| 786 |
"output$text2", |
| 787 |
"promise 2", |
| 788 |
"promise 1 then 1", |
| 789 |
"promise 2 then 1" |
| 790 |
)) |
| 791 |
clearMessages() |
| 792 |
session$setInputs(go = 2) |
| 793 |
expect_equal(output$text1, "r1 2") |
| 794 |
expect_equal(output$text2, "r2 2") |
| 795 |
expect_equal(messages, list( |
| 796 |
"output$text1", |
| 797 |
"promise 1", |
| 798 |
"output$text2", |
| 799 |
"promise 2", |
| 800 |
"promise 1 then 2", |
| 801 |
"promise 2 then 2" |
| 802 |
)) |
| 803 |
}) |
| 804 |
}) |
| 805 |
|
| 806 |
# Modules specific behaviour ------------------------------------------------ |
| 807 |
|
| 808 |
test_that("exposes the returned value from the module", { |
| 809 |
module <- function(id) { |
| 810 |
moduleServer(id, function(input, output, session){ |
| 811 |
reactive({ |
| 812 |
return(input$a + input$b) |
| 813 |
}) |
| 814 |
}) |
| 815 |
} |
| 816 |
|
| 817 |
testServer(module, { |
| 818 |
session$setInputs(a=1, b=2) |
| 819 |
expect_equal(session$getReturned()(), 3) |
| 820 |
|
| 821 |
# And retains reactivity |
| 822 |
session$setInputs(a=2) |
| 823 |
expect_equal(session$getReturned()(), 4) |
| 824 |
}) |
| 825 |
}) |
| 826 |
|
| 827 |
test_that("passes dots", { |
| 828 |
module <- function(id, someArg) { |
| 829 |
expect_false(missing(someArg)) |
| 830 |
moduleServer(id, server <- function(input, output, session) { |
| 831 |
expect_equal(someArg, 123) |
| 832 |
}) |
| 833 |
} |
| 834 |
testServer(module, {}, args = list(someArg = 123)) |
| 835 |
}) |
| 836 |
|
| 837 |
test_that("handles modules with additional arguments", { |
| 838 |
module <- function(id, arg1, arg2) { |
| 839 |
moduleServer(id, function(input, output, session){ |
| 840 |
output$txt1 <- renderText({ |
| 841 |
arg1 |
| 842 |
}) |
| 843 |
|
| 844 |
output$txt2 <- renderText({ |
| 845 |
arg2 |
| 846 |
}) |
| 847 |
|
| 848 |
output$inp <- renderText({ |
| 849 |
input$x |
| 850 |
}) |
| 851 |
}) |
| 852 |
} |
| 853 |
|
| 854 |
testServer(module, { |
| 855 |
expect_equal(output$txt1, "val1") |
| 856 |
expect_equal(output$txt2, "val2") |
| 857 |
}, list(arg1="val1", arg2="val2")) |
| 858 |
}) |
| 859 |
|
| 860 |
|
| 861 |
test_that("assigning an output in a module function with a non-function errors", { |
| 862 |
module <- function(id) { |
| 863 |
moduleServer(id, function(input, output, session) { |
| 864 |
output$someVar <- 123 |
| 865 |
}) |
| 866 |
} |
| 867 |
|
| 868 |
expect_error(testServer(module, {}), "^Unexpected") |
| 869 |
}) |
| 870 |
|
| 871 |
test_that("renderCachedPlot with cache = app and cache = session works", { |
| 872 |
module <- function(id, cache, callback) { |
| 873 |
moduleServer(id, function(input, output, session) { |
| 874 |
output$plot <- renderCachedPlot({ |
| 875 |
callback() |
| 876 |
plot(input$x, input$y) |
| 877 |
}, |
| 878 |
cacheKeyExpr = c(input$x, input$y), |
| 879 |
cache = cache |
| 880 |
) |
| 881 |
}) |
| 882 |
} |
| 883 |
|
| 884 |
timesRendered <- 0 |
| 885 |
callback <- function() (timesRendered <<- timesRendered + 1) |
| 886 |
|
| 887 |
testServer(module, { |
| 888 |
expect_equal(timesRendered, 0) |
| 889 |
session$setInputs(x = 1:10, y = 1:10) |
| 890 |
output$plot |
| 891 |
expect_equal(timesRendered, 1) |
| 892 |
session$setInputs(x = 1:10, y = 1:10) |
| 893 |
output$plot |
| 894 |
expect_equal(timesRendered, 1) |
| 895 |
}, args = list(cache = "session", callback = callback)) |
| 896 |
|
| 897 |
timesRendered <- 0 |
| 898 |
|
| 899 |
testServer(module, { |
| 900 |
expect_equal(timesRendered, 0) |
| 901 |
session$setInputs(x = 1:10, y = 1:10) |
| 902 |
output$plot |
| 903 |
expect_equal(timesRendered, 1) |
| 904 |
session$setInputs(x = 1:10, y = 1:10) |
| 905 |
output$plot |
| 906 |
expect_equal(timesRendered, 1) |
| 907 |
}, args = list(cache = "app", callback = callback)) |
| 908 |
}) |
| 909 |
|
| 910 |
|
| 911 |
# Helpers ----------------------------------------------------------------- |
| 912 |
|
| 913 |
test_that("isServer is only returns true for server funtions", { |
| 914 |
expect_false(isServer(10)) |
| 915 |
expect_false(isServer(function(x) {})) |
| 916 |
expect_false(isServer(function(output, session, input) {})) |
| 917 |
expect_true(isServer(function(input, output, session) {})) |
| 918 |
}) |