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

1163 extend TealAppDriver$new() and TealAppDriver$click() with $wait_for_idle() calls #1171

Merged
merged 14 commits into from
Mar 27, 2024
Merged
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
33 changes: 29 additions & 4 deletions R/TealAppDriver.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,29 @@ TealAppDriver <- R6::R6Class( # nolint: object_name.
#' Initialize a `TealAppDriver` object for testing a `teal` application.
#'
#' @param data,modules,filter,title,header,footer arguments passed to `init`
#' @param timeout (`numeric`) Default number of milliseconds for any timeout or
#' timeout_ parameter in the `TealAppDriver` class.
#' Defaults to 20s.
#'
#' See [`shinytest2::AppDriver`] `new` method for more details on how to change it
#' via options or environment variables.
#' @param load_timeout (`numeric`) How long to wait for the app to load, in ms.
#' This includes the time to start R. Defaults to 100s.
#'
#' See [`shinytest2::AppDriver`] `new` method for more details on how to change it
#' via options or environment variables
#' @param ... Additional arguments to be passed to `shinytest2::AppDriver$new`
#'
#'
#' @return Object of class `TealAppDriver`
initialize = function(data,
modules,
filter = teal_slices(),
title = build_app_title(),
header = tags$p(),
footer = tags$p(),
timeout = rlang::missing_arg(),
load_timeout = rlang::missing_arg(),
...) {
private$data <- data
private$modules <- modules
Expand All @@ -37,16 +51,29 @@ TealAppDriver <- R6::R6Class( # nolint: object_name.
header = header,
footer = footer
)

# Default timeout is hardcoded to 4s in shinytest2:::resolve_timeout
# It must be set as parameter to the AppDriver
suppressWarnings(
super$initialize(
shinyApp(app$ui, app$server),
app_dir = shinyApp(app$ui, app$server),
name = "teal",
variant = platform_variant(),
timeout = rlang::maybe_missing(timeout, 20 * 1000),
load_timeout = rlang::maybe_missing(load_timeout, 100 * 1000),
...
)
)

private$set_active_ns()
self$wait_for_idle()
vedhav marked this conversation as resolved.
Show resolved Hide resolved
},
#' @description
#' Append parent [`shinytest2::AppDriver`] `click` method with a call to `waif_for_idle()` method.
#' @param ... arguments passed to parent [`shinytest2::AppDriver`] `click()` method.
click = function(...) {
super$click(...)
self$wait_for_idle()
},
#' @description
#' Check if the app has shiny errors. This checks for global shiny errors.
Expand Down Expand Up @@ -112,7 +139,7 @@ TealAppDriver <- R6::R6Class( # nolint: object_name.
)
root <- sprintf("%s-%s", private$modules$label, get_unique_labels(tab))
}
self$wait_for_idle(timeout = private$idle_timeout)
self$wait_for_idle()
private$set_active_ns()
invisible(self)
},
Expand Down Expand Up @@ -444,8 +471,6 @@ TealAppDriver <- R6::R6Class( # nolint: object_name.
module = character(0),
filter_panel = character(0)
),
idle_timeout = 20000, # 20 seconds
load_timeout = 100000, # 100 seconds
# private methods ----
set_active_ns = function() {
all_inputs <- self$get_values()$input
Expand Down
34 changes: 33 additions & 1 deletion man/TealAppDriver.Rd

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

2 changes: 0 additions & 2 deletions tests/testthat/helper-shinytest2.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
library(shinytest2)
library(rvest)

default_idle_timeout <- 20000

simple_teal_data <- function() {
data <- within(teal_data(), {
iris <- iris
Expand Down
10 changes: 0 additions & 10 deletions tests/testthat/test-shinytest2-filter_panel.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ testthat::test_that("e2e: module content is updated when a data is filtered in f
)
)

app$wait_for_idle(timeout = default_idle_timeout)

old_output <- app$get_active_module_output("text")

app$set_active_filter_selection("iris", "Species", c("setosa", "versicolor"))
Expand Down Expand Up @@ -47,20 +45,16 @@ testthat::test_that("e2e: filtering a module-specific filter is refected in othe
)
)

app$wait_for_idle(timeout = default_idle_timeout)

expect_equal(
app$get_active_data_filters("iris")$Species,
c("setosa", "versicolor", "virginica")
)

app$navigate_teal_tab("Module_2")
app$wait_for_idle(timeout = default_idle_timeout)

app$set_active_filter_selection("iris", "Species", c("setosa"))

app$navigate_teal_tab("Module_1")
app$wait_for_idle(timeout = default_idle_timeout)

expect_equal(
app$get_active_data_filters("iris")$Species,
Expand Down Expand Up @@ -90,20 +84,16 @@ testthat::test_that("e2e: filtering a module-specific filter is not refected in
)
)

app$wait_for_idle(timeout = default_idle_timeout)

expect_equal(
app$get_active_data_filters("mtcars")$cyl,
c("4", "6")
)

app$navigate_teal_tab("Module_2")
app$wait_for_idle(timeout = default_idle_timeout)

app$set_active_filter_selection("mtcars", "cyl", c("4"))

app$navigate_teal_tab("Module_1")
app$wait_for_idle(timeout = default_idle_timeout)

expect_equal(
app$get_active_data_filters("mtcars")$cyl,
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-shinytest2-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ testthat::test_that("e2e: teal app initializes with no errors", {
data = simple_teal_data(),
modules = example_module(label = "Example Module")
)
app$wait_for_idle(timeout = default_idle_timeout)
app$expect_no_shiny_error()
app$stop()
})
Expand All @@ -15,7 +14,6 @@ testthat::test_that("e2e: teal app initializes with sessionInfo modal", {
data = simple_teal_data(),
modules = example_module(label = "Example Module")
)
app$wait_for_idle(timeout = default_idle_timeout)

# Check if button exists.
button_selector <- "#teal-sessionInfo-button"
Expand All @@ -25,7 +23,6 @@ testthat::test_that("e2e: teal app initializes with sessionInfo modal", {
)

app$click(selector = button_selector)
app$wait_for_idle(timeout = default_idle_timeout)

# Check header and title content.
testthat::expect_equal(
Expand Down Expand Up @@ -82,7 +79,6 @@ testthat::test_that("e2e: init creates UI containing specified title, favicon, h
header = app_header,
footer = app_footer
)
app$wait_for_idle(timeout = default_idle_timeout)

testthat::expect_equal(
app$get_text("head > title")[1],
Expand Down
7 changes: 0 additions & 7 deletions tests/testthat/test-shinytest2-landing_popup.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ testthat::test_that("e2e: teal app with landing_popup_module initializes with no
)
)

app$wait_for_idle(timeout = default_idle_timeout)
testthat::expect_equal(
app$get_text("#landingpopup b"),
"A welcome message!"
Expand All @@ -28,7 +27,6 @@ testthat::test_that("e2e: app with default landing_popup_module creates modal co
example_module()
)
)
app$wait_for_idle(timeout = default_idle_timeout)

testthat::expect_equal(
app$get_text("#shiny-modal-wrapper button"),
Expand All @@ -47,11 +45,9 @@ testthat::test_that("e2e: when default landing_popup_module is closed, it shows
example_module()
)
)
app$wait_for_idle(timeout = default_idle_timeout)

# Button is clicked.
app$click(selector = "#shiny-modal-wrapper button[data-dismiss='modal']")
app$wait_for_idle(timeout = default_idle_timeout)

# There is no more modal displayed.
testthat::expect_null(app$get_html("#shiny-modal-wrapper"))
Expand Down Expand Up @@ -104,8 +100,6 @@ testthat::test_that(
)
)

app$wait_for_idle(timeout = default_idle_timeout)

testthat::expect_equal(
app$get_text(".modal-title"),
modal_title
Expand Down Expand Up @@ -157,7 +151,6 @@ testthat::test_that("e2e: when customized button in landing_popup_module is clic
example_module()
)
)
app$wait_for_idle(timeout = default_idle_timeout)

testthat::expect_equal(
app$get_attr("#read", "onclick"),
Expand Down
5 changes: 1 addition & 4 deletions tests/testthat/test-shinytest2-modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ testthat::test_that("e2e: the module server logic is only triggered when the tea
value_export_module(label = "Module 2")
)
)
app$wait_for_idle(timeout = default_idle_timeout)

test_exports <- app$get_values()$export

expect_equal(length(test_exports), 1)
Expand All @@ -45,7 +45,6 @@ testthat::test_that("e2e: filter panel only shows the data supplied using datana
example_module(label = "mtcars", datanames = "mtcars")
)
)
app$wait_for_idle(timeout = default_idle_timeout)

testthat::expect_identical(
app$get_active_filter_vars(),
Expand All @@ -62,7 +61,6 @@ testthat::test_that("e2e: filter panel shows all the datasets when datanames is
example_module(label = "all", datanames = "all")
)
)
app$wait_for_idle(timeout = default_idle_timeout)

testthat::expect_identical(
app$get_active_filter_vars(),
Expand All @@ -79,7 +77,6 @@ testthat::test_that("e2e: filter panel is not displayed when datanames is NULL",
example_module(label = "NULL", datanames = NULL)
)
)
app$wait_for_idle(timeout = default_idle_timeout)

testthat::expect_identical(
app$get_html_rvest(".teal_secondary_col") %>%
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-shinytest2-reporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ testthat::test_that("e2e: adding a report card in a module adds it in the report
data = simple_teal_data(),
modules = report_module(label = "Module with Reporter")
)
app$wait_for_idle(timeout = default_idle_timeout)

app$click(NS(app$active_module_ns(), "reporter-add_report_card_simple-add_report_card_button"))
app$wait_for_idle(timeout = default_idle_timeout)

app$set_input(
NS(app$active_module_ns(), "reporter-add_report_card_simple-label"),
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-shinytest2-show-rcode.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ testthat::test_that("e2e: teal app initializes with Show R Code modal", {
data = simple_teal_data(),
modules = example_module(label = "Example Module")
)
app$wait_for_idle(timeout = default_idle_timeout)

# Check if button exists.
button_selector <- app$active_module_element("rcode-button")
Expand All @@ -14,7 +13,6 @@ testthat::test_that("e2e: teal app initializes with Show R Code modal", {
)

app$click(selector = button_selector)
app$wait_for_idle(timeout = default_idle_timeout)

# Check header and title content.
testthat::expect_equal(
Expand Down
14 changes: 4 additions & 10 deletions tests/testthat/test-shinytest2-teal_data_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ testthat::test_that("e2e: teal_data_module will have a delayed load of datasets"

app <- TealAppDriver$new(
data = tdm,
modules = example_module(label = "Example Module"),
timeout = default_idle_timeout
modules = example_module(label = "Example Module")
)

app$click("teal_data_module-submit")
app$wait_for_idle()
testthat::expect_setequal(app$get_active_filter_vars(), c("dataset1", "dataset2"))

app$stop()
Expand Down Expand Up @@ -62,10 +60,8 @@ testthat::test_that("e2e: teal_data_module shows validation errors", {

app <- TealAppDriver$new(
data = tdm,
modules = example_module(label = "Example Module"),
timeout = default_idle_timeout
modules = example_module(label = "Example Module")
)
app$wait_for_idle()

app$click("teal_data_module-submit")

Expand Down Expand Up @@ -108,17 +104,15 @@ testthat::test_that("e2e: teal_data_module inputs change teal_data object that i

app <- TealAppDriver$new(
data = tdm,
modules = example_module(label = "Example Module"),
timeout = default_idle_timeout
modules = example_module(label = "Example Module")
)
app$wait_for_idle()

app$set_input("teal_data_module-new_column", "A_New_Column")
app$click("teal_data_module-submit")

# This may fail if teal_data_module does not perform the transformation
testthat::expect_no_error(app$add_filter_var("dataset1", "A_New_Column"))

app$wait_for_idle()
testthat::expect_setequal(
app$get_active_data_filters("dataset1")$A_New_Column,
unique(sprintf("%s new", iris$Species))
Expand Down
Loading
Loading