From de76932b5509f6a1c0693db7d2d1d4f33affdf44 Mon Sep 17 00:00:00 2001 From: Mahmoud Hallal Date: Thu, 24 Nov 2022 16:13:52 +0100 Subject: [PATCH] revert --- DESCRIPTION | 1 - R/app_pws.R | 50 --- R/app_tws.R | 33 -- R/plot_with_settings.R | 111 +++---- inst/tws/app.R | 24 ++ inst/tws/tests/testthat.R | 1 + inst/tws/tests/testthat/_snaps/shinytest2.md | 12 + .../_snaps/shinytest2/tws-001.download | 8 + .../_snaps/shinytest2/tws-002.download | 9 + inst/tws/tests/testthat/setup.R | 2 + .../tws/tests/testthat/test-shinytest2.R | 45 +-- man/app_pws.Rd | 12 - man/app_tws.Rd | 12 - man/print_plot.Rd | 22 -- tests/testthat/_snaps/tws.md | 12 - tests/testthat/_snaps/tws/tws-001-tab1.csv | 6 - tests/testthat/_snaps/tws/tws-002-tab2.txt | 7 - tests/testthat/test-pws.R | 300 ------------------ tests/testthat/test-table_with_settings.R | 9 + 19 files changed, 125 insertions(+), 551 deletions(-) delete mode 100644 R/app_pws.R delete mode 100644 R/app_tws.R create mode 100644 inst/tws/app.R create mode 100644 inst/tws/tests/testthat.R create mode 100644 inst/tws/tests/testthat/_snaps/shinytest2.md create mode 100644 inst/tws/tests/testthat/_snaps/shinytest2/tws-001.download create mode 100644 inst/tws/tests/testthat/_snaps/shinytest2/tws-002.download create mode 100644 inst/tws/tests/testthat/setup.R rename tests/testthat/test-tws.R => inst/tws/tests/testthat/test-shinytest2.R (70%) delete mode 100644 man/app_pws.Rd delete mode 100644 man/app_tws.Rd delete mode 100644 man/print_plot.Rd delete mode 100644 tests/testthat/_snaps/tws.md delete mode 100644 tests/testthat/_snaps/tws/tws-001-tab1.csv delete mode 100644 tests/testthat/_snaps/tws/tws-002-tab2.txt delete mode 100644 tests/testthat/test-pws.R create mode 100644 tests/testthat/test-table_with_settings.R diff --git a/DESCRIPTION b/DESCRIPTION index 5f5a5cc0..8553d85a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,7 +31,6 @@ Imports: styler Suggests: knitr, - lattice, magrittr, rmarkdown, shinytest2, diff --git a/R/app_pws.R b/R/app_pws.R deleted file mode 100644 index 774efea3..00000000 --- a/R/app_pws.R +++ /dev/null @@ -1,50 +0,0 @@ -#' Plot with settings app -#' -#' @description Example plot with setting app for testing using \code{shinytest2} -#' -#' @keywords internal -#' -app_pws <- function() { - shiny::shinyApp( - ui = shiny::fluidPage( - shinyjs::useShinyjs(), - shiny::actionButton("button", "Show/Hide"), - plot_with_settings_ui( - id = "plot_with_settings" - ) - ), - server = function(input, output, session) { - plot_r <- shiny::reactive({ - ggplot2::ggplot(data.frame(x = 1:5, y = 1:5)) + - ggplot2::geom_point(ggplot2::aes(x = 1:5, y = 1:5)) - }) - - show_hide_signal <- shiny::reactiveVal(TRUE) - - shiny::observeEvent(input$button, { - show_hide_signal( - shiny::isolate( - !show_hide_signal() - ) - ) - }) - - plot_data <- plot_with_settings_srv( - id = "plot_with_settings", - plot_r = plot_r, - height = c(400, 100, 1200), - width = c(500, 250, 750), - brushing = TRUE, - clicking = TRUE, - dblclicking = TRUE, - hovering = TRUE, - show_hide_signal = show_hide_signal - ) - - shiny::exportTestValues( - plot_r = plot_r, - plot_data = plot_data - ) - } - ) -} diff --git a/R/app_tws.R b/R/app_tws.R deleted file mode 100644 index c789ab01..00000000 --- a/R/app_tws.R +++ /dev/null @@ -1,33 +0,0 @@ -#' Table with settings app -#' -#' @description Example table with setting app for testing using \code{shinytest2} -#' -#' @keywords internal -#' -app_tws <- function() { - shiny::shinyApp( - ui = shiny::fluidPage( - table_with_settings_ui( - id = "table_with_settings" - ) - ), - server = function(input, output, session) { - df1 <- data.frame( - AGE = c(35, 41), - SEX = factor(c("M", "F")), - ARM = c("B: Placebo", "C: Combination") - ) - - table_r <- shiny::reactive({ - l <- rtables::basic_table() %>% - rtables::split_cols_by("ARM") %>% - rtables::analyze(c("SEX", "AGE")) - - tbl <- rtables::build_table(l, df1) - - tbl - }) - table_with_settings_srv(id = "table_with_settings", table_r = table_r) - } - ) -} diff --git a/R/plot_with_settings.R b/R/plot_with_settings.R index c856ef11..95e72024 100644 --- a/R/plot_with_settings.R +++ b/R/plot_with_settings.R @@ -315,22 +315,20 @@ plot_with_settings_srv <- function(id, } }) - plot_reactive <- reactive({ - if (plot_type() == "gg" && dblclicking) { - plot_r() + - ggplot2::coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE) - } else if (plot_type() == "grob") { - # calling grid.draw on plot_r() is needed; - # otherwise the plot will not re-render if the user triggers the zoom in or out feature of the browser. - grid::grid.newpage() - grid::grid.draw(plot_r()) - } else { - plot_r() - } - }) - output$plot_modal <- output$plot_main <- renderPlot( - plot_reactive(), + expr = { + if (plot_type() == "gg" && dblclicking) { + plot_r() + + ggplot2::coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE) + } else if (plot_type() == "grob") { + # calling grid.draw on plot_r() is needed; + # otherwise the plot will not re-render if the user triggers the zoom in or out feature of the browser. + grid::grid.newpage() + grid::grid.draw(plot_r()) + } else { + plot_r() + } + }, res = get_plot_dpi() ) @@ -511,8 +509,39 @@ type_download_srv <- function(id, plot_reactive, plot_type, plot_w, default_w, p svg = grDevices::svg(file, width / get_plot_dpi(), height / get_plot_dpi()) ) - print_plot(plot_reactive(), plot_type()) - + if (plot_type() == "other") { + graphics::plot.new() + graphics::text( + x = graphics::grconvertX(0.5, from = "npc"), + y = graphics::grconvertY(0.5, from = "npc"), + labels = "This plot graphic type is not yet supported to download" + ) + } else { + g <- plot_reactive() + wm <- grid::grid.text( + "DRAFT", + draw = FALSE, + rot = -45, + gp = grid::gpar( + alpha = 0.3, + fontface = "bold", + cex = 3 + ) + ) + g_fin <- grid::gTree( + children = grid::gList( + if (plot_type() == "grob") { + g + } else if (plot_type() == "gg") { + ggplot2::ggplotGrob(g) + } else if (plot_type() == "trel") { + grid::grid.grabExpr(print(g), warn = 0, wrap.grobs = TRUE) + }, + wm + ) + ) + grid::grid.draw(g_fin) + } grDevices::dev.off() } ) @@ -571,51 +600,3 @@ get_plot_dpi <- function() { } dpi } - - - -#' Print plot for download functionality -#' -#' @param plot (`reactive`)\cr -#' reactive expression to draw a plot -#' @param plot_type (`reactive`)\cr -#' reactive plot type (`gg`, `trel`, `grob`, `other`) -#' -#' @return Nothing returned, the plot is printed. -#' @keywords internal -#' -print_plot <- function(plot, plot_type) { - if (plot_type == "other") { - graphics::plot.new() - graphics::text( - x = graphics::grconvertX(0.5, from = "npc"), - y = graphics::grconvertY(0.5, from = "npc"), - labels = "This plot graphic type is not yet supported to download" - ) - } else { - g <- plot - wm <- grid::grid.text( - "DRAFT", - draw = FALSE, - rot = -45, - gp = grid::gpar( - alpha = 0.3, - fontface = "bold", - cex = 3 - ) - ) - g_fin <- grid::gTree( - children = grid::gList( - if (plot_type == "grob") { - g - } else if (plot_type == "gg") { - ggplot2::ggplotGrob(g) - } else if (plot_type == "trel") { - grid::grid.grabExpr(print(g), warn = 0, wrap.grobs = TRUE) - }, - wm - ) - ) - grid::grid.draw(g_fin) - } -} diff --git a/inst/tws/app.R b/inst/tws/app.R new file mode 100644 index 00000000..4cb463e4 --- /dev/null +++ b/inst/tws/app.R @@ -0,0 +1,24 @@ +library(shiny) +library(rtables) +library(magrittr) +library(teal.widgets) + +shinyApp( + ui = fluidPage( + table_with_settings_ui( + id = "table_with_settings" + ) + ), + server = function(input, output, session) { + table_r <- reactive({ + l <- basic_table() %>% + split_cols_by("ARM") %>% + analyze(c("SEX", "AGE")) + + tbl <- build_table(l, DM) + + tbl + }) + table_with_settings_srv(id = "table_with_settings", table_r = table_r) + } +) diff --git a/inst/tws/tests/testthat.R b/inst/tws/tests/testthat.R new file mode 100644 index 00000000..7d25b5b9 --- /dev/null +++ b/inst/tws/tests/testthat.R @@ -0,0 +1 @@ +shinytest2::test_app() diff --git a/inst/tws/tests/testthat/_snaps/shinytest2.md b/inst/tws/tests/testthat/_snaps/shinytest2.md new file mode 100644 index 00000000..cbae0df9 --- /dev/null +++ b/inst/tws/tests/testthat/_snaps/shinytest2.md @@ -0,0 +1,12 @@ +# {shinytest2} snapshotting inputs + + "\n \n \n A: Drug X\n B: Placebo\n C: Combination\n \n \n SEX\n \n \n \n \n \n F\n 70\n 56\n 61\n \n \n M\n 51\n 50\n 68\n \n \n U\n 0\n 0\n 0\n \n \n UNDIFFERENTIATED\n 0\n 0\n 0\n \n \n AGE\n \n \n \n \n \n Mean\n 34.91\n 33.02\n 34.57\n \n \n \n \n \n \n \n \n " + +# {shinytest2} txt csv download + + "tab1.csv" + +--- + + "tab2.txt" + diff --git a/inst/tws/tests/testthat/_snaps/shinytest2/tws-001.download b/inst/tws/tests/testthat/_snaps/shinytest2/tws-001.download new file mode 100644 index 00000000..f0eef60f --- /dev/null +++ b/inst/tws/tests/testthat/_snaps/shinytest2/tws-001.download @@ -0,0 +1,8 @@ +"1","","A: Drug X","B: Placebo","C: Combination" +"2","SEX","","","" +"3","F","70","56","61" +"4","M","51","50","68" +"5","U","0","0","0" +"6","UNDIFFERENTIATED","0","0","0" +"7","AGE","","","" +"8","Mean","34.91","33.02","34.57" diff --git a/inst/tws/tests/testthat/_snaps/shinytest2/tws-002.download b/inst/tws/tests/testthat/_snaps/shinytest2/tws-002.download new file mode 100644 index 00000000..e0662c6a --- /dev/null +++ b/inst/tws/tests/testthat/_snaps/shinytest2/tws-002.download @@ -0,0 +1,9 @@ + A: Drug X B: Placebo C: Combination +———————————————————————————————————————————————————————————— +SEX + F 70 56 61 + M 51 50 68 + U 0 0 0 + UNDIFFERENTIATED 0 0 0 +AGE + Mean 34.91 33.02 34.57 diff --git a/inst/tws/tests/testthat/setup.R b/inst/tws/tests/testthat/setup.R new file mode 100644 index 00000000..be65b4f0 --- /dev/null +++ b/inst/tws/tests/testthat/setup.R @@ -0,0 +1,2 @@ +# Load application support files into testing environment +shinytest2::load_app_env() diff --git a/tests/testthat/test-tws.R b/inst/tws/tests/testthat/test-shinytest2.R similarity index 70% rename from tests/testthat/test-tws.R rename to inst/tws/tests/testthat/test-shinytest2.R index 3dccd5a4..441daf74 100644 --- a/tests/testthat/test-tws.R +++ b/inst/tws/tests/testthat/test-shinytest2.R @@ -1,45 +1,28 @@ library(shinytest2) -library(testthat) -# launch app for all tests -tws <- app_tws() -app <- AppDriver$new(tws, name = "tws") - -# Testing snapshots -testthat::test_that("file name and table content", { - skip_on_cran() - skip_on_ci() - - # default file name - file_name <- app$get_value(input = "table_with_settings-downbutton-file_name") - file_name <- sub("_\\d{6}$", "", file_name) - expected_file_name <- paste0("table_", strftime(Sys.Date(), - format = "%Y%m%d" - )) - testthat::expect_equal(file_name, expected_file_name) - - # check for table content - app$expect_text("table") -}) - -# downloading tables -testthat::test_that("txt and csv download", { - skip_on_cran() - skip_on_ci() - - # download table in .csv +test_that("{shinytest2} txt/csv download", { + app <- AppDriver$new(name = "tws", height = 820, width = 1551) app$click("table_with_settings-downbutton-dwnl") app$set_inputs(`table_with_settings-downbutton-dwnl_state` = TRUE) app$set_inputs(`table_with_settings-downbutton-file_format` = ".csv") app$set_inputs(`table_with_settings-downbutton-dwnl_state` = FALSE) app$set_inputs(`table_with_settings-downbutton-file_name` = "tab1") app$expect_download("table_with_settings-downbutton-data_download") - - # download table in .txt app$set_inputs(`table_with_settings-downbutton-file_format` = ".txt") app$set_inputs(`table_with_settings-downbutton-dwnl_state` = TRUE) app$set_inputs(`table_with_settings-downbutton-file_name` = "tab2") app$expect_download("table_with_settings-downbutton-data_download") + app$set_inputs(`table_with_settings-downbutton-dwnl_state` = FALSE) }) -app$stop() +test_that("{shinytest2} snapshotting inputs", { + app <- AppDriver$new(name = "tws_filename") + file_name <- app$get_value(input = "table_with_settings-downbutton-file_name") + file_name <- sub("_\\d{6}$", "", file_name) + expected_file_name <- paste0("table_", strftime(Sys.Date(), + format = "%Y%m%d" + )) + testthat::expect_equal(file_name, expected_file_name) + app$expect_text("table") + app$stop() +}) diff --git a/man/app_pws.Rd b/man/app_pws.Rd deleted file mode 100644 index 7f5bdd0e..00000000 --- a/man/app_pws.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/app_pws.R -\name{app_pws} -\alias{app_pws} -\title{Plot with settings app} -\usage{ -app_pws() -} -\description{ -Example plot with setting app for testing using \code{shinytest2} -} -\keyword{internal} diff --git a/man/app_tws.Rd b/man/app_tws.Rd deleted file mode 100644 index ab79dd70..00000000 --- a/man/app_tws.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/app_tws.R -\name{app_tws} -\alias{app_tws} -\title{Table with settings app} -\usage{ -app_tws() -} -\description{ -Example table with setting app for testing using \code{shinytest2} -} -\keyword{internal} diff --git a/man/print_plot.Rd b/man/print_plot.Rd deleted file mode 100644 index 48387c28..00000000 --- a/man/print_plot.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/plot_with_settings.R -\name{print_plot} -\alias{print_plot} -\title{Print plot for download functionality} -\usage{ -print_plot(plot, plot_type) -} -\arguments{ -\item{plot}{(\code{reactive})\cr -reactive expression to draw a plot} - -\item{plot_type}{(\code{reactive})\cr -reactive plot type (\code{gg}, \code{trel}, \code{grob}, \code{other})} -} -\value{ -Nothing returned, the plot is printed. -} -\description{ -Print plot for download functionality -} -\keyword{internal} diff --git a/tests/testthat/_snaps/tws.md b/tests/testthat/_snaps/tws.md deleted file mode 100644 index 068cdcd5..00000000 --- a/tests/testthat/_snaps/tws.md +++ /dev/null @@ -1,12 +0,0 @@ -# file name and table content - - "\n \n \n B: Placebo\n C: Combination\n \n \n SEX\n \n \n \n \n F\n 0\n 1\n \n \n M\n 1\n 0\n \n \n AGE\n \n \n \n \n Mean\n 35.00\n 41.00\n \n \n \n \n \n \n \n \n " - -# txt and csv download - - "tab1.csv" - ---- - - "tab2.txt" - diff --git a/tests/testthat/_snaps/tws/tws-001-tab1.csv b/tests/testthat/_snaps/tws/tws-001-tab1.csv deleted file mode 100644 index e5ad56e6..00000000 --- a/tests/testthat/_snaps/tws/tws-001-tab1.csv +++ /dev/null @@ -1,6 +0,0 @@ -"1","","B: Placebo","C: Combination" -"2","SEX","","" -"3","F","0","1" -"4","M","1","0" -"5","AGE","","" -"6","Mean","35.00","41.00" diff --git a/tests/testthat/_snaps/tws/tws-002-tab2.txt b/tests/testthat/_snaps/tws/tws-002-tab2.txt deleted file mode 100644 index e36f4887..00000000 --- a/tests/testthat/_snaps/tws/tws-002-tab2.txt +++ /dev/null @@ -1,7 +0,0 @@ - B: Placebo C: Combination -———————————————————————————————————— -SEX - F 0 1 - M 1 0 -AGE - Mean 35.00 41.00 diff --git a/tests/testthat/test-pws.R b/tests/testthat/test-pws.R deleted file mode 100644 index 8b2368e3..00000000 --- a/tests/testthat/test-pws.R +++ /dev/null @@ -1,300 +0,0 @@ -library(shinytest2) - -brush_vals <- c( - 1.575922029932, 2.2959573557845, - 1.218473025872, 2.2972720836904, 252, 532, 251, 349, 252, 532, 251, 349, 1, - 1, "1:5", "1:5", 1, 5, 1, 5, 28.0413099315069, 1583.52054794521, 368.846473150198, - 5.47945205479452, character(0), character(0), "xy", "plot_with_settings-plot_brush", - "plot_with_settings-plot_main" -) - -hover_vals <- c( - 2.95684692272772, 2.34130469829519, - 789, 247, 789, 247, 1, 1, "1:5", "1:5", 1, 5, 1, 5, 28.0413099315069, 1583.52054794521, - 368.846473150198, 5.47945205479452, character(0), character(0) -) - -dbl_click_vals <- c( - 2.95684692272772, 2.34130469829519, - 789, 247, 789, 247, 1, 1, "1:5", "1:5", 1, 5, 1, 5, 28.0413099315069, 1583.52054794521, - 368.846473150198, 5.47945205479452, character(0), character(0) -) - -click_vals <- c( - 2.95941847746291, 1.9450111668517, - 790, 283, 790, 283, 1, 1, "1:5", "1:5", 1, 5, 1, 5, 28.0413099315069, 1583.52054794521, - 368.846473150198, 5.47945205479452, character(0), character(0) -) - -# one app opens for all tests -pws <- app_pws() -app <- AppDriver$new(pws, name = pws, height = 937, width = 1619) - -# tests plot click functionalities -testthat::test_that("plot_with_settings: click functionalities ggplot2", { - skip_on_cran() - skip_on_ci() - - # hovering - app$set_inputs( - `plot_with_settings-plot_hover` = hover_vals, - allow_no_input_binding_ = TRUE, priority_ = "event" - ) - - # double click - app$set_inputs( - `plot_with_settings-plot_dblclick` = dbl_click_vals, - allow_no_input_binding_ = TRUE, priority_ = "event" - ) - - # click - app$set_inputs( - `plot_with_settings-plot_click` = click_vals, - allow_no_input_binding_ = TRUE, priority_ = "event" - ) - - # brushing - app$set_inputs( - `plot_with_settings-plot_brush` = brush_vals, allow_no_input_binding_ = TRUE - ) - - vals <- app$get_values() - - # testing clicking / hovering / brushing - test_hover <- shiny::isolate(vals$export$plot_data$hover()) - testthat::expect_equal( - test_hover, - hover_vals - ) - - test_dblclick <- shiny::isolate(vals$export$plot_data$dblclick()) - testthat::expect_equal( - test_dblclick, - dbl_click_vals - ) - - test_click <- shiny::isolate(vals$export$plot_data$click()) - testthat::expect_equal( - test_click, - click_vals - ) - - test_brush <- shiny::isolate(vals$export$plot_data$brush()) - testthat::expect_equal( - test_brush, - brush_vals - ) - - # reset brush to character(0) for next tests - app$set_inputs( - `plot_with_settings-plot_brush` = character(0), - allow_no_input_binding_ = TRUE - ) -}) - -# test output that is returned (reactives and graphic encoded in base64) -testthat::test_that("plot_with_settings: output is returned", { - skip_on_cran() - skip_on_ci() - - vals <- app$get_values() - - # check if outputs are reactive - for (react_i in vals$export$"plot_data") { - testthat::expect_true(is(react_i, "reactive")) - } - - # check if output is desired plot - testthat::expect_type( - vals$output$`plot_with_settings-plot_main`$src, "character" - ) - # nolint start - testthat::expect_true( - grepl("data:image/png;base64,", - vals$output$`plot_with_settings-plot_main`$src, - fixed = TRUE - ) - ) - # nolint end -}) - -# download plots. expect_download() might not be stable, hence we test -# setting inputs and plot name changes -testthat::test_that("plot_with_settings: download functionality ggplot2", { - skip_on_cran() - skip_on_ci() - - # test default download options - app$click("plot_with_settings-downbutton-downl") - date <- strftime(Sys.time(), format = "%Y%m%d") - testthat::expect_true( - grepl( - paste0("plot_", date), - app$get_value(input = "plot_with_settings-downbutton-file_name") - ) - ) - testthat::expect_equal( - app$get_value(input = "plot_with_settings-downbutton-file_format"), "png" - ) - - # Download svg format - app$set_inputs(`plot_with_settings-downbutton-downl_state` = TRUE) - app$set_inputs(`plot_with_settings-downbutton-file_format` = "svg") - app$set_inputs(`plot_with_settings-downbutton-file_name` = "plot_svg") - app$click("plot_with_settings-downbutton-downl") - testthat::expect_equal( - app$get_value(input = "plot_with_settings-downbutton-file_name"), "plot_svg" - ) - - # change plot width and height - app$click("plot_with_settings-expbut") - app$set_inputs(`plot_with_settings-width` = 300) - app$set_inputs(`plot_with_settings-height` = 300) - app$click("plot_with_settings-downbutton-downl") - vals <- app$get_values() - - testthat::expect_equal(app$get_value(input = "plot_with_settings-downbutton-file_name"), "plot_svg") - testthat::expect_equal(isolate(vals$output$`plot_with_settings-plot_main`$height), 300) - testthat::expect_equal(isolate(vals$output$`plot_with_settings-plot_main`$width), 300) - - # reset - app$set_inputs(`plot_with_settings-expbut_state` = FALSE) - app$set_inputs(`plot_with_settings-downbutton-downl_state` = FALSE) -}) - -# downloading plot with modal -testthat::test_that("plot_with_settings: download ggplot2 modal", { - skip_on_cran() - skip_on_ci() - - # default downloading with modal - app$set_inputs( - `plot_with_settings-plot_hover` = character(0), - allow_no_input_binding_ = TRUE - ) - app$click("plot_with_settings-expand") - app$click("plot_with_settings-modal_downbutton-downl") - date <- strftime(Sys.time(), format = "%Y%m%d") - testthat::expect_true( - grepl( - paste0("plot_", date), - app$get_value(input = "plot_with_settings-modal_downbutton-file_name") - ) - ) - - # Change width and height of plot on modal - app$click("plot_with_settings-expand") - app$click("plot_with_settings-modal_downbutton-downl") - app$set_inputs(`plot_with_settings-height_in_modal` = 400) - app$set_inputs(`plot_with_settings-width_in_modal` = 500) - app$set_inputs(`plot_with_settings-modal_downbutton-file_name` = "plot_pdf") - app$set_inputs(`plot_with_settings-modal_downbutton-file_format` = "pdf") - app$set_inputs(`plot_with_settings-height_in_modal` = 1200) - app$set_inputs(`plot_with_settings-width_in_modal` = 750) - app$click("plot_with_settings-modal_downbutton-downl") - app$set_inputs(`plot_with_settings-modal_downbutton-downl_state` = TRUE) - - vals <- app$get_values() - testthat::expect_equal( - shiny::isolate(vals$input$`plot_with_settings-modal_downbutton-file_name`), - "plot_pdf" - ) - testthat::expect_equal( - shiny::isolate(vals$input$`plot_with_settings-height_in_modal`), 1200 - ) - testthat::expect_equal( - shiny::isolate(vals$input$`plot_with_settings-width_in_modal`), 750 - ) - - # nolint start - testthat::expect_equal( - shiny::isolate(vals$output$`plot_with_settings-plot_modal`$width), 750 - ) - testthat::expect_equal( - shiny::isolate(vals$output$`plot_with_settings-plot_modal`$height), 1200 - ) - # nolint end - - # change to svg - app$set_inputs(`plot_with_settings-modal_downbutton-downl_state` = TRUE) - app$set_inputs(`plot_with_settings-modal_downbutton-file_format` = "svg") - app$set_inputs( - `plot_with_settings-modal_downbutton-file_name` = "plot_svg_modal" - ) - testthat::expect_equal( - app$get_value(input = "plot_with_settings-modal_downbutton-file_name"), - "plot_svg_modal" - ) - - # reset - app$set_inputs(`plot_with_settings-modal_downbutton-downl_state` = FALSE) -}) - -# Testing hide and show button -testthat::test_that("plot_with_settings: hide/show button", { - skip_on_cran() - skip_on_ci() - - # nolint start - # visible on load - testthat::expect_true( - app$get_js( - "$('#plot_with_settings-plot-with-settings').is(':visible')" - ) - ) - - # hide - app$click("button") - testthat::expect_true( - app$get_js( - "$('#plot_with_settings-plot-with-settings').is(':hidden')" - ) - ) - - # unhide - app$click("button") - testthat::expect_true( - app$get_js( - "$('#plot_with_settings-plot-with-settings').is(':visible')" - ) - ) - # nolint end -}) - -# tests width warning displays when width too low, hides when not. -# note that warning is not hidden/visible in the usual sense. -# rather, it has the fa icon as a child or it does not. -# hence we're checking number of children. -testthat::test_that("plot_with_settings: width warning", { - skip_on_cran() - skip_on_ci() - - app$click("plot_with_settings-expbut") - - # starts out visible - testthat::expect_equal( - app$get_js("$('#plot_with_settings-width_warning').children().length"), 1 - ) - - # now hidden - app$set_inputs("plot_with_settings-width" = 600) - # output can take a bit to update - app$wait_for_value( - output = "plot_with_settings-width_warning", ignore = list("") - ) - testthat::expect_equal( - app$get_js("$('#plot_with_settings-width_warning').children().length"), 0 - ) - - # and back to visible - app$set_inputs("plot_with_settings-width" = 300) - app$wait_for_value( - output = "plot_with_settings-width_warning", ignore = list("") - ) - testthat::expect_equal( - app$get_js("$('#plot_with_settings-width_warning').children().length"), 1 - ) -}) - -# stop the app after finishing all tests -app$stop() diff --git a/tests/testthat/test-table_with_settings.R b/tests/testthat/test-table_with_settings.R new file mode 100644 index 00000000..211421b5 --- /dev/null +++ b/tests/testthat/test-table_with_settings.R @@ -0,0 +1,9 @@ +library(shinytest2) + +test_that("table-with-settings", { + skip_on_cran() + skip_on_ci() + + appdir <- system.file(package = "teal.widgets", "tws") + shinytest2::test_app(appdir) +})