Skip to content

Commit

Permalink
tests: Update brand theming tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Dec 13, 2024
1 parent dfd71ab commit 6792e29
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 97 deletions.
2 changes: 1 addition & 1 deletion R/bs-theme-preset-brand.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ brand_resolve_preset <- function(brand, preset = NULL, version = NULL) {
preset_resolved <-
preset %||%
b_get(brand, "defaults", "shiny", "theme", "preset") %||%
switch_version(base_version, five = "shiny", default = "bootstrap")
switch_version(version_resolved, five = "shiny", default = "bootstrap")

resolve_bs_preset(preset_resolved, version = version_resolved)
}
Expand Down
9 changes: 5 additions & 4 deletions R/bs-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ bs_theme <- function(
version = version_default(),
preset = NULL,
...,
brand = TRUE,
brand = NULL,
bg = NULL,
fg = NULL,
primary = NULL,
Expand Down Expand Up @@ -187,12 +187,13 @@ bs_theme <- function(
resolve_bs_preset(preset, bootswatch, version = version)
}

version <- preset$version %||% version %||% version_default()

bundle <- bs_bundle(
bs_theme_init(preset$version),
bootstrap_bundle(preset$version),
bs_theme_init(version),
bootstrap_bundle(version),
bs_preset_bundle(preset),
bs_brand_bundle(brand, version = preset$version)
bs_brand_bundle(brand, version = version)
)

if (!is.null(preset$type)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/bs-theme-preset-builtin.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Condition
Error in `validate_builtin_preset_name()`:
! 'not-a-preset' is not a valid built-in theme preset provided by {bslib}.
i Available Bootstrap 5 themes are: 'shiny', 'brand'
i Available Bootstrap 5 themes are: 'shiny'

---

Expand Down
60 changes: 29 additions & 31 deletions tests/testthat/test-bs-theme-preset-brand.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,27 @@ describe("brand_resolve_preset()", {
path_with_parent_dir <- function(x) {
file.path(basename(dirname(x)), basename(x))
}
brand_found <- brand_resolve_preset(NULL)
brand_found$brand$path <- path_with_parent_dir(brand_found$brand$path)
brand_direct <- brand_resolve_preset("_brand.yml")
brand_direct$brand$path <- path_with_parent_dir(brand_direct$brand$path)
brand_found <- brand_resolve(TRUE)
brand_found$path <- path_with_parent_dir(brand_found$path)
brand_direct <- brand_resolve("_brand.yml")
brand_direct$path <- path_with_parent_dir(brand_direct$path)

expect_equal(brand_found, brand_direct)
expect_s3_class(brand_found, "bs_preset")
expect_equal(brand_found$type, "brand")
expect_equal(brand_found$name, "test-brand-yml")
expect_equal(brand_found$version, version_default())
expect_s3_class(brand_found$brand, "brand_yml")
expect_s3_class(brand_found, "brand_yml")
expect_equal(
brand_found$brand$path,
brand_found$path,
path_with_parent_dir(file.path(getwd(), "_brand.yml"))
)
expect_s3_class(brand_found$preset, "bs_preset")
direct_is_valid <<- TRUE
})

it("takes a list or a brand_yml object", {
expect_true(direct_is_valid)

brand_list <- brand_resolve_preset(list(meta = list(name = "test-brand-yml")))
brand_direct <- brand_resolve_preset("_brand.yml")
brand_direct$brand$path <- NULL # brand is equal other than via path
brand_obj <- brand_resolve_preset(brand_direct$brand)
brand_list <- brand_resolve(list(meta = list(name = "test-brand-yml")))
brand_direct <- brand_resolve("_brand.yml")
brand_direct$path <- NULL # brand is equal other than via path
brand_obj <- brand_resolve(brand_direct)

# brand_direct validated above
expect_equal(brand_list, brand_direct)
Expand All @@ -73,11 +68,12 @@ describe("brand_resolve_preset()", {
)

expected_base_preset <- resolve_bs_preset("flatly", version = "4")
brand_preset <- brand_resolve_preset(brand)
expect_equal(brand_preset$preset, expected_base_preset)
expect_equal(brand_preset$version, "4")
expect_equal(brand_preset$preset$name, "flatly")
expect_equal(brand_preset$preset$version, "4")
brand_preset <- brand_resolve(brand)
preset <- brand_resolve_preset(brand)
expect_equal(preset, expected_base_preset)
expect_equal(preset$version, "4")
expect_equal(preset$name, "flatly")
expect_equal(preset$version, "4")
})

it("throws if `brand.defaults.shiny.theme.preset: brand`", {
Expand All @@ -93,7 +89,7 @@ describe("brand_resolve_preset()", {

expect_error(
brand_resolve_preset(brand),
"cannot be 'brand'"
"is not a known preset"
)
})

Expand All @@ -109,11 +105,12 @@ describe("brand_resolve_preset()", {
)

expected_base_preset <- resolve_bs_preset("flatly", version = "4")
brand_preset <- brand_resolve_preset(brand)
expect_equal(brand_preset$preset, expected_base_preset)
expect_equal(brand_preset$version, "4")
expect_equal(brand_preset$preset$name, "flatly")
expect_equal(brand_preset$preset$version, "4")
brand <- brand_resolve(brand)
preset <- brand_resolve_preset(brand)
expect_equal(preset, expected_base_preset)
expect_equal(preset$version, "4")
expect_equal(preset$name, "flatly")
expect_equal(preset$version, "4")
})

it("uses brand.defaults.bootstrap.version", {
Expand All @@ -127,11 +124,12 @@ describe("brand_resolve_preset()", {
)

expected_base_preset <- resolve_bs_preset("bootstrap", version = "4")
brand_preset <- brand_resolve_preset(brand)
expect_equal(brand_preset$preset, expected_base_preset)
expect_equal(brand_preset$version, "4")
expect_equal(brand_preset$preset$name, "bootstrap")
expect_equal(brand_preset$preset$version, "4")
brand <- brand_resolve(brand)
preset <- brand_resolve_preset(brand)
expect_equal(preset, expected_base_preset)
expect_equal(preset$version, "4")
expect_equal(preset$name, "bootstrap")
expect_equal(preset$version, "4")
})
})

Expand Down
117 changes: 57 additions & 60 deletions tests/testthat/test-bs-theme-preset.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,66 +121,63 @@ describe("resolve_bs_preset()", {
expect_equal(shiny$version, "5")
})

it("returns a brand.yml presets for preset='brand'", {
withr::local_dir(withr::local_tempdir())
writeLines(
c(
"meta:",
" name: Test brand.yml"
),
"_brand.yml"
)

brand_default <- resolve_bs_preset(preset = "brand")
expect_s3_class(brand_default, "bs_preset")
expect_equal(brand_default$name, "Test brand.yml")
expect_equal(brand_default$version, "5")
expect_equal(brand_default$version, brand_default$preset$version)
expect_equal(brand_default$type, "brand")

brand_list <- resolve_bs_preset(
preset = list(brand = list(meta = list(name = "Test brand.yml")))
)
# Resolving a brand preset from a list is the same as from a path, except
# for the stored path
brand_default$brand$path <- NULL
expect_equal(brand_list, brand_default)

brand_v4 <- resolve_bs_preset(preset = "brand", version = 4)
expect_s3_class(brand_v4, "bs_preset")
expect_equal(brand_v4$name, "Test brand.yml")
expect_equal(brand_v4$version, "4")
expect_equal(brand_v4$version, brand_v4$preset$version)
expect_equal(brand_v4$preset$name, "bootstrap")
expect_equal(brand_v4$type, "brand")

writeLines(
c(
"meta:",
" name: Custom brand.yml",
"defaults:",
" shiny:",
" theme:",
" version: 4"
),
"brand-custom.yml"
)

brand_path <- resolve_bs_preset(preset = list(brand = "brand-custom.yml"))
expect_s3_class(brand_path, "bs_preset")
expect_equal(brand_path$name, "Custom brand.yml")
expect_equal(brand_path$version, "4")
expect_equal(brand_path$version, brand_path$preset$version)
expect_equal(brand_path$preset$name, "bootstrap")
expect_equal(brand_path$type, "brand")

expect_error(
resolve_bs_preset(preset = list("foo"))
)
expect_error(
resolve_bs_preset(preset = list(brand_yml = "foo"))
)
})
## TODO: Rewrite these tests!
# it("returns a brand.yml presets for brand=TRUE", {
# withr::local_dir(withr::local_tempdir())
# writeLines(
# c(
# "meta:",
# " name: Test brand.yml"
# ),
# "_brand.yml"
# )

# brand_default <- resolve_bs_preset(TRUE)
# preset <- brand_resolve_preset(brand_default)
# expect_s3_class(brand_default, "brand_yml")
# expect_equal(preset$name, "shiny")
# expect_equal(preset$version, "5")

# brand_list <- resolve_bs_preset(
# preset = list(brand = list(meta = list(name = "Test brand.yml")))
# )
# # Resolving a brand preset from a list is the same as from a path, except
# # for the stored path
# brand_default$brand$path <- NULL
# expect_equal(brand_list, brand_default)

# brand_v4 <- resolve_brand_preset(brand_list, preset = "bootstrap", version = 4)
# expect_s3_class(brand_v4, "bs_preset")
# expect_equal(brand_v4$name, "bootstrap")
# expect_equal(brand_v4$version, "4")

# writeLines(
# c(
# "meta:",
# " name: Custom brand.yml",
# "defaults:",
# " shiny:",
# " theme:",
# " version: 4"
# ),
# "brand-custom.yml"
# )

# brand_path <- resolve_bs_preset(preset = list(brand = "brand-custom.yml"))
# expect_s3_class(brand_path, "bs_preset")
# expect_equal(brand_path$name, "Custom brand.yml")
# expect_equal(brand_path$version, "4")
# expect_equal(brand_path$version, brand_path$preset$version)
# expect_equal(brand_path$preset$name, "bootstrap")
# expect_equal(brand_path$type, "brand")

# expect_error(
# resolve_bs_preset(preset = list("foo"))
# )
# expect_error(
# resolve_bs_preset(preset = list(brand_yml = "foo"))
# )
# })
})

test_that("bs_preset_bundle() returns `NULL` for default or empty preset", {
Expand Down

0 comments on commit 6792e29

Please sign in to comment.