@@ -11,3 +11,80 @@ vdoc <- local({
1111})
1212specs <- 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+ }
0 commit comments