Files
civilyticsR/tests/testthat/test_flextable.R
T
jaredandClaude Sonnet 5.5 d6e3ba2fb4 fix(logo): take style_flextable_civilytics() defaults from civilytics_colors (#21)
Header, zebra, borders, and footer text now use navy_700, paper_2,
rule_strong, rule, and ink_3 instead of Bootstrap hex values. Tables built
with the defaults change appearance.

Closes #21

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-15 15:27:54 -04:00

108 lines
3.9 KiB
R

test_that("style_flextable_civilytics returns a styled flextable", {
skip_if_not_installed("flextable")
skip_if_not_installed("officer")
ft <- flextable::flextable(head(mtcars, 4))
ft <- flextable::add_footer_lines(ft, "Source: mtcars")
styled <- style_flextable_civilytics(ft)
expect_s3_class(styled, "flextable")
# Fixed layout is the documented end-state of the styling.
expect_identical(styled$properties$layout, "fixed")
})
test_that("style_flextable_civilytics is idempotent on a 1-row table (no zebra error)", {
skip_if_not_installed("flextable")
skip_if_not_installed("officer")
ft <- flextable::flextable(head(mtcars, 1))
expect_no_error(once <- style_flextable_civilytics(ft))
# Re-styling an already-styled table must not error either.
expect_no_error(twice <- style_flextable_civilytics(once))
expect_s3_class(twice, "flextable")
})
test_that("zebra = FALSE still returns a valid flextable", {
skip_if_not_installed("flextable")
skip_if_not_installed("officer")
ft <- flextable::flextable(head(mtcars, 6))
expect_s3_class(style_flextable_civilytics(ft, zebra = FALSE), "flextable")
})
test_that("save_branded_flextable_png writes a PNG with plausible dimensions", {
skip_if_not_installed("flextable")
skip_if_not_installed("officer")
skip_if_not_installed("ragg")
skip_if_not_installed("png")
ft <- flextable::flextable(head(mtcars, 4))
ft <- flextable::add_footer_lines(ft, "Source: mtcars")
ft <- style_flextable_civilytics(ft)
path <- tempfile(fileext = ".png")
on.exit(unlink(path), add = TRUE)
out <- save_branded_flextable_png(ft, path)
expect_identical(out, path)
expect_true(file.exists(path))
dims <- dim(png::readPNG(path))
# height x width x channels — a real table is at least a few hundred px each.
expect_gt(dims[1], 50)
expect_gt(dims[2], 50)
})
test_that("save_branded_flextable_png with logo = FALSE skips stamping but still writes", {
skip_if_not_installed("flextable")
skip_if_not_installed("officer")
skip_if_not_installed("ragg")
skip_if_not_installed("png")
ft <- style_flextable_civilytics(flextable::flextable(head(mtcars, 3)))
path <- tempfile(fileext = ".png")
on.exit(unlink(path), add = TRUE)
save_branded_flextable_png(ft, path, logo = FALSE)
expect_true(file.exists(path))
expect_gt(file.info(path)$size, 0)
})
test_that("extra_height increases the exported PNG height", {
skip_if_not_installed("flextable")
skip_if_not_installed("officer")
skip_if_not_installed("ragg")
skip_if_not_installed("png")
ft <- style_flextable_civilytics(flextable::flextable(head(mtcars, 4)))
path_small <- tempfile(fileext = ".png")
path_big <- tempfile(fileext = ".png")
on.exit(unlink(c(path_small, path_big)), add = TRUE)
save_branded_flextable_png(ft, path_small, logo = FALSE, extra_height = 0)
save_branded_flextable_png(ft, path_big, logo = FALSE, extra_height = 2)
expect_gt(dim(png::readPNG(path_big))[1], dim(png::readPNG(path_small))[1])
})
test_that("style_flextable_civilytics defaults come from civilytics_colors (#21)", {
defaults <- formals(style_flextable_civilytics)
expect_identical(eval(defaults$header_bg), civilytics_colors[["navy_700"]])
expect_identical(eval(defaults$zebra_bg), civilytics_colors[["paper_2"]])
expect_identical(eval(defaults$outer_border_color), civilytics_colors[["rule_strong"]])
expect_identical(eval(defaults$inner_border_color), civilytics_colors[["rule"]])
expect_identical(eval(defaults$footer_color), civilytics_colors[["ink_3"]])
})
test_that("style_flextable_civilytics paints the header with brand navy", {
skip_if_not_installed("flextable")
skip_if_not_installed("officer")
styled <- style_flextable_civilytics(flextable::flextable(head(mtcars, 3)))
header_fill <- unique(as.vector(styled$header$styles$cells$background.color$data))
expect_identical(toupper(header_fill), toupper(civilytics_colors[["navy_700"]]))
})