Skip to content

Commit

Permalink
test all input args
Browse files Browse the repository at this point in the history
  • Loading branch information
maxheld83 committed Jul 8, 2024
1 parent d45654a commit c677ab8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
24 changes: 18 additions & 6 deletions R/module2app.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module2app_server <- function(module_server = NULL, server_args = list()) {
module_server <- purrr::partial(module_server, !!!server_args)
function(input, output, session) {
output$server_args_react <- shiny::renderPrint(
filter_react_els(server_args)
exec_tree_of_reacs(filter_react_els(server_args))
)
res <- notify_non_reactive_returns(module_server(id = "test_object"))
if (shiny::is.reactive(res)) {
Expand Down Expand Up @@ -129,10 +129,14 @@ no_fun_provided_server <- function(id, x = "server") {
no_fun_provided_glue <- function(x) glue::glue("No {x} function provided.")

exec_tree_of_reacs <- function(.x) {
purrr::modify_tree(.x, leaf = rlang::exec)
purrr::modify_tree(
.x,
is_node = function(x) !(shiny::is.reactive(x)),
leaf = rlang::exec
)
}

# test module =====
# test modules =====

# taken from shiny docs https://shiny.posit.co/r/articles/improve/modules/

Expand Down Expand Up @@ -164,14 +168,22 @@ counter_button_ui <- function(id, label = "Counter") {
}

#' @describeIn counter_button Module Server
#' @param set_to
#' A reactive, giving the value to set the counter to.
#' @param increment_by
#' Increment of the count.
#' @export
counter_button_server <- function(id) {
counter_button_server <- function(id,
set_to = 0L,
increment_by = shiny::reactiveVal(1L)) {
abort_if_reactive(set_to)
abort_if_not_reactive(increment_by)
shiny::moduleServer(
id,
function(input, output, session) {
count <- shiny::reactiveVal(0)
count <- shiny::reactiveVal(set_to)
shiny::observeEvent(input$button, {
count(count() + 1)
count(count() + increment_by())
})
output$out <- shiny::renderText({
count()
Expand Down
9 changes: 9 additions & 0 deletions inst/examples/module2app/simple/args.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# you can pass in
# - reactive and non-reactive values to the server function and
# - non-reactive values to the ui function
module2app(
module_ui = counter_button_ui,
module_server = counter_button_server,
ui_args = list(label = "Double Counter"),
server_args = list(set_to = 100L, increment_by = shiny::reactiveVal(2L))
)
26 changes: 26 additions & 0 deletions tests/testthat/test-module2app.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,29 @@ test_that("works with both inputs", {
driver$click(selector = "#test_object-button")
expect_equal(driver$get_value(output = "res"), capture.output(print(1)))
})
test_that("works with arguments to ui and server", {
increment_by <- shiny::reactiveVal(4L)
shiny::testServer(
app = module2app_server(
module_server = counter_button_server,
server_args = list(set_to = 10, increment_by = increment_by)
),
expr = {
expect_equal(res(), 10)
session$setInputs(`test_object-button` = 1)
expect_equal(res(), 14)
# changing reactive input here
increment_by(1L)
session$setInputs(`test_object-button` = 2)
expect_equal(res(), 15)
}
)
skip_if_load_all2()
driver <- shinytest2::AppDriver$new(
source_pef("module2app", "simple", "args")
)
withr::defer(driver$stop())
expect_equal(driver$get_value(output = "res"), capture.output(print(100)))
driver$click(selector = "#test_object-button")
expect_equal(driver$get_value(output = "res"), capture.output(print(102)))
})

0 comments on commit c677ab8

Please sign in to comment.