diff --git a/001-hello/app.R b/001-hello/app.R index 887c50576d..542141eac1 100644 --- a/001-hello/app.R +++ b/001-hello/app.R @@ -34,20 +34,15 @@ ui <- fluidPage( # Define server logic required to draw a histogram ---- server <- function(input, output) { - # Histogram of the Old Faithful Geyser Data ---- - # with requested number of bins - # This expression that generates a histogram is wrapped in a call - # to renderPlot to indicate that: - # - # 1. It is "reactive" and therefore should be automatically - # re-executed when inputs (input$bins) change - # 2. Its output type is a plot - output$distPlot <- renderPlot({ + x <- faithful$waiting + + bins <- reactive({ + seq(min(x), max(x), length.out = input$bins + 1) + }) - x <- faithful$waiting - bins <- seq(min(x), max(x), length.out = input$bins + 1) + output$distPlot <- renderPlot({ - hist(x, breaks = bins, col = "#75AADB", border = "white", + hist(x, breaks = bins(), col = "#75AADB", border = "white", xlab = "Waiting time to next eruption (in mins)", main = "Histogram of waiting times") diff --git a/001-hello/tests/testthat.R b/001-hello/tests/testthat.R new file mode 100644 index 0000000000..edc089d427 --- /dev/null +++ b/001-hello/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(shiny) + +testthat::test_file("testthat/tests.R") diff --git a/001-hello/tests/testthat/tests.R b/001-hello/tests/testthat/tests.R new file mode 100644 index 0000000000..e70bfcf6c3 --- /dev/null +++ b/001-hello/tests/testthat/tests.R @@ -0,0 +1,9 @@ +library(testthat) +library(shiny) + +test_that("reactives update", { + testServer({ + session$setInputs(bins = 5) + expect_equal(bins(), seq(43, 96, length.out = 6)) + }) +}) diff --git a/005-sliders/tests/testthat.R b/005-sliders/tests/testthat.R new file mode 100644 index 0000000000..edc089d427 --- /dev/null +++ b/005-sliders/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(shiny) + +testthat::test_file("testthat/tests.R") diff --git a/005-sliders/tests/testthat/tests.R b/005-sliders/tests/testthat/tests.R new file mode 100644 index 0000000000..fda9342e38 --- /dev/null +++ b/005-sliders/tests/testthat/tests.R @@ -0,0 +1,26 @@ +library(testthat) +library(shiny) + +test_that("inputs are stored", { + testServer({ + session$setInputs( + integer = 593, + decimal = 0.6, + range = c(100,300), + format = 5000, + animation = 100 + ) + + # The data frame should have these properties + df <- sliderValues() + expect_equal(nrow(df), 5) + expect_equal(ncol(df), 2) + expect_is(df$Value, "character") + + # Updating an input should result in a new plot + plot1 <- output$values + session$setInputs(integer = 594) + plot2 <- output$values + expect_true(plot1 != plot2) + }) +}) diff --git a/050-kmeans-example/tests/testthat.R b/050-kmeans-example/tests/testthat.R new file mode 100644 index 0000000000..edc089d427 --- /dev/null +++ b/050-kmeans-example/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(shiny) + +testthat::test_file("testthat/tests.R") diff --git a/050-kmeans-example/tests/testthat/tests.R b/050-kmeans-example/tests/testthat/tests.R new file mode 100644 index 0000000000..78c95d1ed4 --- /dev/null +++ b/050-kmeans-example/tests/testthat/tests.R @@ -0,0 +1,16 @@ +library(testthat) +library(shiny) + +testServer({ + test_that("it works", { + session$setInputs( + xcol = "Sepal.Length", + ycol = "Petal.Length", + clusters = 4 + ) + expect_equal(colnames(selectedData()), c("Sepal.Length", "Petal.Length")) + expect_equal(nrow(clusters()$centers), 4) + session$setInputs(clusters = 3) + expect_equal(nrow(clusters()$centers), 3) + }) +}) diff --git a/119-namespaced-conditionalpanel-demo/app.R b/119-namespaced-conditionalpanel-demo/app.R index 0039644516..f6cec88966 100644 --- a/119-namespaced-conditionalpanel-demo/app.R +++ b/119-namespaced-conditionalpanel-demo/app.R @@ -33,11 +33,9 @@ myPlotUI <- function(id, label = "My Plot") { } myPlot <- function(input, output, session, deviates) { - output$scatterPlot <- renderPlot({ - x <- rnorm(input$n) - y <- rnorm(input$n) - plot(x, y) - }) + x <- reactive(rnorm(input$n)) + y <- reactive(rnorm(input$n)) + output$scatterPlot <- renderPlot(plot(x(), y())) } server <- function(input, output) { diff --git a/119-namespaced-conditionalpanel-demo/tests/testthat.R b/119-namespaced-conditionalpanel-demo/tests/testthat.R new file mode 100644 index 0000000000..edc089d427 --- /dev/null +++ b/119-namespaced-conditionalpanel-demo/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(shiny) + +testthat::test_file("testthat/tests.R") diff --git a/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R b/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R new file mode 100644 index 0000000000..e043d14ddb --- /dev/null +++ b/119-namespaced-conditionalpanel-demo/tests/testthat/tests.R @@ -0,0 +1,15 @@ +library(testthat) +library(shiny) +library(withr) + +# TODO Determine the ideal way for module-reliant test code to load an app +with_dir(shiny:::findApp(), { + local({ + source("app.R") + test_that("module works", { + testModule(myPlot, { + expect_true(TRUE) + }) + }) + }) +}) diff --git a/168-supporting-r-dir/tests/testthat.R b/168-supporting-r-dir/tests/testthat.R new file mode 100644 index 0000000000..9974530cae --- /dev/null +++ b/168-supporting-r-dir/tests/testthat.R @@ -0,0 +1,8 @@ +library(testthat) +library(shiny) + +# We explicitly pass environment() here because shiny::runTests() has sourced +# the files in R/, and the module we're testing was defined in those files. If +# we don't specify env, testthat uses an environment that doesn't include the +# module, and the tests fail. +testthat::test_file("testthat/tests.R", env = environment()) diff --git a/168-supporting-r-dir/tests/testthat/tests.R b/168-supporting-r-dir/tests/testthat/tests.R new file mode 100644 index 0000000000..360ea936e6 --- /dev/null +++ b/168-supporting-r-dir/tests/testthat/tests.R @@ -0,0 +1,13 @@ +library(testthat) +library(shiny) + +test_that("counter works", { + testModule(counter, { + inc <- function(x) if (is.null(x)) 0 else x+1 + expect_equal(count(), 0) + session$setInputs(button = inc(input$button)) + expect_equal(count(), 1) + expect_equal(output$out, "1") + expect_equal(session$returned(), 1) + }) +})