Using shinytest with modules - set reactives for testing?

I'm building a reprex to try to fully understand shinytest with testthat with modules. I have a module that imports data, one that plots the data, and another that filters the data. The small repo is here: https://github.com/MayaGans/module_demo

I'm trying to write a test for the filtered data (tests/table.R), I want to make sure the rendered table matches my expectations for the table. I know I can set an input using app$setInputs(filter_number = 6), then convert the table from html to a dataframe and see if they're the same.

BUT this module is reliant on the data uploaded in the data module! Is there a way, using shinytest to set a reactive like app$getReactive <- (datafile = iris)?

Here's the Test

library(shinytest)
library(testthat)
library(rvest)

context("Testing Table Output")

app <- ShinyDriver$new(".")

test_that("Sepal Length filtered by filter_number", {
  
  # set filter_number within table module to 6
  # https://shiny.rstudio.com/articles/integration-testing.html
  app$setInputs(filter_number = 6)
  # I want something like
  # app$setReactives(datafile = iris)
  # because my output needs a dataframe to filter by before it can filter

  # get the value of the filtered table and convert to dataframe
  # do we need to somehow access datafile here to generate the table
  # since that lives in a different module?
  output <- app$getValue(name = "filtered_table")
  
  my_df <- as.data.frame(read_html(output) %>% html_table(fill=TRUE))
  
  # create static test data output
  ir <- iris
  ir$Species <- as.character(iris$Species)
  test_data <- ir %>% filter(Sepal.Length > 6)
  
  # test that filtered data matches expected
  expect_identical(test_data, my_df)
})

app$stop()

Is this possible? Is there a different approach I could take?

1 Like

In addition I should mention that running this test results in an additional error Unable to find input binding for element with id filter_number I think this is perhaps due to name space issues because this is a module?

1 Like

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