Debian Codegraph @

tests/testthat/test-test-server-app.R

raw
1 test_that("testServer works with dir app", {
2 # app.R
3 testServer(test_path("..", "test-modules", "06_tabsets"), {
4 session$setInputs(dist="norm", n=5)
5 expect_length(d(), 5)
6
7 session$setInputs(dist="unif", n=6)
8 expect_length(d(), 6)
9 })
10
11 # server.R
12 testServer(test_path("..", "test-modules", "server_r"), {
13 session$setInputs(dist="norm", n=5)
14 expect_length(d(), 5)
15
16 session$setInputs(dist="unif", n=6)
17 expect_length(d(), 6)
18 })
19 })
20
21 test_that("testServer works when referencing external globals", {
22 # If global is defined at the top of app.R outside of the server function.
23 testServer(test_path("..", "test-modules", "06_tabsets"), {
24 expect_equal(get("global", session$env), 123)
25 })
26 })
27
28 test_that("testServer defaults to the app at .", {
29 curwd <- getwd()
30 on.exit(setwd(curwd))
31 setwd(test_path("..", "test-modules", "06_tabsets"))
32 testServer(expr = {
33 expect_equal(get("global", session$env), 123)
34 })
35 })
36
37 test_that("runTests works with a dir app that calls modules and uses testServer", {
38 app <- test_path("..", "test-modules", "12_counter")
39 run <- testthat::expect_output(
40 print(runTests(app)),
41 "Shiny App Test Results\\n\\v Success\\n - 12_counter/tests/testthat\\.R"
42 )
43 expect_true(all(run$pass))
44 })
45
46 test_that("runTests works with a dir app that calls modules that return reactives and use brushing", {
47 skip_if_not_installed("ggplot2")
48 app <- test_path("..", "test-modules", "107_scatterplot")
49 run <- testthat::expect_output(
50 print(runTests(app)),
51 "Shiny App Test Results\\n\\v Success\\n - 107_scatterplot/tests/testthat\\.R"
52 )
53 expect_true(all(run$pass))
54 })
55
56 test_that("a Shiny app object with a module inside can be tested", {
57
58 counterUI <- function(id, label = "Counter") {
59 ns <- NS(id)
60 tagList(
61 actionButton(ns("button"), label = label),
62 verbatimTextOutput(ns("out"))
63 )
64 }
65
66 counterServer <- function(id) {
67 moduleServer(
68 id,
69 function(input, output, session) {
70 count <- reactiveVal(0)
71 observeEvent(input$button, {
72 count(count() + 1)
73 })
74 output$out <- renderText({
75 count()
76 })
77 count
78 }
79 )
80 }
81
82 ui <- fluidPage(
83 textInput("number", "A number"),
84 textOutput("numberDoubled"),
85 counterUI("counter1", "Counter #1"),
86 counterUI("counter2", "Counter #2")
87 )
88 server <- function(input, output, session) {
89 counterServer("counter1")
90 counterServer("counter2")
91 doubled <- reactive( { as.integer(input$number) * 2 })
92 output$numberDoubled <- renderText({ doubled() })
93 }
94 app <- shinyApp(ui, server)
95
96 testServer(app, {
97 session$setInputs(number = "42")
98 expect_equal(doubled(), 84)
99 })
100 })
101
102 test_that("It's an error to pass arguments to a server", {
103 expect_error(testServer(test_path("..", "test-modules", "06_tabsets"), {}, args = list(an_arg = 123)))
104 })