Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Add 318-navbar-colors #283

Merged
merged 9 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions R/data-apps-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ apps_deps_map <- list(
`315-bslib-input-switch` = c("bslib", "shiny", "shinytest2", "withr"),
`316-bslib-popovers` = c("bsicons", "bslib", "plotly", "rversions", "shiny", "shinycoreci", "shinytest2", "testthat", "withr"),
`317-nav-insert` = c("bslib", "htmltools", "leaflet", "rversions", "shiny", "shinycoreci", "shinytest2", "testthat", "withr"),
`318-navbar-colors` = c("bslib", "jsonlite", "rlang", "rprojroot", "shiny", "shinytest2", "testthat", "withr"),
`900-text-jster` = c("shiny", "shinyjster", "shinytest2"),
`901-button-jster` = c("shiny", "shinyjster", "shinytest2")
)
37 changes: 37 additions & 0 deletions inst/apps/318-navbar-colors/01-default/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
library(shiny)
library(bslib)

VERSION <- Sys.getenv("TEST_VERSION", "5")
PRESET <- Sys.getenv("TEST_PRESET", "shiny")

pre_code <- function(x) {
HTML(sprintf("<pre><code>%s</code></pre>", x))
}

ui <- page_navbar(
title = "My Simple App",
theme = bs_theme(
preset = PRESET,
version = VERSION
),
nav_panel(
"Home",
h2("Default navbar"),
pre_code(
sprintf('bs_theme(%s, preset="%s")', VERSION, PRESET)
),
p("Default navbar colors without any customization."),
if (VERSION >= 5) {
bslib::input_dark_mode(mode = "light", id = "color_mode")
}
),
nav_panel(
"About",
h2("About navbars")
)
)

server <- function(input, output, session) {
}

shinyApp(ui, server)
45 changes: 45 additions & 0 deletions inst/apps/318-navbar-colors/02-global/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
library(shiny)
library(bslib)

VERSION <- Sys.getenv("TEST_VERSION", "5")
PRESET <- Sys.getenv("TEST_PRESET", "shiny")

pre_code <- function(x) {
HTML(sprintf("<pre><code>%s</code></pre>", x))
}

ui <- page_navbar(
title = "My Simple App",
theme = bs_theme(
preset = PRESET,
version = VERSION,
navbar_bg = "#732400",
),
nav_panel(
"Home",
h2("Constant background color (Sass)"),
pre_code(
sprintf(
'bs_theme(%s, preset="%s", navbar_bg="#732400")',
VERSION,
PRESET
)
),
p(
"Test constant navbar background color set via $navbar-bg.",
"Navbar should be the same color in all variations regardless of preset."
),
if (VERSION >= 5) {
bslib::input_dark_mode(mode = "light", id = "color_mode")
}
),
nav_panel(
"About",
h2("About navbars")
)
)

server <- function(input, output, session) {
}

shinyApp(ui, server)
45 changes: 45 additions & 0 deletions inst/apps/318-navbar-colors/03-light-dark/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
library(shiny)
library(bslib)

VERSION <- Sys.getenv("TEST_VERSION", "5")
PRESET <- Sys.getenv("TEST_PRESET", "shiny")

pre_code <- function(x) {
HTML(sprintf("<pre><code>%s</code></pre>", x))
}

ui <- page_navbar(
title = "My Simple App",
theme = bs_theme(
preset = PRESET,
version = VERSION,
navbar_light_bg = "#b2d8ff",
navbar_dark_bg = "#00165d",
),
nav_panel(
"Home",
h2("Light/dark background color (Sass)"),
pre_code(
sprintf(
'bs_theme(%s, preset="%s", navbar_light_bg = "#b2d8ff", navbar_dark_bg = "#00165d")',
VERSION,
PRESET
)
),
p(
"Test light/dark navbar background color variants set via $navbar-light-bg and $navbar-dark-bg."
),
if (VERSION >= 5) {
bslib::input_dark_mode(mode = "light", id = "color_mode")
}
),
nav_panel(
"About",
h2("About navbars")
)
)

server <- function(input, output, session) {
}

shinyApp(ui, server)
42 changes: 42 additions & 0 deletions inst/apps/318-navbar-colors/04-navbar-options/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
library(shiny)
library(bslib)

VERSION <- Sys.getenv("TEST_VERSION", "5")
PRESET <- Sys.getenv("TEST_PRESET", "shiny")

pre_code <- function(x) {
HTML(sprintf("<pre><code>%s</code></pre>", x))
}

ui <- page_navbar(
title = "My Simple App",
theme = bs_theme(
preset = PRESET,
version = VERSION,
),
navbar_options = navbar_options(bg = "#732400"),
nav_panel(
"Home",
h2("Constant (R)"),
pre_code(
sprintf('bs_theme(%s, preset="%s")', VERSION, PRESET)
),
p(
"Navbar background color is set via",
code('navbar_options = navbar_options(bg = "#732400")'),
"and should be constant in all variations."
),
if (VERSION >= 5) {
bslib::input_dark_mode(mode = "light", id = "color_mode")
}
),
nav_panel(
"About",
h2("About navbars")
)
)

server <- function(input, output, session) {
}

shinyApp(ui, server)
44 changes: 44 additions & 0 deletions inst/apps/318-navbar-colors/05-primary-dark/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
library(shiny)
library(bslib)

VERSION <- Sys.getenv("TEST_VERSION", "5")
PRESET <- Sys.getenv("TEST_PRESET", "shiny")

pre_code <- function(x) {
HTML(sprintf("<pre><code>%s</code></pre>", x))
}

ui <- page_navbar(
title = "My Simple App",
theme = bs_theme(
preset = PRESET,
version = VERSION,
),
navbar_options = navbar_options(class = "bg-primary", theme = "dark"),
nav_panel(
"Home",
h2("Using class and theme"),
pre_code(
sprintf('bs_theme(%s, preset="%s")', VERSION, PRESET)
),
p(
"Navbar background color is set via",
code(
'navbar_options = navbar_options(class = "bg-primary", theme = "dark")'
),
"and should have the primary color as the background with light-colored text."
),
if (VERSION >= 5) {
bslib::input_dark_mode(mode = "light", id = "color_mode")
}
),
nav_panel(
"About",
h2("About navbars")
)
)

server <- function(input, output, session) {
}

shinyApp(ui, server)
1 change: 1 addition & 0 deletions inst/apps/318-navbar-colors/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shinytest2::test_app()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions inst/apps/318-navbar-colors/tests/testthat/setup-shinytest2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Load application support files into testing environment
shinytest2::load_app_env()

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
library(shinytest2)

release <- jsonlite::fromJSON("https://api.r-hub.io/rversions/resolve/release")
release <- paste0(
strsplit(release$version, ".", fixed = TRUE)[[1]][1:2],
collapse = "."
)

is_testing_on_ci <-
identical(Sys.getenv("CI"), "true") && testthat::is_testing()
is_mac_release <- identical(paste0("mac-", release), platform_variant())

# These tests are all screenshots that only happen on macos with latest R
if (!is_mac_release) {
test_that("tests pass; no screenshots required", expect_true(TRUE))
skip("Only performing screenshot tests on macOS with r-release.")
}

app_new <- function(app_name, version = 5, preset = "shiny") {
withr::local_envvar(list(TEST_VERSION = version, TEST_PRESET = preset))

AppDriver$new(
app_dir = rprojroot::find_package_root_file(
"inst",
"apps",
"318-navbar-colors",
app_name
),
name = sprintf("318-navbar-colors_%s_%s_bs%s", preset, app_name, version),
variant = NULL,
height = 800,
width = 1200,
view = rlang::is_interactive(),
options = list(bslib.precompiled = FALSE),
expect_values_screenshot_args = FALSE,
screenshot_args = list(
selector = "viewport",
delay = 0.5,
options = list(captureBeyondViewport = FALSE)
)
)
}

app_names <- c(
"01-default",
"02-global",
"03-light-dark",
"04-navbar-options",
"05-primary-dark"
)

for (app_name in app_names) {
for (version in unlist(bslib::versions())) {
if (app_name == "05-primary-dark" && version != "5") next

presets <- c(
bslib::builtin_themes(version),
bslib::bootswatch_themes(version)
)

for (preset in presets) {
if (app_name %in% app_names[2:4] && preset != "shiny") {
# These apps test static navbar colors, no need to test all presets
next
}

test_that(sprintf("%s - %s (bs%s)", app_name, preset, version), {
app <- app_new(app_name, version, preset)

app$expect_screenshot(name = "01-light")

if (version >= 5) {
app$run_js(
"document.getElementById('color_mode').setAttribute('mode', 'dark')"
)
app$expect_screenshot(name = "02-dark")
}

app$stop()
})
}
}
}
Loading