From 43475d3392223485482b05107aaff0b0bd53ae52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?fran=C3=A7ois=20GUILLEM?= Date: Fri, 7 Jul 2017 13:37:30 +0200 Subject: [PATCH] More tests for add/updateMinicharts --- R/{popupArgs.R => popup_args.R} | 0 tests/testthat/test-minichart.R | 32 -------- tests/testthat/test-minicharts.R | 132 +++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 32 deletions(-) rename R/{popupArgs.R => popup_args.R} (100%) delete mode 100644 tests/testthat/test-minichart.R create mode 100644 tests/testthat/test-minicharts.R diff --git a/R/popupArgs.R b/R/popup_args.R similarity index 100% rename from R/popupArgs.R rename to R/popup_args.R diff --git a/tests/testthat/test-minichart.R b/tests/testthat/test-minichart.R deleted file mode 100644 index b28d6a6..0000000 --- a/tests/testthat/test-minichart.R +++ /dev/null @@ -1,32 +0,0 @@ -context("minichart") - -# Helper function -expect_invoke_js_method <- function(method, postProcess = I) { - map <- leaflet::leaflet() %>% - addMinicharts(0, 0, 1:3, layerId = "a") %>% - postProcess() - - expect_true(all(method %in% map$jsmethods)) -} - -with_mock( - `leaflet::invokeMethod` = function(map, data, method, ...) { - map$jsargs = list(...) - map$jsmethods <- c(map$jsmethods, method) - map - }, - { - - test_that("One can add, update, clear and remove minicharts", { - expect_invoke_js_method("addMinicharts") - expect_invoke_js_method("updateMinicharts", function(map) { - updateMinicharts(map, layerId = "a", chartdata = 1:4) - }) - expect_invoke_js_method("clearMinicharts", clearMinicharts) - expect_invoke_js_method("removeMinicharts", function(map) { - removeMinicharts(map, "a") - }) - }) - - } -) diff --git a/tests/testthat/test-minicharts.R b/tests/testthat/test-minicharts.R new file mode 100644 index 0000000..bc28d42 --- /dev/null +++ b/tests/testthat/test-minicharts.R @@ -0,0 +1,132 @@ +context("minichart") + +# Helper function +expect_invoke_js_method <- function(method, postProcess = I) { + map <- leaflet::leaflet() %>% + addMinicharts(0, 0, 1:3, layerId = "a") %>% + postProcess() + + expect_true(all(method %in% map$jsmethods)) +} + +with_mock( + `leaflet::invokeMethod` = function(map, data, method, ...) { + map$jsargs <- list(...) + map$jsmethods <- c(map$jsmethods, method) + map + }, + `leaflet::addLegend` = function(map, ...) { + map$legendArgs <- list(...) + map + }, + `leaflet::removeControl` = function(map, id) { + map$controlRemoved <- id + map + }, + { + + test_that("One can add, update, clear and remove minicharts", { + expect_invoke_js_method("addMinicharts") + expect_invoke_js_method("updateMinicharts", function(map) { + updateMinicharts(map, layerId = "a", chartdata = 1:4) + }) + expect_invoke_js_method("clearMinicharts", clearMinicharts) + expect_invoke_js_method("removeMinicharts", function(map) { + removeMinicharts(map, "a") + }) + }) + + describe("addMinicharts", { + it("correctly manages the 'labels' argument", { + # No labels + map <- leaflet::leaflet() %>% addMinicharts(0, 0, 1:3, layerId = "a") + expect_equal(map$jsargs[[1]][[1]]$static$labels, "none") + # Auto labels + map <- leaflet::leaflet() %>% + addMinicharts(0, 0, 1:3, layerId = "a", showLabels = TRUE) + expect_equal(map$jsargs[[1]][[1]]$static$labels, "auto") + # Custom labels + map <- leaflet::leaflet() %>% + addMinicharts(0, 0, 1, layerId = "a", showLabels = TRUE, + labelText = "test") + expect_equal(map$jsargs[[1]][[1]]$static$labels, "test") + }) + + it("adds a legend when chartdata has multiple named columns", { + map <- leaflet::leaflet() %>% + addMinicharts(0, 0, data.frame(a = 1, b = 2), layerId = "a") + expect_false(is.null(map$legendArgs)) + expect_equal(map$legendArgs$labels, I(c("a", "b"))) + # No names, no legend + map <- leaflet::leaflet() %>% addMinicharts(0, 0, 1:3, layerId = "a") + expect_null(map$legendArgs) + # One column, no legend + map <- leaflet::leaflet() %>% + addMinicharts(0, 0, data.frame(a = 1), layerId = "a") + expect_null(map$legendArgs) + # Argument legend is FALSE + map <- leaflet::leaflet() %>% + addMinicharts(0, 0, data.frame(a = 1, b = 2), layerId = "a", + legend = FALSE) + expect_null(map$legendArgs) + }) + }) + + describe("updateMinicharts", { + basemap <- leaflet::leaflet() %>% addMinicharts(0, 0, 1) + + it("correctly manages the 'labels' argument", { + # No labels + map <- basemap %>% updateMinicharts("a", showLabels = FALSE) + expect_equal(map$jsargs[[1]][[1]]$static$labels, "none") + # Auto labels + map <- basemap %>% updateMinicharts("a", showLabels = TRUE) + expect_equal(map$jsargs[[1]][[1]]$static$labels, "auto") + # Custom labels + map <- basemap %>% + updateMinicharts("a", showLabels = TRUE, labelText = "test") + expect_equal(map$jsargs[[1]][[1]]$static$labels, "test") + }) + + it ("does not update labels by default", { + map <- basemap %>% updateMinicharts("a") + expect_null(map$jsargs[[1]][[1]]$static$labels) + }) + + it ("updates time slider only when data is updated", { + map <- basemap %>% updateMinicharts("a") + expect_null(map$jsargs[[5]]) + + map <- basemap %>% updateMinicharts("a", chartdata = 2) + expect_false(is.null(map$jsargs[[5]])) + }) + + it ("updates legend only when data is updated", { + baseMap <- leaflet::leaflet() %>% addMinicharts(0, 0, 1, layerId = "a") + + map <- baseMap %>% updateMinicharts("a", data.frame(a = 1, b = 2)) + expect_false(is.null(map$legendArgs)) + expect_equal(map$legendArgs$labels, I(c("a", "b"))) + # No names, no legend + map <- baseMap %>% updateMinicharts("a", 1:3) + expect_null(map$legendArgs) + expect_equal(map$controlRemoved, "minichartsLegend") + # One column, no legend + map <- baseMap %>% updateMinicharts("a", data.frame(a = 1)) + expect_null(map$legendArgs) + expect_equal(map$controlRemoved, "minichartsLegend") + # Argument legend is FALSE + map <- baseMap %>% updateMinicharts("a", data.frame(a = 1, b = 2), legend = FALSE) + expect_null(map$legendArgs) + expect_equal(map$controlRemoved, "minichartsLegend") + + # data is not updated + map <- baseMap %>% updateMinicharts("a") + expect_null(map$legendArgs) + expect_null(map$controlRemoved) + }) + + }) + + } +)