Skip to content
Draft
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
28 changes: 28 additions & 0 deletions tests/testthat/test-render.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,31 @@ test_that("reactlog_widget() inlines the page and defaults to shiny::reactlog()"
# no line breaks inside the base64 data uris
expect_false(grepl("base64,[A-Za-z0-9+/=]*\n", html))
})

test_that("reactlog_render() fills the template and copies the assets", {
file <- reactlog_render(structure(list(), version = "1"), time = FALSE)
html <- paste(readLines(file, warn = FALSE), collapse = "\n")

expect_false(grepl("__DATA__", html, fixed = TRUE))
expect_false(grepl("defaultLog.js", html, fixed = TRUE))
expect_true(grepl("window.__APP_TIME__ = String(\"FALSE\")", html, fixed = TRUE))
expect_true(file.exists(file.path(dirname(file), "reactlogAsset", "reactlog.js")))
})

test_that("reactlog_widget() passes `time`, `session_token`, and sizes on", {
skip_if_not_installed("htmlwidgets")
log <- structure(list(
list(action = "define", reactId = "r1", label = "label_session_a", session = "a"),
list(action = "define", reactId = "r2", label = "label_session_b", session = "b")
), version = "1")
w <- reactlog_widget(
log, time = FALSE, session_token = "a",
width = 300, height = 200, elementId = "my_log"
)

expect_true(grepl("String(\"FALSE\")", w$x$html, fixed = TRUE))
expect_true(grepl("label_session_a", w$x$html, fixed = TRUE))
expect_false(grepl("label_session_b", w$x$html, fixed = TRUE))
expect_equal(c(w$width, w$height), c(300, 200))
expect_equal(w$elementId, "my_log")
})
7 changes: 7 additions & 0 deletions tests/testthat/test-setReactLog.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("reactlog_enable() and reactlog_disable() set the shiny option", {
withr::local_options(shiny.reactlog = NULL)
reactlog_enable()
expect_true(getOption("shiny.reactlog"))
reactlog_disable(warn = FALSE)
expect_false(getOption("shiny.reactlog"))
})
13 changes: 13 additions & 0 deletions tests/testthat/test-shiny_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ test_that("iframe is displayed immediately", {
testthat::skip(paste0("Insufficient shiny version. Need >= ", shiny_version_required()))
}

# testServer() calls require(shiny); attach it first so nothing is printed
withr::local_package("shiny")
shiny::testServer(
reactlog_module_server,
args = list(
Expand All @@ -45,3 +47,14 @@ test_that("shiny version is read from Suggests", {
)
expect_length(shiny_version_required(), 1)
})

test_that("module UIs namespace their outputs and can drop the refresh button", {
skip_if_not_installed("htmlwidgets")
has <- function(ui, text) grepl(text, as.character(ui), fixed = TRUE)
for (ui in list(reactlog_module_ui, reactlog_widget_module_ui)) {
expect_true(has(ui(id = "x"), "id=\"x-refresh\""))
expect_false(has(ui(include_refresh = FALSE, id = "x"), "x-refresh"))
}
expect_true(has(reactlog_module_ui(id = "x"), "id=\"x-iframe\""))
expect_true(has(reactlog_widget_module_ui(id = "x"), "id=\"x-widget\""))
})
25 changes: 25 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
log_two_sessions <- function() {
structure(list(
list(action = "define", reactId = "r1", label = "global", session = NULL),
list(action = "define", reactId = "r2", label = "a", session = "a"),
list(action = "define", reactId = "r3", label = "b", session = "b")
), version = "1")
}

test_that("reactlog_write() returns json when `file` is NULL", {
json <- reactlog_write(log_two_sessions(), file = NULL)
labels <- vapply(jsonlite::fromJSON(json, simplifyVector = FALSE), `[[`, "", "label")
expect_equal(labels, c("global", "a", "b"))
})

test_that("reactlog_write() keeps global and matching session entries", {
json <- reactlog_write(log_two_sessions(), file = NULL, session_token = "a")
labels <- vapply(jsonlite::fromJSON(json, simplifyVector = FALSE), `[[`, "", "label")
expect_equal(labels, c("global", "a"))
})

test_that("reactlog_write() writes to a file", {
file <- withr::local_tempfile(fileext = ".json")
reactlog_write(log_two_sessions(), file = file)
expect_length(jsonlite::fromJSON(file, simplifyVector = FALSE), 3)
})