Skip to content

Commit acf5c04

Browse files
committed
add jumping test
1 parent e11b2da commit acf5c04

3 files changed

Lines changed: 94 additions & 1 deletion

File tree

inst/validation/specs.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ plot_specs <- specs_list(
2323
)
2424

2525
framework_specs <- specs_list(
26-
bookmarking = "The app's state gets restored correctly after bookmarking."
26+
bookmarking = "The app's state gets restored correctly after bookmarking.",
27+
jumping_feature = "The module allows to drill-down by jumping to the Patient Profile module."
2728
)
2829

2930
specs <- specs_list(

tests/testthat/setup.R

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,80 @@ vdoc <- local({
1111
})
1212
specs <- vdoc[["specs"]]
1313
# validation (F)
14+
15+
16+
17+
# YT#VH27d48516d3cbdadbf92a7b5e0860b78e#VH19ec235e56cdd18f129215603abf0ca6#
18+
19+
#' Test harness for communication with `dv.papo`.
20+
#'
21+
#' @param mod Parameterized instance of the module to test. Should produce valid output and not trigger a `shiny::req`.
22+
#' @param data Data matching the previous parameterization.
23+
#' @param trigger_input_id Fully namespaced input ID that, when set to a subject ID value,
24+
#' should make the module send `dv.papo` a message.
25+
test_communication_with_papo <- function(mod, data, trigger_input_id) {
26+
datasets <- shiny::reactive(data)
27+
28+
afmm <- list(
29+
data = list(DS = data),
30+
unfiltered_dataset = datasets,
31+
filtered_dataset = datasets,
32+
module_output = function() list(),
33+
module_names = list(papo = "Papo"),
34+
utils = list(switch2mod = function(id) NULL),
35+
dataset_metadata = list(name = shiny::reactive("dummy_dataset_name"))
36+
)
37+
38+
app_ui <- function() {
39+
shiny::fluidPage(mod[["ui"]](mod[["module_id"]]))
40+
}
41+
42+
app_server <- function(input, output, session) {
43+
ret_value <- mod[["server"]](afmm)
44+
45+
ret_value_update_count <- shiny::reactiveVal(0)
46+
shiny::observeEvent(ret_value[["subj_id"]](), ret_value_update_count(ret_value_update_count() + 1))
47+
48+
shiny::exportTestValues(
49+
ret_value = try(ret_value[["subj_id"]]()), # try because of https://github.com/rstudio/shiny/issues/3768
50+
update_count = ret_value_update_count()
51+
)
52+
return(ret_value)
53+
}
54+
55+
app <- shiny::shinyApp(ui = app_ui, server = app_server)
56+
57+
testthat::test_that("module adheres to send_subject_id_to_papo protocol" %>%
58+
vdoc[["add_spec"]](specs$framework_specs$jumping_feature), {
59+
app <- shinytest2::AppDriver$new(app, name = "test_send_subject_id_to_papo_protocol")
60+
61+
app$wait_for_idle()
62+
63+
# Module starts and sends no message
64+
exports <- app$get_values()[["export"]]
65+
testthat::expect_equal(exports[["update_count"]], 0)
66+
67+
trigger_subject_selection <- function(subject_id) {
68+
set_input_params <- append(
69+
as.list(setNames(paste0("[{\"key\":\"", subject_id, "\"}]"), trigger_input_id)),
70+
list(allow_no_input_binding_ = TRUE, priority_ = "event")
71+
)
72+
do.call(app$set_inputs, set_input_params)
73+
}
74+
75+
# Module sends exactly one message per trigger event, even if subject does not change
76+
subject_ids <- c("A", "A", "B")
77+
for (i in seq_along(subject_ids)) {
78+
trigger_subject_selection(subject_ids[[i]])
79+
app$wait_for_idle()
80+
81+
exports <- app$get_values()[["export"]]
82+
# Module outputs selection once
83+
testthat::expect_equal(exports[["ret_value"]], subject_ids[[i]])
84+
testthat::expect_equal(exports[["update_count"]], i)
85+
# print(exports[["update_count"]])
86+
}
87+
88+
app$stop()
89+
})
90+
}

tests/testthat/test-06-jumping.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
data_list <- list(
2+
dm = pharmaverseadam::adsl,
3+
lb = pharmaverseadam::adlb
4+
)
5+
6+
mod <- dv.edish::mod_edish(
7+
module_id = "mod",
8+
subject_level_dataset_name = "dm",
9+
lab_dataset_name = "lb",
10+
baseline_visit_val = "SCREENING 1",
11+
receiver_id = "papo"
12+
)
13+
14+
trigger_input_id <- "plotly_click-mod-plot"
15+
test_communication_with_papo(mod, data_list, trigger_input_id)

0 commit comments

Comments
 (0)