Debian Codegraph @

tests/testthat/test-tabPanel.R

raw
1 # tabsetPanel() et al. use p_randomInt() to generate ids (which uses withPrivateSeed()),
2 # so we need to fix Shiny's private seed in order to make their HTML output deterministic
3 navlist_panel <- function(...) {
4 withPrivateSeed(set.seed(100))
5 navlistPanel(...)
6 }
7 navbar_page <- function(...) {
8 withPrivateSeed(set.seed(100))
9 navbarPage(...)
10 }
11 tabset_panel <- function(...) {
12 withPrivateSeed(set.seed(100))
13 tabsetPanel(...)
14 }
15
16 expect_snapshot2 <- function(...) {
17 if (getRversion() < "3.6.0") {
18 skip("Skipping snapshots on R < 3.6 because of different RNG method")
19 }
20 if (packageVersion("htmltools") <= "0.5.6" && getRversion() > "4.3.1") {
21 skip("Skipping snapshots since htmltools is 'outdated'")
22 }
23 expect_snapshot(...)
24 }
25
26 expect_snapshot_bslib <- function(x, ...) {
27 expect_snapshot2(bslib_tags(x), ...)
28 }
29
30 # Simulates the UI tags that would be produced by
31 # shinyApp(bootstrapPage(theme), function(...) {})
32 bslib_tags <- function(ui, theme = bslib::bs_theme()) {
33 skip_if_not_installed("bslib", "0.5.1.9000")
34 old_theme <- getCurrentTheme()
35 on.exit(setCurrentTheme(old_theme), add = TRUE)
36 setCurrentTheme(theme)
37 htmltools::renderTags(ui)$html
38 }
39
40 panels <- list(
41 tabPanel("A", "a"),
42 tabPanel("B", "b", icon = icon("github")),
43 navbarMenu("Menu", tabPanel("C", "c"))
44 )
45
46 test_that("tabsetPanel() markup is correct", {
47 default <- tabset_panel(!!!panels)
48 pills <- tabset_panel(
49 !!!panels, type = "pills", selected = "B",
50 header = div(class = "content-header"),
51 footer = div(class = "content-footer")
52 )
53 # BS3
54 expect_snapshot2(default)
55 expect_snapshot2(pills)
56 # BS4
57 expect_snapshot_bslib(default)
58 expect_snapshot_bslib(pills)
59
60 # Make sure .active class gets added to both the .dropdown as well as the
61 # .dropdown-menu's tab
62 dropdown_active <- tabset_panel(!!!panels, selected = "C")
63 expect_snapshot2(dropdown_active)
64 })
65
66 test_that("navbarPage() markup is correct", {
67 nav_page <- navbar_page("Title", !!!panels)
68 expect_snapshot2(nav_page)
69 expect_snapshot_bslib(nav_page)
70 })
71
72 # navlistPanel() can handle strings, but the others can't
73 test_that("String input is handled properly", {
74 nav_list <- navlist_panel(!!!c(list("A header"), panels))
75 expect_snapshot2(nav_list)
76 expect_snapshot_bslib(nav_list)
77 expect_error(
78 tabsetPanel(!!!c(list("A header"), panels)),
79 "tabPanel"
80 )
81 })
82
83 test_that("Shiny.tag input produces a warning", {
84 panels3 <- c(list(div("A div")), panels)
85 expect_warning(tab_tags <- tabset_panel(!!!panels3))
86 # Carson March 12th, 2021: Yes, he 'empty nav' output here isn't
87 # sensible (which is why we now throw a warning), but it's probably
88 # too late to change the behavior (it could break user code to do
89 # anything different)
90 expect_snapshot2(tab_tags)
91 })
92
93 test_that("tabPanelBody validates it's input", {
94 expect_silent(tabPanelBody("a", "content1", "content2", icon = icon("table")))
95 expect_silent(tabPanelBody(value = "a", "content1", "content2", icon = icon("table")))
96
97 expect_error(tabPanelBody())
98 expect_error(tabPanelBody(NULL), "single, non-empty string")
99 expect_error(tabPanelBody(1), "single, non-empty string")
100 expect_error(tabPanelBody(TRUE), "single, non-empty string")
101 expect_error(tabPanelBody(NA), "single, non-empty string")
102 expect_error(tabPanelBody(NA_character_), "single, non-empty string")
103 expect_error(tabPanelBody(""), "single, non-empty string")
104 expect_error(tabPanelBody(letters[1:2]), "single, non-empty string")
105 })
106
107
108 # https://github.com/rstudio/shiny/issues/3352
109 test_that("tabItem titles can contain tag objects", {
110 title <- tagList(tags$i("Hello"), "world")
111 x <- tabsetPanel(tabPanel(title, "tab content"))
112 x <- renderTags(x)
113
114 # Result should contain (with different whitespace):
115 # "<a ....> <i>Hello</i> world"
116 # As opposed to:
117 # "<a ....>&lt;i&gt;Hello&lt;/i&gt; world
118 expect_match(x$html, "<a [^>]+>\\s*<i>Hello</i>\\s+world")
119 })