|
| 1 | +context("components") |
| 2 | + |
| 3 | +test_that("Components work recursively (components can be children of components)", { |
| 4 | + |
| 5 | + # div inside a div |
| 6 | + x <- dashHtmlComponents::htmlDiv(id = "one", htmlDiv(id = "two")) |
| 7 | + expect_true(dash:::is.component(x)) |
| 8 | + expect_true(dash:::is.component(x[[1]]$children)) |
| 9 | + |
| 10 | + # slider inside a div |
| 11 | + x <- htmlDiv( |
| 12 | + dashCoreComponents::dccSlider( |
| 13 | + id = "h", |
| 14 | + min = 1, |
| 15 | + max = 100, |
| 16 | + value = 48 |
| 17 | + ) |
| 18 | + ) |
| 19 | + |
| 20 | + expect_true(dash:::is.component(x)) |
| 21 | + expect_true(dash:::is.component(x[[1]]$children)) |
| 22 | + slider <- x$props |
| 23 | + expect_true(slider$children$props[["id"]] == "h") |
| 24 | + expect_true(slider$children$props[["min"]] == 1) |
| 25 | + expect_true(slider$children$props[["max"]] == 100) |
| 26 | + expect_true(slider$children$props[["value"]] == 48) |
| 27 | +}) |
| 28 | + |
| 29 | +test_that("Component constructors behave as intended", { |
| 30 | + |
| 31 | + # components have three main keys |
| 32 | + # (1) props: or the main properties, which are recursive (component) |
| 33 | + # (2) type: or the 'name' of the component |
| 34 | + # (3) namespace: is this a core/html component? |
| 35 | + |
| 36 | + expect_component_names <- function(component) { |
| 37 | + diff <- dash:::setdiffsym(names(component), c("props", "type", "namespace", "propNames", "package")) |
| 38 | + expect_length(diff, 0) |
| 39 | + } |
| 40 | + |
| 41 | + expect_component_names(dashHtmlComponents::htmlA()) |
| 42 | + expect_component_names(dashCoreComponents::dccDropdown()) |
| 43 | + |
| 44 | + expect_equal( |
| 45 | + htmlH2("A header")$props$children[[1]], "A header" |
| 46 | + ) |
| 47 | + |
| 48 | + # test akin to this one https://github.com/plotly/dash-renderer/blob/851d717b/tests/test_render.py#L25-L38 |
| 49 | + vals <- list("Basic string", 3.14, NULL, htmlDiv("Just a test")) |
| 50 | + prop_vals <- htmlH2(vals)$props |
| 51 | + expect_identical(prop_vals$children[[1]], vals[[1]]) |
| 52 | + |
| 53 | + # TODO: test the rendered DOM! |
| 54 | + |
| 55 | +}) |
| 56 | + |
| 57 | + |
| 58 | +test_that("Giving nonsense arguments to components yields error", { |
| 59 | + expect_error( |
| 60 | + htmlA(nonsense = "string", gibberish = "string"), |
| 61 | + "The following props are not valid in this component: 'nonsense, gibberish'", |
| 62 | + fixed = TRUE |
| 63 | + ) |
| 64 | +}) |
| 65 | + |
| 66 | +# test_that("Can identify whether a component contains a component of a given type", { |
| 67 | +# g <- dashCoreComponents::dccGraph() |
| 68 | +# s <- dashCoreComponents::dccSlider() |
| 69 | +# expect_true(dash:::component_contains_type(g, "dashCoreComponents", "Graph")) |
| 70 | +# expect_false(dash:::component_contains_type(g, "dash", "Graph")) |
| 71 | +# expect_false(dash:::component_contains_type(s, "dashCoreComponents", "Graph")) |
| 72 | +# expect_true(dash:::component_contains_type(htmlDiv(children=list(s, htmlDiv(g))), "dashCoreComponents", "Graph")) |
| 73 | +# }) |
| 74 | + |
| 75 | +test_that("wildcard attributes work with children", { |
| 76 | + s1 <- htmlSpan("hmm", className = "value-output", `data-icon` = "fa-pencil") |
| 77 | + s2 <- htmlSpan(children = list("hmm"), className = "value-output", `data-icon` = "fa-pencil") |
| 78 | + |
| 79 | + expect_equal(s1$props$children, "hmm") |
| 80 | + expect_equal(s1$props$`data-icon`, "fa-pencil") |
| 81 | + expect_equal(s2$props$children, list("hmm")) |
| 82 | + expect_equal(s2$props$`data-icon`, "fa-pencil") |
| 83 | +}) |
| 84 | + |
| 85 | +# test_that("Can translate arbitrary HTML string", { |
| 86 | +# skip_if_not_installed("dashDangerouslySetInnerHtml") |
| 87 | +# |
| 88 | +# html <- "<div> 1 </div>" |
| 89 | +# expect_is( |
| 90 | +# dashDangerouslySetInnerHtml::DangerouslySetInnerHTML(HTML(html)), |
| 91 | +# "dash_component" |
| 92 | +# ) |
| 93 | +# }) |
0 commit comments