Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
117 changes: 117 additions & 0 deletions tests/testthat/test-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,120 @@ testthat::test_that("init throws when dataname in filter incompatible w/ datanam
"Filter 'iris Species' refers to dataname not available in 'data'"
)
})

# filter module_specific tests ----
testthat::test_that("init throws error when filter mapping has invalid module names", {
testthat::expect_error(
init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module(label = "mod1")),
filter = teal_slices(
teal_slice(dataname = "iris", varname = "Species"),
module_specific = TRUE,
mapping = list(nonexistent_module = "iris Species")
)
),
"Some module names in the mapping arguments don't match module labels"
)
})

testthat::test_that("init throws error when modules have duplicate labels with module_specific filter", {
testthat::expect_error(
init(
data = teal.data::teal_data(iris = iris),
modules = modules(
example_module(label = "duplicate_label"),
example_module(label = "duplicate_label")
),
filter = teal_slices(
teal_slice(dataname = "iris", varname = "Species"),
module_specific = TRUE,
mapping = list(duplicate_label = "iris Species")
)
),
"Module labels should be unique when teal_slices\\(mapping = TRUE\\)"
)
})

testthat::test_that("init accepts valid module_specific filter with proper mapping", {
testthat::expect_no_error(
init(
data = teal.data::teal_data(iris = iris, mtcars = mtcars),
modules = modules(
example_module(label = "iris_module"),
example_module(label = "mtcars_module")
),
filter = teal_slices(
teal_slice(dataname = "iris", varname = "Species"),
teal_slice(dataname = "mtcars", varname = "cyl"),
module_specific = TRUE,
mapping = list(
iris_module = "iris Species",
global_filters = "mtcars cyl"
)
)
)
)
})

# reporter tests ----
testthat::test_that("init accepts NULL reporter to disable reporting", {
testthat::expect_no_error(
init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module()),
reporter = NULL
)
)
})

testthat::test_that("init accepts Reporter object", {
testthat::expect_no_error(
init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module()),
reporter = teal.reporter::Reporter$new()
)
)
})

# deprecated parameters tests ----
testthat::test_that("init shows deprecation warning for title parameter", {
lifecycle::expect_deprecated(
init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module()),
title = "Deprecated Title"
)
)
})

testthat::test_that("init shows deprecation warning for header parameter", {
lifecycle::expect_deprecated(
init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module()),
header = tags$div("Deprecated Header")
)
)
})

testthat::test_that("init shows deprecation warning for footer parameter", {
lifecycle::expect_deprecated(
init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module()),
footer = tags$div("Deprecated Footer")
)
)
})

testthat::test_that("init shows deprecation warning for id parameter", {
lifecycle::expect_deprecated(
init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module()),
id = "test_id"
)
)
})
180 changes: 180 additions & 0 deletions tests/testthat/test-shinytest2-teal_modifiers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
testthat::skip_if_not_installed("shinytest2")
testthat::skip_if_not_installed("rvest")

testthat::describe("e2e: modify_title", {
testthat::it("displays custom title in the app", {
skip_if_too_deep(5)

app <- init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module())
) |>
modify_title(title = "Custom Test Title")

app_driver <- TealAppDriver$new(app)

# Check that the title is present in the page
page_title <- app_driver$get_text("head title")
testthat::expect_equal(page_title, "Custom Test Title")

app_driver$stop()
})

testthat::it("displays custom favicon in the app", {
skip_if_too_deep(5)

custom_favicon <- "https://raw.githubusercontent.com/insightsengineering/hex-stickers/main/PNG/teal.png"
app <- init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module())
) |>
modify_title(title = "Test App", favicon = custom_favicon)

app_driver <- TealAppDriver$new(app)

# Check that favicon link is present
testthat::expect_true(app_driver$is_visible("link[rel='icon']"))

app_driver$stop()
})
})

testthat::describe("e2e: modify_header", {
testthat::it("displays custom header in the app", {
skip_if_too_deep(5)

app <- init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module())
) |>
modify_header(element = tags$h1("Custom App Header", id = "test-header"))

app_driver <- TealAppDriver$new(app)

# Check that the header content is visible
testthat::expect_true(app_driver$is_visible("#test-header"))
header_text <- app_driver$get_text("#test-header")
testthat::expect_equal(header_text, "Custom App Header")

app_driver$stop()
})
})

testthat::describe("e2e: modify_footer", {
testthat::it("displays custom footer in the app", {
skip_if_too_deep(5)

app <- init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module())
) |>
modify_footer(element = tags$p("Custom Footer Text", id = "test-footer"))

app_driver <- TealAppDriver$new(app)

# Check that the footer content is visible
testthat::expect_true(app_driver$is_visible("#test-footer"))
footer_text <- app_driver$get_text("#test-footer")
testthat::expect_equal(footer_text, "Custom Footer Text")

app_driver$stop()
})
})

testthat::describe("e2e: add_landing_modal", {
testthat::it("displays landing modal on app startup", {
skip_if_too_deep(5)

app <- init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module())
) |>
add_landing_modal(
title = "Welcome to the App",
content = "Please read these instructions before proceeding."
)

app_driver <- TealAppDriver$new(app)

# Check that modal is visible on startup
testthat::expect_true(app_driver$is_visible(".modal"))

# Check modal title
modal_title <- app_driver$get_text(".modal-title")
testthat::expect_equal(modal_title, "Welcome to the App")

# Check modal content is present
modal_body <- app_driver$get_text(".modal-body")
testthat::expect_match(modal_body, "Please read these instructions")

app_driver$stop()
})

testthat::it("modal can be dismissed", {
skip_if_too_deep(5)

app <- init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module())
) |>
add_landing_modal(
title = "Welcome",
content = "Test content",
footer = modalButton("Accept")
)

app_driver <- TealAppDriver$new(app)

# Modal should be visible initially
testthat::expect_true(app_driver$is_visible(".modal"))

# Click the Accept button
app_driver$click("button:contains('Accept')")

# Wait a moment for modal to close
Sys.sleep(0.5)

# Modal should no longer be visible
testthat::expect_false(app_driver$is_visible(".modal"))

app_driver$stop()
})
})

testthat::describe("e2e: combined modifiers", {
testthat::it("displays all customizations when chained together", {
skip_if_too_deep(5)

app <- init(
data = teal.data::teal_data(iris = iris),
modules = modules(example_module())
) |>
modify_title(title = "Complete Custom App") |>
modify_header(element = tags$div("Custom Header", id = "custom-header")) |>
modify_footer(element = tags$div("Custom Footer", id = "custom-footer")) |>
add_landing_modal(title = "Welcome", content = "Welcome message")

app_driver <- TealAppDriver$new(app)

# Check title
page_title <- app_driver$get_text("head title")
testthat::expect_equal(page_title, "Complete Custom App")

# Check modal is visible
testthat::expect_true(app_driver$is_visible(".modal"))

# Dismiss modal
app_driver$click("button:contains('Accept')")
Sys.sleep(0.5)

# Check header is visible
testthat::expect_true(app_driver$is_visible("#custom-header"))
testthat::expect_equal(app_driver$get_text("#custom-header"), "Custom Header")

# Check footer is visible
testthat::expect_true(app_driver$is_visible("#custom-footer"))
testthat::expect_equal(app_driver$get_text("#custom-footer"), "Custom Footer")

app_driver$stop()
})
})
Loading