From 5ac0087461faac233d3e8b625178fa8f1dd0af09 Mon Sep 17 00:00:00 2001 From: pvictor Date: Wed, 6 Jan 2021 14:47:34 +0100 Subject: [PATCH] more tests --- R/apexcharter.R | 8 +-- R/facets.R | 12 ++-- R/grid.R | 8 +-- R/spark-box.R | 8 +-- tests/testthat/test-annotations.R | 91 ++++++++++++++++++++++++++++++ tests/testthat/test-facets.R | 30 +++++++++- tests/testthat/test-grid.R | 13 +++++ tests/testthat/test-mixed-charts.R | 32 +++++++++++ tests/testthat/test-spark_box.R | 17 ++++++ 9 files changed, 200 insertions(+), 19 deletions(-) create mode 100644 tests/testthat/test-annotations.R create mode 100644 tests/testthat/test-grid.R create mode 100644 tests/testthat/test-mixed-charts.R create mode 100644 tests/testthat/test-spark_box.R diff --git a/R/apexcharter.R b/R/apexcharter.R index 2479326..a0ebfb5 100644 --- a/R/apexcharter.R +++ b/R/apexcharter.R @@ -136,13 +136,13 @@ config_update <- function(series_animate = TRUE, #' @importFrom htmlwidgets shinyWidgetOutput shinyRenderWidget #' #' @example examples/apexcharter-shiny.R -apexchartOutput <- function(outputId, width = "100%", height = "400px"){ +apexchartOutput <- function(outputId, width = "100%", height = "400px") { # nocov start htmlwidgets::shinyWidgetOutput(outputId, "apexcharter", width, height, package = "apexcharter") -} +} # nocov end #' @rdname apexcharter-shiny #' @export -renderApexchart <- function(expr, env = parent.frame(), quoted = FALSE) { +renderApexchart <- function(expr, env = parent.frame(), quoted = FALSE) { # nocov start if (!quoted) { expr <- substitute(expr) } # force quoted htmlwidgets::shinyRenderWidget(expr, apexchartOutput, env, quoted = TRUE) -} +} # nocov end diff --git a/R/facets.R b/R/facets.R index 1502554..f3b5f3f 100644 --- a/R/facets.R +++ b/R/facets.R @@ -346,7 +346,7 @@ apexfacetOutput <- function(outputId) { #' #' @importFrom shiny exprToFunction createRenderFunction createWebDependency #' @importFrom htmltools renderTags resolveDependencies -renderApexfacet <- function(expr, env = parent.frame(), quoted = FALSE) { +renderApexfacet <- function(expr, env = parent.frame(), quoted = FALSE) { # nocov start func <- exprToFunction(expr, env, quoted) createRenderFunction( func = func, @@ -371,7 +371,7 @@ renderApexfacet <- function(expr, env = parent.frame(), quoted = FALSE) { ) }, apexfacetOutput, list() ) -} +} # nocov end @@ -380,15 +380,15 @@ renderApexfacet <- function(expr, env = parent.frame(), quoted = FALSE) { # Print methods ----------------------------------------------------------- #' @export -print.apex_facet <- function(x, ...) { +print.apex_facet <- function(x, ...) { # nocov start TAG <- build_facet_tag(x) print(htmltools::browsable(TAG)) -} +} # nocov end -knit_print.apex_facet <- function(x, ..., options = NULL) { +knit_print.apex_facet <- function(x, ..., options = NULL) { # nocov start TAG <- build_facet_tag(x) knitr::knit_print(htmltools::browsable(TAG), options = options, ...) -} +} # nocov end diff --git a/R/grid.R b/R/grid.R index c34afe6..9e52e5d 100644 --- a/R/grid.R +++ b/R/grid.R @@ -160,7 +160,7 @@ apexgridOutput <- function(outputId) { #' #' @importFrom shiny exprToFunction createRenderFunction createWebDependency #' @importFrom htmltools renderTags resolveDependencies -renderApexgrid <- function(expr, env = parent.frame(), quoted = FALSE) { +renderApexgrid <- function(expr, env = parent.frame(), quoted = FALSE) { # nocov start func <- exprToFunction(expr, env, quoted) createRenderFunction( func = func, @@ -193,14 +193,14 @@ renderApexgrid <- function(expr, env = parent.frame(), quoted = FALSE) { ) }, apexgridOutput, list() ) -} +} # nocov end # Print methods ----------------------------------------------------------- - +# nocov start #' @export print.apex_grid <- function(x, ...) { TAG <- build_grid( @@ -227,7 +227,7 @@ knit_print.apex_grid <- function(x, ..., options = NULL) { ) knitr::knit_print(htmltools::browsable(TAG), options = options, ...) } - +# nocov end diff --git a/R/spark-box.R b/R/spark-box.R index 0dd7507..27bbf61 100644 --- a/R/spark-box.R +++ b/R/spark-box.R @@ -103,14 +103,14 @@ spark_box <- function(data, #' @rdname apexcharter-shiny #' @export -sparkBoxOutput <- function(outputId, width = "100%", height = "160px"){ +sparkBoxOutput <- function(outputId, width = "100%", height = "160px") { # nocov start htmlwidgets::shinyWidgetOutput(outputId, "apexcharter", width, height, package = "apexcharter") -} +} # nocov end #' @rdname apexcharter-shiny #' @export -renderSparkBox <- function(expr, env = parent.frame(), quoted = FALSE) { +renderSparkBox <- function(expr, env = parent.frame(), quoted = FALSE) { # nocov start if (!quoted) { expr <- substitute(expr) } # force quoted htmlwidgets::shinyRenderWidget(expr, apexchartOutput, env, quoted = TRUE) -} +} # nocov end diff --git a/tests/testthat/test-annotations.R b/tests/testthat/test-annotations.R new file mode 100644 index 0000000..6ed4a23 --- /dev/null +++ b/tests/testthat/test-annotations.R @@ -0,0 +1,91 @@ +test_that("add_shade works", { + + ax <- apex(consumption, aes(date, value, group = type), "spline") %>% + add_shade(from = "2020-01-06", to = "2020-01-20") + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$annotations, "list") + expect_is(ax$x$ax_opts$annotations$xaxis, "list") +}) + + +test_that("add_shade_weekend works", { + + ax <- apex(consumption, aes(date, value, group = type), "spline") %>% + add_shade_weekend() + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$annotations, "list") + expect_is(ax$x$ax_opts$annotations$xaxis, "list") +}) + + +test_that("add_event works", { + + ax <- apex(consumption, aes(date, value, group = type), "spline") %>% + add_event(when = c("2020-01-11", "2020-01-29")) + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$annotations, "list") + expect_is(ax$x$ax_opts$annotations$xaxis, "list") +}) + + +test_that("add_event_marker works", { + + ax <- apex(consumption, aes(date, value, group = type), "spline") %>% + add_event_marker(when = "2020-01-22", y = 1805) + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$annotations, "list") + expect_is(ax$x$ax_opts$annotations$points, "list") +}) + + +test_that("add_hline works", { + + ax <- apex(consumption, aes(date, value, group = type), "spline") %>% + add_hline(value = 2100) + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$annotations, "list") + expect_is(ax$x$ax_opts$annotations$yaxis, "list") +}) + + +test_that("add_vline works", { + + ax <- apex(consumption, aes(date, value, group = type), "spline") %>% + add_vline(value = 2100) + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$annotations, "list") + expect_is(ax$x$ax_opts$annotations$xaxis, "list") +}) + + +test_that("add_point works", { + + ax <- apex( + data = iris, + aes(Sepal.Length, Sepal.Width), + "scatter" + ) %>% + add_point( + x = mean(iris$Sepal.Length), + y = mean(iris$Sepal.Width) + ) + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$annotations, "list") + expect_is(ax$x$ax_opts$annotations$points, "list") +}) + + + +test_that("label & marker works", { + + expect_is(label(), "list") + expect_is(marker(), "list") +}) + diff --git a/tests/testthat/test-facets.R b/tests/testthat/test-facets.R index 76d31fe..92f501e 100644 --- a/tests/testthat/test-facets.R +++ b/tests/testthat/test-facets.R @@ -46,6 +46,11 @@ test_that("set_scale works", { expect_true(is.null(ax$x$ax_opts$xaxis$min)) expect_true(is.null(ax$x$ax_opts$xaxis$max)) + ax <- set_scale(apexchart(), 1:100, scales = "free_x", axis = "x") + expect_true(is.null(ax$x$ax_opts$xaxis)) + expect_true(is.null(ax$x$ax_opts$xaxis$min)) + expect_true(is.null(ax$x$ax_opts$xaxis$max)) + ax <- set_scale(apexchart(), 1:100, scales = "fixed", axis = "y") expect_true(!is.null(ax$x$ax_opts$yaxis)) expect_true(!is.null(ax$x$ax_opts$yaxis$min)) @@ -56,6 +61,10 @@ test_that("set_scale works", { expect_true(is.null(ax$x$ax_opts$yaxis$min)) expect_true(is.null(ax$x$ax_opts$yaxis$max)) + ax <- set_scale(apexchart(), 1:100, scales = "free_y", axis = "y") + expect_true(is.null(ax$x$ax_opts$yaxis)) + expect_true(is.null(ax$x$ax_opts$yaxis$min)) + expect_true(is.null(ax$x$ax_opts$yaxis$max)) }) @@ -79,7 +88,7 @@ test_that("ax_facet_wrap works", { }) -test_that("ax_facet_grid works", { +test_that("ax_facet_grid works with row", { ax <- apex(mtcars, aes(disp, wt), type = "scatter") %>% ax_facet_grid(vars(cyl)) @@ -98,3 +107,22 @@ test_that("ax_facet_grid works", { }) +test_that("ax_facet_grid works with row and col", { + + ax <- apex(mtcars, aes(disp, wt), type = "scatter") %>% + ax_facet_grid(vars(cyl), vars(carb)) + + expect_is(ax, "apex") + expect_is(ax, "apex_facet") + expect_true(!is.null(ax$x$facet)) + + facet <- build_facets(ax) + expect_is(facet, "list") + expect_identical(facet$type, "grid") + expect_length(facet$facets, length(unique(mtcars$cyl)) * length(unique(mtcars$carb))) + + TAG <- build_facet_tag(ax) + expect_is(TAG, "shiny.tag") +}) + + diff --git a/tests/testthat/test-grid.R b/tests/testthat/test-grid.R new file mode 100644 index 0000000..1ab486b --- /dev/null +++ b/tests/testthat/test-grid.R @@ -0,0 +1,13 @@ +test_that("apex_grid works", { + + ax <- apex_grid( + apexchart(), + apexchart(), + apexchart(), + apexchart(), + grid_area = c("1 / 1 / 3 / 2", "1 / 2 / 2 / 4", "2 / 2 / 3 / 4", "2 / 2 / 3 / 4"), + ncol = 2, nrow = 2, + height = "600px" + ) + expect_is(ax, "apex_grid") +}) diff --git a/tests/testthat/test-mixed-charts.R b/tests/testthat/test-mixed-charts.R new file mode 100644 index 0000000..e93b487 --- /dev/null +++ b/tests/testthat/test-mixed-charts.R @@ -0,0 +1,32 @@ +test_that("add_line works with column chart", { + + ax <- apex(climate_paris, aes(month, precipitation), type = "column") %>% + add_line(aes(month, temperature)) + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$series, "list") + expect_length(ax$x$ax_opts$series, 2) +}) + + +test_that("add_line works with scatter chart", { + + ax <- apex(cars, aes(speed, dist), type = "scatter") %>% + add_line(aes(x, y), data = lowess(cars), serie_name = "lowess") + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$series, "list") + expect_length(ax$x$ax_opts$series, 2) +}) + + +test_that("add_smooth_line works with scatter chart", { + + ax <- apex(cars, aes(speed, dist), type = "scatter") %>% + add_smooth_line(model = "loess", span = 1) + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$series, "list") + expect_length(ax$x$ax_opts$series, 2) +}) + diff --git a/tests/testthat/test-spark_box.R b/tests/testthat/test-spark_box.R new file mode 100644 index 0000000..d0668d5 --- /dev/null +++ b/tests/testthat/test-spark_box.R @@ -0,0 +1,17 @@ +test_that("spark_box works", { + spark_data <- data.frame( + date = Sys.Date() + 1:20, + var1 = round(rnorm(20, 50, 10)), + var2 = round(rnorm(20, 50, 10)), + var3 = round(rnorm(20, 50, 10)) + ) + + sb <- spark_box( + data = spark_data, + title = mean(spark_data$var1), + subtitle = "Variable 1" + ) + + expect_is(sb, "apexcharter") + expect_true(!identical(sb$x$sparkbox, FALSE)) +})