Trigger action button in test server function

hi all,

Is there a way to trigger action button inside test server .
If you see below, the default value is 5 and when the user clicks on button, the value 10 displays.

I need to test above scenario where test server triggers the action automatically when we run the test and should check expect_equal(myreactive(), 10) so it passes. Can we achieve this?

library(testthat)
library(shiny)

app <- shinyApp(
  ui = fluidPage(
    numericInput("x", "X", value = 5),
    textOutput("txt"),
    actionButton("press", "Submit")
  ),
  server = function(input, output, session) {
    myreactive <- reactive({
      input$x * 2
    })

    observeEvent(input$press,{
      output$txt <- renderText({
        paste0("I am ", myreactive())
      })
    })
  }
)



testServer(app, {
  session$setInputs(press = 1)
  expect_equal(myreactive(), 10)
})

you did trigger the action with press=1 but the value for numericInput seems undefined (even though I see that value = 5 is in the ui)
The following works

testServer(app, {
  session$setInputs(x = 5)
  session$setInputs(press = 1)
  expect_equal(myreactive(), 10)
})

Its little challenging..

The issue is line session$setInputs(press = 1) is of no use here in your code above
My thought is, even if I run below code (not setting input as 5), the test should pass. Because the default value is 5, and when the button is presses, the output is 10.... so my plan is not set any inputs here.... will the below code work?

testServer(app, {
  session$setInputs(press = 1)
  expect_equal(myreactive(), 10)
})

FYI just check your code once... even if you run below it still passes . Because the value is 5 but still there is no output since button is not triggered

testServer(app, {
 session$setInputs(x = 5)
  expect_equal(myreactive(), 10)
})

its not of 'no' use, as you've used it to generate a renderText (though its done in a questionable manner)

output$txt <- renderText({
req(input$press)
        paste0("I am ", myreactive())
      })

would be more conventional.

if myreactive should be dependent on the press then

    myreactive <- eventReactive(input$press,
                                {  input$x * 2})

eitherway, for this form of testing in the current version of testServer, even though you set a default value in the ui of 5, this is not used by the testServer, so thats why x=5 setInput is done there. I'm sure thats not ideal, but theres also nothing I can do to improve that given the toolset; you could perhaps raise a feature request and see what comes back.

No worries. I however followed your approach but still the test passes when I comment session$setInputs(press = 1)

Ideally it should fail right since the action button is not triggered ?

therefore do this which I suggested :

    myreactive <- eventReactive(input$press,
                                {  input$x * 2})

Makes sense.... thanks. But may i know why this does not happen when below code is put

 myreactive <- reactive({
      input$x * 2
    })

Because myreactive depends on nothing but x, i.e. not press

Thanks for explaining .....

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.