From 260e3b42871cc1e1b141baf02290d8d1d675259d Mon Sep 17 00:00:00 2001 From: kartikeya kirar Date: Fri, 19 Apr 2024 22:11:36 +0530 Subject: [PATCH] 712 - `{shinytest2}` for `tm_t_crosstable` (#733) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Part of https://github.com/insightsengineering/teal.modules.general/issues/712 --------- Signed-off-by: kartikeya kirar Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com> Co-authored-by: Marcin <133694481+m7pr@users.noreply.github.com> --- .../test-shinytest2-tm_misssing_data.R | 2 +- .../test-shinytest2-tm_t_crosstable.R | 107 ++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-shinytest2-tm_t_crosstable.R diff --git a/tests/testthat/test-shinytest2-tm_misssing_data.R b/tests/testthat/test-shinytest2-tm_misssing_data.R index b85577d6a..0e98298b0 100644 --- a/tests/testthat/test-shinytest2-tm_misssing_data.R +++ b/tests/testthat/test-shinytest2-tm_misssing_data.R @@ -109,7 +109,7 @@ test_that("e2e - tm_missing_data: Check default settings and visibility of the c ) ) - testthat::expect_equal(app_driver$get_active_module_input("iris-combination_cutoff"), 2L) + testthat::expect_equal(app_driver$get_active_module_input("iris-combination_cutoff"), 1L) app_driver$set_active_module_input("iris-combination_cutoff", 10L) app_driver$expect_no_validation_error() diff --git a/tests/testthat/test-shinytest2-tm_t_crosstable.R b/tests/testthat/test-shinytest2-tm_t_crosstable.R new file mode 100644 index 000000000..ff47b2ed7 --- /dev/null +++ b/tests/testthat/test-shinytest2-tm_t_crosstable.R @@ -0,0 +1,107 @@ +app_driver_tm_t_crosstable <- function() { + data <- simple_cdisc_data() + init_teal_app_driver( + data = data, + modules = tm_t_crosstable( + label = "Cross Table", + x = teal.transform::data_extract_spec( + dataname = "ADSL", + select = teal.transform::select_spec( + label = "Select variable:", + choices = teal.transform::variable_choices(data[["ADSL"]], subset = function(data) { + idx <- !vapply(data, inherits, logical(1), c("Date", "POSIXct", "POSIXlt")) + return(names(data)[idx]) + }), + selected = "COUNTRY", + multiple = TRUE, + ordered = TRUE, + fixed = FALSE + ) + ), + y = teal.transform::data_extract_spec( + dataname = "ADSL", + select = teal.transform::select_spec( + label = "Select variable:", + choices = teal.transform::variable_choices(data[["ADSL"]], subset = function(data) { + idx <- vapply(data, is.factor, logical(1)) + return(names(data)[idx]) + }), + selected = "SEX", + multiple = FALSE, + fixed = FALSE + ) + ), + show_percentage = TRUE, + show_total = TRUE, + pre_output = NULL, + post_output = NULL, + basic_table_args = teal.widgets::basic_table_args( + subtitles = "Table generated by Crosstable Module" + ) + ), + timeout = 3000 + ) +} + +test_that("e2e - tm_t_crosstable: Initializes without errors", { + skip_if_too_deep(5) + app_driver <- app_driver_tm_t_crosstable() + + app_driver$expect_no_shiny_error() + + testthat::expect_equal( + app_driver$get_text("#teal-main_ui-root-active_tab > li.active > a"), + "Cross Table" + ) + + encoding_dataset <- app_driver$get_text("#teal-main_ui-root-cross_table .help-block") + testthat::expect_match(encoding_dataset, "Dataset:\\n *ADSL\\n", all = FALSE) + + app_driver$stop() +}) + +test_that("e2e - tm_t_crosstable: Verify module displays data table", { + skip_if_too_deep(5) + app_driver <- app_driver_tm_t_crosstable() + + # table + testthat::expect_true(app_driver$is_visible(selector = app_driver$active_module_element("table-table-with-settings"))) + + app_driver$stop() +}) + +test_that("e2e - tm_t_crosstable: Verify default values and settings (data_extracts) for data selection", { + skip_if_too_deep(5) + app_driver <- app_driver_tm_t_crosstable() + + # default variable selection + testthat::expect_equal( + app_driver$get_active_module_input("x-dataset_ADSL_singleextract-select"), + "COUNTRY" + ) + + testthat::expect_equal( + app_driver$get_active_module_input("y-dataset_ADSL_singleextract-select"), + "SEX" + ) + + # new variable selection + app_driver$set_active_module_input("x-dataset_ADSL_singleextract-select", c("SEX", "RACE", "COUNTRY")) + app_driver$set_active_module_input("y-dataset_ADSL_singleextract-select", "ETHNIC") + app_driver$expect_no_validation_error() + + app_driver$stop() +}) + +test_that("e2e - tm_t_crosstable: Change plot settings", { + skip_if_too_deep(5) + app_driver <- app_driver_tm_t_crosstable() + + app_driver$click(selector = app_driver$active_module_element("show_percentage")) + app_driver$expect_no_validation_error() + + app_driver$click(selector = app_driver$active_module_element("show_total")) + app_driver$expect_no_validation_error() + + app_driver$stop() +})