|
| 1 | +library(mockery) |
| 2 | +library(testthat) |
| 3 | + |
| 4 | +test_that("ui", { |
| 5 | + testthat::local_mocked_bindings( |
| 6 | + "p_randomInt" = \(...) "X", |
| 7 | + .package = "shiny" |
| 8 | + ) |
| 9 | + |
| 10 | + ui <- mod_show_strategy_text_ui("test") |
| 11 | + |
| 12 | + expect_snapshot(ui) |
| 13 | +}) |
| 14 | + |
| 15 | +test_that("server loads descriptions lookup", { |
| 16 | + # arrange |
| 17 | + m <- mock("descriptions_lookup") |
| 18 | + local_mocked_bindings( |
| 19 | + "read_json" = m, |
| 20 | + .package = "jsonlite" |
| 21 | + ) |
| 22 | + |
| 23 | + # act |
| 24 | + shiny::testServer(mod_show_strategy_text_server, { |
| 25 | + # assert |
| 26 | + expect_called(m, 1) |
| 27 | + expect_call( |
| 28 | + m, |
| 29 | + 1, |
| 30 | + jsonlite::read_json( |
| 31 | + app_sys("app", "data", "descriptions.json"), |
| 32 | + simplifyVector = TRUE |
| 33 | + ) |
| 34 | + ) |
| 35 | + }) |
| 36 | +}) |
| 37 | + |
| 38 | +test_that("strategy_text is rendered", { |
| 39 | + # arrange |
| 40 | + local_mocked_bindings( |
| 41 | + "read_json" = \(...) "descriptions_lookup", |
| 42 | + .package = "jsonlite" |
| 43 | + ) |
| 44 | + |
| 45 | + m1 <- mock("strategy text") |
| 46 | + m2 <- mock("html") |
| 47 | + |
| 48 | + local_mocked_bindings( |
| 49 | + "fetch_strategy_text" = m1, |
| 50 | + "convert_md_to_html" = m2 |
| 51 | + ) |
| 52 | + |
| 53 | + # act |
| 54 | + shiny::testServer( |
| 55 | + mod_show_strategy_text_server, |
| 56 | + args = list(selected_strategy = reactiveVal("a")), |
| 57 | + { |
| 58 | + # assert |
| 59 | + actual <- output$strategy_text |
| 60 | + |
| 61 | + expect_equal(actual, "html") |
| 62 | + |
| 63 | + expect_called(m1, 1) |
| 64 | + expect_args( |
| 65 | + m1, |
| 66 | + 1, |
| 67 | + "a", |
| 68 | + "descriptions_lookup" |
| 69 | + ) |
| 70 | + |
| 71 | + expect_called(m2, 1) |
| 72 | + expect_args(m2, 1, "strategy text") |
| 73 | + } |
| 74 | + ) |
| 75 | +}) |
| 76 | + |
| 77 | +test_that("strategy_text caches properly", { |
| 78 | + # arrange |
| 79 | + local_mocked_bindings( |
| 80 | + "read_json" = \(...) "descriptions_lookup", |
| 81 | + .package = "jsonlite" |
| 82 | + ) |
| 83 | + |
| 84 | + m1 <- mock("t1", "t2", "t3") |
| 85 | + m2 <- mock("h1", "h2", "h3") |
| 86 | + |
| 87 | + local_mocked_bindings( |
| 88 | + "fetch_strategy_text" = m1, |
| 89 | + "convert_md_to_html" = m2 |
| 90 | + ) |
| 91 | + |
| 92 | + # act |
| 93 | + shiny::testServer( |
| 94 | + mod_show_strategy_text_server, |
| 95 | + args = list(selected_strategy = reactiveVal()), |
| 96 | + { |
| 97 | + # assert |
| 98 | + selected_strategy("a") |
| 99 | + session$private$flush() |
| 100 | + a1 <- output$strategy_text |
| 101 | + |
| 102 | + selected_strategy("b") |
| 103 | + session$private$flush() |
| 104 | + a2 <- output$strategy_text |
| 105 | + |
| 106 | + selected_strategy("a") |
| 107 | + session$private$flush() |
| 108 | + a3 <- output$strategy_text |
| 109 | + |
| 110 | + expect_equal(a1, "h1") |
| 111 | + expect_equal(a2, "h2") |
| 112 | + expect_equal(a3, "h1") |
| 113 | + |
| 114 | + expect_called(m1, 2) |
| 115 | + expect_args(m1, 1, "a", "descriptions_lookup") |
| 116 | + expect_args(m1, 2, "b", "descriptions_lookup") |
| 117 | + |
| 118 | + expect_called(m2, 2) |
| 119 | + expect_args(m2, 1, "t1") |
| 120 | + expect_args(m2, 2, "t2") |
| 121 | + } |
| 122 | + ) |
| 123 | +}) |
0 commit comments