Debian Codegraph @

tests/testthat/test-mock-session.R

raw
1 test_that("invalidateLater supported", {
2 session <- MockShinySession$new()
3 i <- 0
4 isolate({
5 observe({
6 invalidateLater(10, session)
7 i <<- i + 1
8 })
9 })
10 session$flushReact()
11 expect_equal(i, 1)
12 session$elapse(10)
13 expect_equal(i, 2)
14 })
15
16 test_that("reactiveTimer supported", {
17 session <- MockShinySession$new()
18 i <- 0
19 isolate({
20 rt <- reactiveTimer(10, session)
21 observe({
22 rt()
23 i <<- i + 1
24 })
25 })
26 session$flushReact()
27 expect_equal(i, 1)
28 session$elapse(10)
29 expect_equal(i, 2)
30 })
31
32 test_that("getOutput should auto-flush if needed", {
33 session <- MockShinySession$new()
34 session$defineOutput("n", function(...){ 123 })
35 # There's no flushing in between these lines, so getOutput may need to instigate a flush
36 # in order to get the requisite observer to run.
37 expect_equal(session$getOutput("n"), 123)
38 })
39
40 test_that("reactivePoll supported", {
41 session <- MockShinySession$new()
42 i <- 0
43 isolate({
44 rp <- reactivePoll(10, session, function(){ rnorm(1) }, function(){ i <<- i + 1 })
45 observe({
46 # rnorm(1) as the check function will cause it to (almost) always run the update.
47 rp()
48 })
49 })
50 session$flushReact()
51 expect_equal(i, 1)
52 session$elapse(10)
53 session$flushReact()
54 expect_equal(i, 2)
55 })
56
57 # `renderCachedPlot()` is now implemented with `bindCache()`, and won't work by
58 # calling `f(session, "name")`, because the key computation function is not
59 # called with session and name.
60 # test_that("renderCachedPlot supported", {
61 # session <- MockShinySession$new()
62 # isolate({
63 # # renderCachedPlot is sensitive to having the cache set for it before entering.
64 # origCache <- getShinyOption("cache")
65 # shinyOptions(cache = cachem::cache_mem())
66 # on.exit(shinyOptions(cache = origCache), add=TRUE)
67 #
68 # p <- renderCachedPlot({ plot(1,1) }, { Sys.time() })
69 # plt <- p(session, "name")
70 #
71 # # Should have a size defined
72 # expect_equal(plt$coordmap$dims$width, 692) #FIXME: why isn't this respecting the clientdata sizes?
73 # expect_equal(plt$coordmap$dims$height, 400)
74 # })
75 # })
76
77 test_that("renderDataTable supported", {
78 withr::local_options(list(shiny.legacy.datatable = TRUE))
79
80 session <- MockShinySession$new()
81 isolate({
82 rt <- renderDataTable({
83 head(iris)
84 })
85 res <- rt(session, "name")
86 expect_equal(res$colnames, colnames(iris))
87 })
88 })
89
90 test_that("renderImage supported", {
91 session <- MockShinySession$new()
92 isolate({
93 ri <- renderImage({
94 # A temp file to save the output. It will be deleted after renderImage
95 # sends it, because deleteFile=TRUE.
96 outfile <- tempfile(fileext='.png')
97
98 # Generate a png
99 png(outfile, width=400, height=400)
100 plot(1,1)
101 dev.off()
102
103 # Return a list
104 list(src = outfile,
105 alt = "Alt text here")
106 }, deleteFile = TRUE)
107 img <- ri(session, "name")
108 expect_match(img$src, "^data:image/png;base64,")
109 expect_equal(img$alt, "Alt text here")
110 })
111 })
112
113 test_that("renderPlot supported", {
114 session <- MockShinySession$new()
115 isolate({
116 p <- renderPlot({ plot(1,1) })
117 plt <- p(session, "name")
118
119 # Should have a size defined
120 expect_equal(plt$width, 600)
121 expect_equal(plt$height, 400)
122 })
123 })
124
125 test_that("renderPrint supported", {
126 session <- MockShinySession$new()
127 isolate({
128 p <- renderPrint({ print("hi") })
129 pt <- p(session, "name")
130
131 expect_equal(pt, "[1] \"hi\"")
132 })
133 })
134
135 test_that("renderTable supported", {
136 session <- MockShinySession$new()
137 isolate({
138 rt <- renderTable({
139 head(iris)
140 })
141 ren <- rt(session, "name")
142 expect_match(ren, "^<table")
143 })
144 })
145
146 test_that("renderText supported", {
147 session <- MockShinySession$new()
148 isolate({
149 rt <- renderText({
150 "text here"
151 })
152 ren <- rt(session, "name")
153 expect_equal(ren, "text here")
154 })
155 })
156
157 test_that("renderUI supported", {
158 session <- MockShinySession$new()
159 isolate({
160 ui <- renderUI({
161 tags$a(href="https://rstudio.com", "link")
162 })
163 ren <- ui(session, "name")
164 expect_equal(ren$deps, list())
165 expect_equal(as.character(ren$html), "<a href=\"https://rstudio.com\">link</a>")
166 })
167 })
168
169 test_that("session supports allowReconnect", {
170 session <- MockShinySession$new()
171 session$allowReconnect(TRUE)
172 expect_true(TRUE) # testthat insists that every test must have an expectation
173 })
174
175 test_that("session supports clientData", {
176 session <- MockShinySession$new()
177 expect_equal(session$clientData$pixelratio, 1)
178 expect_equal(session$clientData$url_protocol, "http:")
179 expect_equal(session$clientData$url_hostname, "mocksession")
180 expect_equal(session$clientData$url_port, 1234)
181 expect_equal(session$clientData$url_pathname, "/mockpath")
182 expect_equal(session$clientData$url_hash, "#mockhash")
183 expect_equal(session$clientData$url_hash_initial, "#mockhash")
184 expect_equal(session$clientData$url_search, "?mocksearch=1")
185
186 # Arbitrary names have width, height, and hidden
187 expect_equal(session$clientData$output_arbitrary_width, 600)
188 expect_equal(session$clientData$output_arbitrary_height, 400)
189 expect_equal(session$clientData$output_arbitrary_hidden, FALSE)
190 })
191
192 test_that("session supports ns", {
193 session <- MockShinySession$new()
194 expect_equal(session$ns("hi"), "mock-session-hi")
195 })
196
197 test_that("session supports reload", {
198 session <- MockShinySession$new()
199 session$reload()
200 expect_true(TRUE) # testthat insists that every test must have an expectation
201 })
202
203 test_that("session supports close", {
204 session <- MockShinySession$new()
205 session$close()
206 expect_true(TRUE) # testthat insists that every test must have an expectation
207 })
208
209 test_that("session supports request", {
210 session <- MockShinySession$new()
211 expect_warning(session$request, "doesn't currently simulate a realistic request")
212 expect_error(session$request <- "blah", "can't be assigned to")
213 })
214
215 test_that("session supports userData", {
216 session <- MockShinySession$new()
217 expect_length(ls(session$userData), 0)
218 session$userData$x <- 123
219 expect_length(ls(session$userData), 1)
220 expect_equal(session$userData$x, 123)
221 })
222
223 test_that("session supports resetBrush", {
224 session <- MockShinySession$new()
225 withr::with_options(list("shiny.mocksession.warn" = TRUE), {
226 expect_warning(session$resetBrush(1))
227 })
228 })
229
230 test_that("generated methods signal unused argument errors", {
231 expect_error(session$resetBrush(1,2,3))
232 expect_error(session$resetBrush(1,2))
233 })
234
235 test_that("session supports sendCustomMessage", {
236 session <- MockShinySession$new()
237 session$sendCustomMessage(type=1, message=2)
238 expect_true(TRUE) # testthat insists that every test must have an expectation
239 })
240
241 test_that("session supports sendBinaryMessage", {
242 session <- MockShinySession$new()
243 session$sendBinaryMessage(type=1, message=2)
244 expect_true(TRUE) # testthat insists that every test must have an expectation
245 })
246
247 test_that("session supports sendInputMessage", {
248 session <- MockShinySession$new()
249 session$sendInputMessage(inputId=1, message=2)
250 expect_true(TRUE) # testthat insists that every test must have an expectation
251 })
252
253 test_that("session supports setBookmarkExclude", {
254 session <- MockShinySession$new()
255 withr::with_options(list("shiny.mocksession.warn" = TRUE), {
256 expect_warning(session$setBookmarkExclude(names=1))
257 })
258 })
259
260 test_that("session supports getBookmarkExclude", {
261 session <- MockShinySession$new()
262 withr::with_options(list("shiny.mocksession.warn" = TRUE), {
263 expect_warning(session$getBookmarkExclude())
264 })
265 })
266
267 test_that("session supports onBookmark", {
268 session <- MockShinySession$new()
269 session$onBookmark(fun=1)
270 expect_true(TRUE) # testthat insists that every test must have an expectation
271 })
272
273 test_that("session supports onBookmarked", {
274 session <- MockShinySession$new()
275 session$onBookmarked(fun=1)
276 expect_true(TRUE) # testthat insists that every test must have an expectation
277 })
278
279 test_that("session supports doBookmark", {
280 session <- MockShinySession$new()
281 withr::with_options(list("shiny.mocksession.warn" = TRUE), {
282 expect_warning(session$doBookmark())
283 })
284 })
285
286 test_that("session supports onRestore", {
287 session <- MockShinySession$new()
288 session$onRestore(fun=1)
289 expect_true(TRUE) # testthat insists that every test must have an expectation
290 })
291
292 test_that("session supports onRestored", {
293 session <- MockShinySession$new()
294 session$onRestored(fun=1)
295 expect_true(TRUE) # testthat insists that every test must have an expectation
296 })
297
298 test_that("session supports exportTestValues", {
299 session <- MockShinySession$new()
300 session$exportTestValues()
301 expect_true(TRUE) # testthat insists that every test must have an expectation
302 })
303
304 test_that("session supports getTestSnapshotUrl", {
305 session <- MockShinySession$new()
306 session$getTestSnapshotUrl(input=1, output=1, export=1, format=1)
307 expect_true(TRUE) # testthat insists that every test must have an expectation
308 })