I have a shiny module that I want to test (see code below) that is part of an app that I have packaged as an R package.
The reactive value timeunit_rv
holds a state in the app using the module. It can be set by several controls, and as I update one, I want the other controls to be updated too.
I would like to write two tests, one for each of the two observeEvents
:
- Test that if I update
input$tunit
, thentimeunit_rv
is updated. - Test that if I update
timeunit_rv
, theninput$tunit
is updated.
I would like to use shiny::testServer
, if possible.
Questions:
- Am I correct that it is not possible to use
shiny::testServer
because that function does not take into account the ui, and hence runningupdateSelectInput(session, "tunit", selected = timeunit_rv())
will not updateinput$tunit
? - If testServer does not work, how do I best test my module? Would
shinytest
be my best option? How would I best useshinytest
to test the module in isolation from the rest of the app?
TimeUnitUI <- function(id) {
ns <- NS(id)
selectInput(ns("tunit"), "Unit", choices = c("s", "m", "h"), selected = "h")
}
timeUnitServer <- function(id, timeunit_rv) {
moduleServer(id, function(input, output, session) {
observeEvent(input$tunit, {
timeunit_rv(input$tunit)
})
observeEvent(timeunit_rv(), {
updateSelectInput(session, "tunit", selected = timeunit_rv())
})
})
}