Skip to content

Commit

Permalink
Update tests post-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmlft committed Jan 23, 2025
1 parent dafaef7 commit d33e906
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
11 changes: 6 additions & 5 deletions app/tests/testthat/test-mod_datatable.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ test_that("datatable server works", {
expect_true(grepl(id, ns("")))
expect_true(grepl("test", ns("test")))

# NOTE: the return value of mod_datatable_server is the row selected from the datatable
# by a user. When running in tests, the output has 0 rows because there is no user interaction
selected_row <- session$getReturned()
expect_true(is.reactive(selected_row))
expect_s3_class(selected_row(), "data.frame")
# NOTE: the return value of mod_datatable_server is a vector of the concepts selected from
# the datatable by a user. When running in tests, the output has 0 rows because there is no
# user interaction
selected_concepts <- session$getReturned()
expect_true(is.reactive(selected_concepts))
expect_type(selected_concepts(), "NULL")
expect_s3_class(output$datatable, "json")

# Check that concepts table only shows concepts that have records for the selected date range
Expand Down
26 changes: 13 additions & 13 deletions app/tests/testthat/test-mod_plots.R
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
mock_concept_row <- reactiveVal()
mock_concept_ids <- reactiveVal()
mock_date_range <- reactiveVal(c("2019-04-01", "2024-08-01"))

test_that("mod_plots_server reacts to changes in the selected concept", {
testServer(
mod_plots_server,
args = list(selected_concepts = mock_concept_row, selected_dates = mock_date_range),
args = list(selected_concept_ids = mock_concept_ids, selected_dates = mock_date_range),
{
ns <- session$ns
expect_true(inherits(ns, "function"))
expect_true(grepl(id, ns("")))
expect_true(grepl("test", ns("test")))

selected_row <- list(concept_id = 3003573L, concept_name = "test")
mock_concept_row(selected_row) # update reactive value
selected_concept <- 3003573L
mock_concept_ids(selected_concept) # update reactive value
session$flushReact()
expect_identical(unique(filtered_summary_stats()$concept_id), selected_row$concept_id)
expect_identical(unique(filtered_summary_stats()$concept_id), selected_concept)

selected_row2 <- list(concept_id = 4276526L, concept_name = "test")
mock_concept_row(selected_row2) # update reactive value
selected_concept2 <- 4276526L
mock_concept_ids(selected_concept2) # update reactive value
session$flushReact()
expect_identical(unique(filtered_summary_stats()$concept_id), selected_row2$concept_id)
expect_identical(unique(filtered_summary_stats()$concept_id), selected_concept2)
}
)
})

test_that("mod_plots_server reacts to changes in the selected date range", {
testServer(
mod_plots_server,
args = list(selected_concepts = mock_concept_row, selected_dates = mock_date_range),
args = list(selected_concept_ids = mock_concept_ids, selected_dates = mock_date_range),
{
ns <- session$ns
expect_true(inherits(ns, "function"))
expect_true(grepl(id, ns("")))
expect_true(grepl("test", ns("test")))

mock_concept_row(list(concept_id = 4092281L, concept_name = "test"))
mock_concept_ids(4092281L)

selected_dates <- c("2019-01-01", "2019-12-31")
mock_date_range(selected_dates)
Expand Down Expand Up @@ -70,7 +70,7 @@ test_that("Date filtering works as expected", {
test_that("mod_plots_server fails when input is missing", {
testServer(
mod_plots_server,
args = list(selected_concepts = reactiveVal(NULL), selected_dates = mock_date_range),
args = list(selected_concept_ids = reactiveVal(NULL), selected_dates = mock_date_range),
{
# When no concept_id is selected, no output should be generated
# shiny::req() silently returns an error when the input is missing
Expand All @@ -82,9 +82,9 @@ test_that("mod_plots_server fails when input is missing", {
test_that("mod_plots_server generates an error when no data is available for the selected concept", {
testServer(
mod_plots_server,
args = list(selected_concepts = reactiveVal(NULL), selected_dates = mock_date_range),
args = list(selected_concept_ids = reactiveVal(NULL), selected_dates = mock_date_range),
{
mock_concept_row(list(concept_id = 9999999, concept_name = "idontexist"))
mock_concept_ids(9999999)
session$flushReact()
expect_error(output$summary_plot)
}
Expand Down
18 changes: 9 additions & 9 deletions app/tests/testthat/test-mod_select_for_export.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test_that("mod_select_for_export_server only reacts to button click", {
testServer(
mod_select_for_export_server,
# Add here your module params
args = list(concepts_data = reactiveVal()),
args = list(selected_concept_ids = reactiveVal()),
{
ns <- session$ns
# Pre-defined golem tests
Expand All @@ -24,7 +24,7 @@ test_that("mod_select_for_export_server only reacts to button click", {

# Update input data, the output should not be updated
n_selected <- 10
concepts_data(data.frame(concept_id = seq_len(n_selected)))
selected_concept_ids(seq_len(n_selected))
session$flushReact()
expect_length(out(), 0)

Expand All @@ -39,23 +39,23 @@ test_that("mod_select_for_export_server only reacts to button click", {
test_that("export selection does not remove previously selected items", {
# The module is designed to keep the previously selected items in the output
# even if the corresponding rows are deselected
testServer(mod_select_for_export_server, args = list(concepts_data = reactiveVal()), {
testServer(mod_select_for_export_server, args = list(selected_concept_ids = reactiveVal()), {
out <- session$getReturned()

# Initial selection followed by first button click
initial_selection <- data.frame(concept_id = c("foo", "bar", "baz"))
concepts_data(initial_selection)
initial_selection <- c("foo", "bar", "baz")
selected_concept_ids(initial_selection)
session$setInputs(add_to_export = 1)
session$flushReact()
expect_identical(out(), initial_selection$concept_id)
expect_identical(out(), initial_selection)

# Update selection, with new value and removing the old ones
# Second button click should add the new item to the output without erasing the previous ones
new_selection <- data.frame(concept_id = "hello")
concepts_data(new_selection)
new_selection <- "hello"
selected_concept_ids(new_selection)
session$setInputs(add_to_export = 2)
session$flushReact()
expect_identical(out(), c(initial_selection$concept_id, new_selection$concept_id))
expect_identical(out(), c(initial_selection, new_selection))
})
})

Expand Down

0 comments on commit d33e906

Please sign in to comment.