Debian Codegraph @

tests/testthat/test-bind-event.R

raw
1
2 # Note that there are some tests for bindEvent() and caching in
3 # test-with-cache.R.
4
5 test_that("bindEvent and observers", {
6 trigger <- reactiveVal(1)
7 val <- reactiveVal(10)
8
9 vals <- numeric()
10 o <- bindEvent(
11 trigger(),
12 x = observe({
13 vals <<- c(vals, val())
14 })
15 )
16
17 flushReact()
18 expect_identical(vals, 10)
19
20 # Changing val has no effect
21 val(20)
22 flushReact()
23 expect_identical(vals, 10)
24
25 # Changing trigger causes the observer to execute
26 trigger(2)
27 flushReact()
28 expect_identical(vals, c(10, 20))
29
30 trigger(3)
31 flushReact()
32 expect_identical(vals, c(10, 20, 20))
33 })
34
35
36 test_that("bindEvent alters observers in place", {
37 v <- reactiveVal(1)
38 o <- observe({ v() })
39 o1 <- bindEvent(o, v())
40
41 # o and o1 are the same object
42 expect_identical(o, o1)
43
44 # Can't call bindEvent twice on an observer
45 expect_error(bindEvent(o, v()))
46 })
47
48
49 test_that("ignoreNULL works", {
50 n <- 0
51 observe({ n <<- n+1 }) %>% bindEvent(NULL, ignoreNULL = FALSE)
52 flushReact()
53 expect_identical(n, 1)
54
55 n <- 0
56 observe({ n <<- n+1 }) %>% bindEvent(NULL, ignoreNULL = TRUE)
57 flushReact()
58 expect_identical(n, 0)
59
60 # Two NULLs in the `...` get aggregated into a list, so the result is not
61 # NULL.
62 n <- 0
63 observe({ n <<- n+1 }) %>% bindEvent(NULL, NULL, ignoreNULL = TRUE)
64 flushReact()
65 expect_identical(n, 1)
66 })
67
68
69 test_that("once=TRUE works", {
70 n <- 0
71 v <- reactiveVal(1)
72 observe({ n <<- n + 1 }) %>% bindEvent(v(), once = FALSE)
73 flushReact()
74 expect_identical(n, 1)
75 v(2)
76 flushReact()
77 expect_identical(n, 2)
78
79 n <- 0
80 v <- reactiveVal(1)
81 observe({ n <<- n + v() }) %>% bindEvent(v(), once = TRUE)
82 flushReact()
83 expect_identical(n, 1)
84 v(2)
85 flushReact()
86 expect_identical(n, 1)
87 })