Debian Codegraph @

tests/testthat/test-actionButton.R

raw
1 test_that("Action button accepts class arguments", {
2 make_button <- function(class) {
3 if (missing(class)) {
4 actionButton("id", "label")
5 } else {
6 actionButton("id", "label", class = class)
7 }
8 }
9 act <- make_button()
10 get_class <- function(act) {
11 act_html <- format(act)
12 regmatches(act_html, regexec("class=\"[^\"]\"", act_html))[[1]]
13 }
14 act_class <- get_class(act)
15 expect_equal(
16 get_class(make_button(NULL)), act_class
17 )
18 expect_equal(
19 get_class(make_button(NA)), act_class
20 )
21 expect_equal(
22 get_class(make_button("extra")), sub("\"$", " extra\"", act_class)
23 )
24 expect_equal(
25 get_class(make_button("extra extra2")), sub("\"$", " extra extra2\"", act_class)
26 )
27 })
28
29
30
31 test_that("Action link accepts class arguments", {
32 make_link <- function(class) {
33 if (missing(class)) {
34 actionLink("id", "label")
35 } else {
36 actionLink("id", "label", class = class)
37 }
38 }
39 act <- make_link()
40 get_class <- function(act) {
41 act_html <- format(act)
42 regmatches(act_html, regexec("class=\"[^\"]\"", act_html))[[1]]
43 }
44 act_class <- get_class(act)
45 expect_equal(
46 get_class(make_link(NULL)), act_class
47 )
48 expect_equal(
49 get_class(make_link(NA)), act_class
50 )
51 expect_equal(
52 get_class(make_link("extra")), sub("\"$", " extra\"", act_class)
53 )
54 expect_equal(
55 get_class(make_link("extra extra2")), sub("\"$", " extra extra2\"", act_class)
56 )
57 })
58
59 test_that("Action button allows icon customization", {
60 # No separator between icon and label
61 expect_snapshot(actionButton("foo", "Click me"))
62
63 # Should include separator between icon and label
64 expect_snapshot(
65 actionButton("foo", "Click me", icon = icon("star"))
66 )
67
68 # Warn on a non-HTML icon
69 expect_warning(
70 actionButton("foo", "Click me", icon = "not an icon"),
71 "non-HTML value was provided"
72 )
73
74 # Allows for arbitrary HTML as icon
75 btn <- expect_no_warning(
76 actionButton("foo", "Click me", icon = tags$svg())
77 )
78 btn2 <- expect_no_warning(
79 actionButton("foo", "Click me", icon = tagList(tags$svg()))
80 )
81 btn3 <- expect_no_warning(
82 actionButton("foo", "Click me", icon = list(tags$svg()))
83 )
84 btn4 <- expect_no_warning(
85 actionButton("foo", "Click me", icon = HTML("<svg></svg>"))
86 )
87
88 # Ignore newlines+indentation for comparison
89 as_character <- function(x) {
90 gsub("\\n\\s*", "", as.character(x))
91 }
92
93 expect_equal(as_character(btn), as_character(btn2))
94 expect_equal(as_character(btn2), as_character(btn3))
95 expect_equal(as_character(btn3), as_character(btn4))
96 })
97
98 test_that("actionLink uses .noWS to prevent underline rendering issues", {
99 # actionLink should generate compact HTML without whitespace between tags
100 # This prevents the underline from extending beyond the visible text
101
102 # Test without icon
103 link <- actionLink("test_link", "Click me")
104 link_html <- as.character(link)
105
106 # Verify no newlines/whitespace between closing > and opening <span
107 expect_false(
108 grepl(">\n\\s+<span", link_html),
109 info = "actionLink should not have whitespace between tags"
110 )
111
112 # Test with icon
113 link_icon <- actionLink("test_link2", "Click me", icon = icon("star"))
114 link_icon_html <- as.character(link_icon)
115
116 # Should also have no whitespace between icon span and label span
117 expect_false(
118 grepl(">\n\\s+<span", link_icon_html),
119 info = "actionLink with icon should not have whitespace between tags"
120 )
121 expect_snapshot(actionLink("foo", "Click me"))
122 expect_snapshot(actionLink("foo", "Click me", icon = icon("star")))
123 })