Skip to content

Commit d75ddea

Browse files
committed
Add unit tests
1 parent e63c0fb commit d75ddea

File tree

3 files changed

+143
-7
lines changed

3 files changed

+143
-7
lines changed

R/widget.R

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
247247
},
248248
#' @description
249249
#' Render the widget.
250-
render = function() {
250+
render = function(return_widget = FALSE) {
251251
if(private$.mode == "static") {
252-
invoke_static(self)
252+
invoke_static(self, return_widget = return_widget)
253253
} else if(private$.mode == "gadget") {
254-
invoke_gadget(self)
254+
invoke_gadget(self, return_widget = return_widget)
255255
} else if(private$.mode == "dynamic") {
256-
invoke_dynamic(self)
256+
invoke_dynamic(self, return_widget = return_widget)
257257
} else {
258258
stop("render is meant for use with static, gadget, and dynamic modes")
259259
}
@@ -262,18 +262,21 @@ AnyHtmlWidget <- R6::R6Class("AnyHtmlWidget",
262262
)
263263

264264
#' @keywords internal
265-
invoke_static <- function(w) {
265+
invoke_static <- function(w, return_widget = FALSE) {
266266
w <- the_anyhtmlwidget(
267267
esm = w$.get_esm(),
268268
values = w$.get_values(),
269269
width = w$.get_width(),
270270
height = w$.get_height()
271271
)
272+
if(return_widget) {
273+
return(w)
274+
}
272275
print(w)
273276
}
274277

275278
#' @keywords internal
276-
invoke_dynamic <- function(w) {
279+
invoke_dynamic <- function(w, return_widget = FALSE) {
277280
w$.start_server()
278281
w <- the_anyhtmlwidget(
279282
esm = w$.get_esm(),
@@ -283,11 +286,14 @@ invoke_dynamic <- function(w) {
283286
port = w$.get_port(),
284287
host = w$.get_host()
285288
)
289+
if(return_widget) {
290+
return(w)
291+
}
286292
print(w)
287293
}
288294

289295
#' @keywords internal
290-
invoke_gadget <- function(w) {
296+
invoke_gadget <- function(w, return_widget = FALSE) {
291297
ui <- shiny::tagList(
292298
anyhtmlwidget_output(output_id = "my_widget", width = '100%', height = '100%')
293299
)
@@ -322,6 +328,10 @@ invoke_gadget <- function(w) {
322328
})
323329
}
324330

331+
if(return_widget) {
332+
return(list(ui = ui, server = server))
333+
}
334+
325335
shiny::runGadget(ui, server)
326336
}
327337

tests/testthat.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library(testthat)
2+
library(anyhtmlwidget)
3+
4+
test_check("anyhtmlwidget")

tests/testthat/test-widget.R

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
library(anyhtmlwidget)
2+
3+
esm <- "
4+
function render({ el, model }) {
5+
el.style.border = '4px solid red';
6+
let count = () => model.get('count');
7+
let btn = document.createElement('button');
8+
btn.innerHTML = `count is ${count()}`;
9+
btn.addEventListener('click', () => {
10+
model.set('count', count() + 1);
11+
model.save_changes();
12+
});
13+
model.on('change:count', () => {
14+
btn.innerHTML = `count is ${count()}`;
15+
});
16+
el.appendChild(btn);
17+
}
18+
export default { render };
19+
"
20+
21+
test_that("counter widget can be instantiated", {
22+
w <- AnyHtmlWidget$new(
23+
.esm = esm,
24+
.mode = "static",
25+
.height='400px',
26+
count = 1
27+
)
28+
29+
expect_equal(w$count, 1)
30+
31+
# Check that getters work
32+
expect_equal(w$.get_value("count"), 1)
33+
expect_equal(w$.get_esm(), esm)
34+
expect_equal(w$.get_values(), list(
35+
count = 1
36+
))
37+
expect_equal(w$.get_width(), "100%")
38+
expect_equal(w$.get_height(), '400px')
39+
expect_equal(w$.get_mode(), "static")
40+
expect_equal(w$.get_host(), "0.0.0.0")
41+
expect_true(is.numeric(w$.get_port()))
42+
43+
# Check that setters work
44+
w$.set_value("count", 3, emit_change = FALSE)
45+
expect_equal(w$.get_value("count"), 3)
46+
47+
# Check that onChange handler works.
48+
# Create an empty list to track calls to the handler.
49+
change_list <<- list()
50+
handle_change <- function(key, new_val) {
51+
# Append to the list of { key, val }
52+
# pairs of tracked changes
53+
change_list <<- append(change_list,
54+
list(list(key = key, val = new_val))
55+
)
56+
}
57+
w$.on_change(handle_change)
58+
59+
w$.set_value("count", 5, emit_change = FALSE)
60+
expect_equal(w$.get_value("count"), 5)
61+
expect_equal(length(change_list), 0)
62+
63+
w$.set_value("count", 6, emit_change = TRUE)
64+
expect_equal(w$.get_value("count"), 6)
65+
expect_equal(length(change_list), 1)
66+
expect_equal(change_list[[1]], list(key = "count", val = 6))
67+
68+
w$.set_value("count", 7, emit_change = TRUE)
69+
expect_equal(w$.get_value("count"), 7)
70+
expect_equal(length(change_list), 2)
71+
expect_equal(change_list[[1]], list(key = "count", val = 6))
72+
expect_equal(change_list[[2]], list(key = "count", val = 7))
73+
})
74+
75+
test_that("invalid mode parameter value results in error", {
76+
expect_error(AnyHtmlWidget$new(
77+
.esm = esm,
78+
.mode = "INVALID",
79+
.height='400px',
80+
count = 1
81+
), "Invalid widget mode.")
82+
})
83+
84+
test_that("render return value reflects mode", {
85+
static_w <- AnyHtmlWidget$new(
86+
.esm = esm,
87+
.mode = "static",
88+
.height='400px',
89+
count = 1
90+
)
91+
92+
render_val <- static_w$render(return_widget = TRUE)
93+
expect_equal(class(render_val), c("anyhtmlwidget", "htmlwidget"))
94+
95+
dynamic_w <- AnyHtmlWidget$new(
96+
.esm = esm,
97+
.mode = "dynamic",
98+
.height='400px',
99+
count = 1
100+
)
101+
render_val <- dynamic_w$render(return_widget = TRUE)
102+
expect_equal(class(render_val), c("anyhtmlwidget", "htmlwidget"))
103+
104+
shiny_w <- AnyHtmlWidget$new(
105+
.esm = esm,
106+
.mode = "shiny",
107+
.height='400px',
108+
count = 1
109+
)
110+
expect_error(shiny_w$render(return_widget = TRUE), "render is meant for use with static, gadget, and dynamic modes")
111+
112+
gadget_w <- AnyHtmlWidget$new(
113+
.esm = esm,
114+
.mode = "gadget",
115+
.height='400px',
116+
count = 1
117+
)
118+
render_val <- gadget_w$render(return_widget = TRUE)
119+
expect_equal(class(render_val), c("list"))
120+
expect_equal(names(render_val), c("ui", "server"))
121+
122+
})

0 commit comments

Comments
 (0)