Skip to content

Commit 088cb6e

Browse files
committed
chore: Refactors functions to build better unit tests
1 parent 7691396 commit 088cb6e

4 files changed

Lines changed: 89 additions & 16 deletions

File tree

R/inputs.R

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ date_extra <- function(id, key = NULL, ...) {
120120
)
121121
}
122122

123+
build_dropdown_extra_choices <- function(choices) {
124+
if (length(choices) == 0) {
125+
choices_js <- ""
126+
} else {
127+
choices_js <- paste0(", choices: ", rjson::toJSON(choices))
128+
}
129+
130+
return(choices_js)
131+
}
132+
123133
#' Select input for reactable column cell
124134
#'
125135
#' @param id id of the select input
@@ -137,11 +147,8 @@ date_extra <- function(id, key = NULL, ...) {
137147
#'
138148
#' @export
139149
dropdown_extra <- function(id, choices, key = NULL, ...) {
140-
if (length(choices) == 0) {
141-
choices_js <- ""
142-
} else {
143-
choices_js <- paste0(", choices: ", rjson::toJSON(choices))
144-
}
150+
151+
choices_js <- build_dropdown_extra_choices(choices)
145152

146153
key <- define_key(key)
147154

R/reactable-server.R

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ hide_internal_uuid <- function(args) {
242242
return(args)
243243
}
244244

245+
sort_table <- function(data, column_name, direction) {
246+
column_name <- rlang::sym(column_name)
247+
if (direction == "asc") {
248+
data |>
249+
dplyr::arrange(!!column_name)
250+
} else if (direction == "desc") {
251+
data |>
252+
dplyr::arrange(dplyr::desc(!!column_name))
253+
}
254+
}
255+
245256
#' @rdname reactable-extras-server
246257
#' @export
247258
reactable_extras_server <- function(id, data, total_pages = 4, sortable = TRUE, ...) {
@@ -294,17 +305,10 @@ reactable_extras_server <- function(id, data, total_pages = 4, sortable = TRUE,
294305
reactable_data()
295306
} else {
296307
column_name <- rlang::sym(names(column_sort()))
297-
data <- if (column_sort()[[1]] == "asc") {
298-
data |>
299-
dplyr::arrange(!!column_name) |>
300-
get_data_on_page(page_number = page_number(), total_pages = total_pages) |>
301-
reactable_data()
302-
} else if (column_sort()[[1]] == "desc") {
303-
data |>
304-
dplyr::arrange(dplyr::desc(!!column_name)) |>
305-
get_data_on_page(page_number = page_number(), total_pages = total_pages) |>
306-
reactable_data()
307-
}
308+
data |>
309+
get_data_on_page(page_number = page_number(), total_pages = total_pages) |>
310+
sort_table(column_name = column_name, direction = column_sort()[[1]]) |>
311+
reactable_data()
308312
}
309313

310314
reactable::updateReactable("reactable", data = reactable_data())

tests/testthat/test-inputs.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,50 @@ test_that("`args_js` returns correct output", {
55

66
expect_equal(args, ", a: 'one', b: 'two'")
77
})
8+
9+
test_that("`args_js` correctly remaps class to className", {
10+
args <- args_js(class = "my-class")
11+
12+
expect_equal(args, ", class: 'my-class', className: 'my-class'")
13+
})
14+
15+
test_that("`define_key` correctly defines a key", {
16+
key <- define_key("mykey")
17+
18+
expect_equal(key, "cellInfo.row.mykey")
19+
})
20+
21+
test_that("`define_key` correctly defines a key when key is null", {
22+
key <- define_key(NULL)
23+
24+
expect_equal(key, paste(
25+
"cellInfo.row['.internal_uuid'] ?",
26+
"cellInfo.row['.internal_uuid'] :",
27+
"(Number(cellInfo.id) + 1)"
28+
))
29+
})
30+
31+
test_that("`dropdown_extra` sets choices to blank when length is 0", {
32+
choices <- build_dropdown_extra_choices(character(0))
33+
34+
expect_equal(choices, "")
35+
})
36+
37+
test_that("`dropdown_extra` sets choices correctly", {
38+
choices <- build_dropdown_extra_choices(letters[1:3])
39+
40+
expect_equal(choices, ", choices: [\"a\",\"b\",\"c\"]")
41+
})
42+
43+
test_that("expect tooltip to return JS_EVAL object", {
44+
expect_s3_class(tooltip_extra("This is my tool-tip", theme = "material"), "JS_EVAL")
45+
})
46+
47+
test_that("expect inputs to return JS_EVAL object", {
48+
expect_s3_class(button_extra("button"), "JS_EVAL")
49+
expect_s3_class(checkbox_extra("checkbox"), "JS_EVAL")
50+
expect_s3_class(date_extra("date"), "JS_EVAL")
51+
expect_s3_class(dropdown_extra("dropdown", letters[1:3]), "JS_EVAL")
52+
expect_s3_class(text_extra("text"), "JS_EVAL")
53+
expect_s3_class(button_extra("button"), "JS_EVAL")
54+
})

tests/testthat/test-reactable-server.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,18 @@ test_that("reactable_extras_server should return the correct data subset", {
250250
}
251251
)
252252
})
253+
254+
test_that("tables are sorted correctly according to direction", {
255+
mtcars_with_id <- mutate(mtcars, row_id = row_number())
256+
257+
expect_equal(
258+
sort_table(mtcars_with_id, "row_id", "asc")$row_id,
259+
1:32
260+
)
261+
262+
expect_equal(
263+
sort_table(mtcars_with_id, "row_id", "desc")$row_id,
264+
32:1
265+
)
266+
267+
})

0 commit comments

Comments
 (0)