From d33e906c6c3dacd5d4bdcb010c71a29687a89971 Mon Sep 17 00:00:00 2001 From: Milan Malfait <38256462+milanmlft@users.noreply.github.com> Date: Thu, 23 Jan 2025 12:56:08 +0000 Subject: [PATCH] Update tests post-refactor --- app/tests/testthat/test-mod_datatable.R | 11 ++++---- app/tests/testthat/test-mod_plots.R | 26 +++++++++---------- .../testthat/test-mod_select_for_export.R | 18 ++++++------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/app/tests/testthat/test-mod_datatable.R b/app/tests/testthat/test-mod_datatable.R index a8e6a15..f267723 100644 --- a/app/tests/testthat/test-mod_datatable.R +++ b/app/tests/testthat/test-mod_datatable.R @@ -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 diff --git a/app/tests/testthat/test-mod_plots.R b/app/tests/testthat/test-mod_plots.R index 73e5e8a..60b4561 100644 --- a/app/tests/testthat/test-mod_plots.R +++ b/app/tests/testthat/test-mod_plots.R @@ -1,25 +1,25 @@ -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) } ) }) @@ -27,14 +27,14 @@ test_that("mod_plots_server reacts to changes in the selected concept", { 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) @@ -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 @@ -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) } diff --git a/app/tests/testthat/test-mod_select_for_export.R b/app/tests/testthat/test-mod_select_for_export.R index a9a6d8e..a45a825 100644 --- a/app/tests/testthat/test-mod_select_for_export.R +++ b/app/tests/testthat/test-mod_select_for_export.R @@ -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 @@ -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) @@ -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)) }) })