Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

* `{shinytest2}` now imports `{cli}` and no longer imports `{crayon}` (@olivroy, #399).

* Added [`{testthat}`'s snapshot file `transform=` parameter support](https://github.com/r-lib/testthat/pull/1530) to `AppDriver$expect_download()` and `AppDriver$expect_values()`. This allows for text transformations of the snapshot files before they are compared. (#403)


# shinytest2 0.3.2

## Bug / Improvements
Expand Down
4 changes: 3 additions & 1 deletion R/app-driver-expect-download.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ app_expect_download <- function(
output,
...,
compare = NULL,
name = NULL
name = NULL,
transform = NULL
) {
ckm8_assert_app_driver(self, private)
rlang::check_dots_empty()
Expand All @@ -111,6 +112,7 @@ app_expect_download <- function(
self,
private,
snapshot_info$download_path,
transform = transform,
compare = compare
)

Expand Down
4 changes: 3 additions & 1 deletion R/app-driver-expect-values.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ app_expect_values <- function(
output = missing_arg(),
export = missing_arg(),
name = NULL,
screenshot_args = missing_arg()
screenshot_args = missing_arg(),
transform = NULL
) {
ckm8_assert_app_driver(self, private)
rlang::check_dots_empty()
Expand Down Expand Up @@ -271,6 +272,7 @@ app_expect_values <- function(
self,
private,
json_path,
transform = transform,
compare = testthat::compare_file_text
)

Expand Down
9 changes: 8 additions & 1 deletion R/app-driver.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,11 @@ check_cran_deprecated <- function(
#' Defaults to the resolved `timeout` value during the `AppDriver` initialization.
#' @param timeout_ Amount of time to wait before giving up (milliseconds).
#' Defaults to the resolved `timeout` value during the `AppDriver` initialization.
#' @param transform Optionally, a function to scrub sensitive or stochastic text
#' from the output. Should take a character vector of lines as input and
#' return a modified character vector as output.
#' @param cran Deprecated. With `AppDriver` never testing on CRAN,
#' this parameter no longer has any effect.
#' this parameter no longer has any effect.
#' @param wait_ Wait until all reactive updates have completed?
#' @param hash_images If `TRUE`, images will be hashed before being returned.
#' Otherwise, all images will return their full data64 encoded value.
Expand Down Expand Up @@ -571,6 +574,7 @@ AppDriver <- R6Class(
export = missing_arg(),
screenshot_args = missing_arg(),
name = NULL,
transform = transform,
cran = deprecated()
) {
check_cran_deprecated(cran)
Expand All @@ -584,6 +588,7 @@ AppDriver <- R6Class(
export = export,
screenshot_args = screenshot_args,
name = name,
transform = transform
)
},
#' @description
Expand Down Expand Up @@ -750,6 +755,7 @@ AppDriver <- R6Class(
...,
compare = NULL,
name = NULL,
transform = NULL,
cran = deprecated()
) {
check_cran_deprecated(cran)
Expand All @@ -760,6 +766,7 @@ AppDriver <- R6Class(
output = output,
...,
compare = compare,
transform = transform,
name = name
)
},
Expand Down
2 changes: 2 additions & 0 deletions R/expect-snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ app__expect_snapshot_file <- function(
file,
variant,
name = fs::path_file(file),
transform = NULL,
compare = testthat::compare_file_binary
) {
ckm8_assert_app_driver(self, private)
Expand All @@ -43,6 +44,7 @@ app__expect_snapshot_file <- function(
name = name_safe,
cran = FALSE,
compare = compare,
transform = transform,
variant = self$get_variant()
),
expectation_failure = function(cnd) {
Expand Down
10 changes: 10 additions & 0 deletions man/AppDriver.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/_snaps/app-expect-file-transform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# transform can be passed to expect_snapshot_file()

"download-button.txt"

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Download content!
11 changes: 11 additions & 0 deletions tests/testthat/_snaps/app-expect-file-transform/002.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"input": {

},
"output": {
"txt": "(app loaded)"
},
"export": {

}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions tests/testthat/test-app-expect-file-transform.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require("shiny", quietly = TRUE, character.only = TRUE)

shiny_app <- shinyApp(
ui = fluidPage(
downloadButton("download_button_txt", "Download Button - Text"),
verbatimTextOutput("txt")
),
server = function(input, output) {
output$download_button_txt <- downloadHandler(
filename = function() {
"download-button.txt"
},
content = function(file) {
cat("Download content!\n", file = file)
}
)

output$txt <- renderText({
"(app loaded)"
})
}
)


test_that("transform can be passed to expect_snapshot_file()", {
app <- AppDriver$new(shiny_app)

download_called <- 0
values_called <- 0

app$expect_download("download_button_txt", transform = function(...) {
download_called <<- download_called + 1
list(...)[[1]]
})
app$expect_values(transform = function(...) {
values_called <<- values_called + 1
list(...)[[1]]
})

expect_equal(download_called, 1)
expect_equal(values_called, 1)
})