|
| 1 | +library(mockery) |
| 2 | +library(testthat) |
| 3 | + |
| 4 | +setup_mod_select_strategy_server <- function(.env = parent.frame()) { |
| 5 | + strategies <- list( |
| 6 | + ip = tibble::tibble( |
| 7 | + name = c("Strategy A", "Strategy B"), |
| 8 | + strategy = c("a", "b") |
| 9 | + ), |
| 10 | + op = tibble::tibble( |
| 11 | + name = c("Strategy C", "Strategy D"), |
| 12 | + strategy = c("c", "d") |
| 13 | + ), |
| 14 | + ae = tibble::tibble( |
| 15 | + name = c("Strategy E", "Strategy F"), |
| 16 | + strategy = c("e", "f") |
| 17 | + ) |
| 18 | + ) |
| 19 | + m <- mock(strategies) |
| 20 | + |
| 21 | + local_mocked_bindings( |
| 22 | + "mod_select_strategy_get_strategies" = m, |
| 23 | + .env = .env |
| 24 | + ) |
| 25 | + |
| 26 | + m |
| 27 | +} |
| 28 | + |
| 29 | +test_that("ui", { |
| 30 | + testthat::local_mocked_bindings( |
| 31 | + "p_randomInt" = \(...) "X", |
| 32 | + .package = "shiny" |
| 33 | + ) |
| 34 | + |
| 35 | + ui <- mod_select_strategy_ui("test") |
| 36 | + |
| 37 | + expect_snapshot(ui) |
| 38 | +}) |
| 39 | + |
| 40 | + |
| 41 | +test_that("mod_select_strategy_get_strategies works", { |
| 42 | + # arrange |
| 43 | + m <- mock( |
| 44 | + list( |
| 45 | + "a" = "Strategy A (IP-AA-001)", |
| 46 | + "b" = "Strategy B (IP-AA-002)", |
| 47 | + "c" = "Strategy C (IP-EF-001)", |
| 48 | + "d" = "Strategy D (OP-AA-001)", |
| 49 | + "e" = "Strategy E (OP-AA-002)" |
| 50 | + ) |
| 51 | + ) |
| 52 | + local_mocked_bindings( |
| 53 | + "read_json" = m, |
| 54 | + .package = "jsonlite" |
| 55 | + ) |
| 56 | + expected <- list( |
| 57 | + "ip" = tibble::tribble( |
| 58 | + ~strategy , ~name , |
| 59 | + "a" , "Strategy A (IP-AA-001)" , |
| 60 | + "b" , "Strategy B (IP-AA-002)" , |
| 61 | + "c" , "Strategy C (IP-EF-001)" |
| 62 | + ), |
| 63 | + op = tibble::tribble( |
| 64 | + ~strategy , ~name , |
| 65 | + "d" , "Strategy D (OP-AA-001)" , |
| 66 | + "e" , "Strategy E (OP-AA-002)" |
| 67 | + ) |
| 68 | + ) |> |
| 69 | + dplyr::bind_rows(.id = "category") |> |
| 70 | + dplyr::group_nest(.data$category) |> |
| 71 | + tibble::deframe() |
| 72 | + |
| 73 | + # act |
| 74 | + actual <- mod_select_strategy_get_strategies() |
| 75 | + |
| 76 | + # assert |
| 77 | + expect_equal(actual, expected) |
| 78 | + expect_called(m, 1) |
| 79 | + expect_call( |
| 80 | + m, |
| 81 | + 1, |
| 82 | + jsonlite::read_json( |
| 83 | + app_sys("app", "data", "mitigators.json"), |
| 84 | + simplify_vector = TRUE |
| 85 | + ) |
| 86 | + ) |
| 87 | +}) |
| 88 | + |
| 89 | +test_that("server returns reactive", { |
| 90 | + # arrange |
| 91 | + setup_mod_select_strategy_server() |
| 92 | + |
| 93 | + test_server <- function(input, output, session) { |
| 94 | + selected_strategy <- mod_select_strategy_server("test") |
| 95 | + } |
| 96 | + |
| 97 | + # act |
| 98 | + shiny::testServer(test_server, { |
| 99 | + session$setInputs("test-strategy_select" = "a") |
| 100 | + expect_equal(selected_strategy(), "a") |
| 101 | + |
| 102 | + session$setInputs("test-strategy_select" = "b") |
| 103 | + expect_equal(selected_strategy(), "b") |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +test_that("it calls mod_select_strategy_get_strategies", { |
| 108 | + # arrange |
| 109 | + m <- setup_mod_select_strategy_server() |
| 110 | + |
| 111 | + # act |
| 112 | + shiny::testServer(mod_select_strategy_server, { |
| 113 | + # assert |
| 114 | + expect_called(m, 1) |
| 115 | + expect_args(m, 1) |
| 116 | + }) |
| 117 | +}) |
| 118 | + |
| 119 | +test_that("selected_category", { |
| 120 | + # arrange |
| 121 | + setup_mod_select_strategy_server() |
| 122 | + |
| 123 | + # act |
| 124 | + shiny::testServer(mod_select_strategy_server, { |
| 125 | + session$setInputs("strategy_category_select" = "ip") |
| 126 | + actual <- selected_category() |
| 127 | + |
| 128 | + # assert |
| 129 | + expect_equal(actual, "ip") |
| 130 | + }) |
| 131 | +}) |
| 132 | + |
| 133 | +test_that("it updates the strategy_select choices", { |
| 134 | + # arrange |
| 135 | + setup_mod_select_strategy_server() |
| 136 | + m <- mock() |
| 137 | + local_mocked_bindings( |
| 138 | + "updateSelectInput" = m, |
| 139 | + .package = "shiny" |
| 140 | + ) |
| 141 | + |
| 142 | + # act |
| 143 | + shiny::testServer(mod_select_strategy_server, { |
| 144 | + # assert |
| 145 | + session$setInputs("strategy_category_select" = "ip") |
| 146 | + expect_called(m, 1) |
| 147 | + expect_args( |
| 148 | + m, |
| 149 | + 1, |
| 150 | + session, |
| 151 | + "strategy_select", |
| 152 | + choices = c("Strategy A" = "a", "Strategy B" = "b") |
| 153 | + ) |
| 154 | + |
| 155 | + # assert |
| 156 | + session$setInputs("strategy_category_select" = "op") |
| 157 | + expect_called(m, 2) |
| 158 | + expect_args( |
| 159 | + m, |
| 160 | + 2, |
| 161 | + session, |
| 162 | + "strategy_select", |
| 163 | + choices = c("Strategy C" = "c", "Strategy D" = "d") |
| 164 | + ) |
| 165 | + }) |
| 166 | +}) |
0 commit comments