diff --git a/tests/testthat/test-apex-utils.R b/tests/testthat/test-apex-utils.R index d6ea6cc..54a211a 100644 --- a/tests/testthat/test-apex-utils.R +++ b/tests/testthat/test-apex-utils.R @@ -31,4 +31,17 @@ test_that("all apex utilities works", { }) +test_that("ax_nodata works", { + empty <- data.frame( + var1 = character(0), + var2 = numeric(0) + ) + ax <- apex(empty, aes(var1, var2), "column") %>% + ax_nodata( + text = "Sorry no data to visualize", + fontSize = "30px" + ) + expect_is(ax, "apexcharter") + expect_is(ax$x$ax_opts$noData, "list") +}) diff --git a/tests/testthat/test-colors.R b/tests/testthat/test-colors.R index 49a36ea..4ebc644 100644 --- a/tests/testthat/test-colors.R +++ b/tests/testthat/test-colors.R @@ -22,4 +22,15 @@ test_that("ax_colors_manual works", { expect_is(ax1$x$ax_opts$colors, "list") expect_length(ax1$x$ax_opts$colors, 3) expect_identical(ax1$x$ax_opts$colors, ax2$x$ax_opts$colors) + + + ax <- apex( + data = mtcars, + type = "scatter", + mapping = aes(x = wt, y = mpg, fill = cyl) + ) + expect_error(ax_colors_manual(ax, c("red", "blue", "green"))) + expect_error(ax_colors_manual(ax, c(a = "red", b = "blue", "green"))) + expect_error(ax_colors_manual(apexchart(), list(a = "red", b = "blue", b = "green"))) + expect_error(ax_colors_manual(list(), list(a = "red", b = "blue", b = "green"))) }) diff --git a/tests/testthat/test-facets.R b/tests/testthat/test-facets.R index adea28d..ef34cac 100644 --- a/tests/testthat/test-facets.R +++ b/tests/testthat/test-facets.R @@ -127,7 +127,7 @@ test_that("ax_facet_grid works with row and col", { -test_that("globla title and subtitle works", { +test_that("global title and subtitle works", { ax <- apex(mtcars, aes(disp, wt), type = "scatter") %>% ax_facet_grid(vars(cyl), vars(carb)) @@ -139,8 +139,8 @@ test_that("globla title and subtitle works", { ax <- ax %>% ax_labs( - title = "Facet wrap example", - subtitle = "mpg data from ggplot2" + title = "this is a title", + subtitle = "this is a subtitle" ) facet <- build_facets(ax) @@ -155,6 +155,61 @@ test_that("globla title and subtitle works", { }) +test_that("axis title works (grid)", { + + ax <- apex(mtcars, aes(disp, wt), type = "scatter") %>% + ax_facet_grid(vars(cyl), vars(carb)) + + facet <- build_facets(ax) + expect_is(facet, "list") + expect_null(facet$xaxis_title) + expect_null(facet$yaxis_title) + + ax <- ax %>% + ax_labs( + x = "x axis title", + y = "y axis title" + ) + + facet <- build_facets(ax) + expect_is(facet, "list") + expect_is(facet$xaxis_title, "list") + expect_is(facet$yaxis_title, "list") + + TAG <- build_facet_tag(ax) + TAG <- htmltools::doRenderTags(TAG) + expect_true(grepl(pattern = "apexcharter-facet-yaxis-title", x = TAG)) + expect_true(grepl(pattern = "apexcharter-facet-xaxis-title", x = TAG)) +}) + + +test_that("axis title works (wrap)", { + + ax <- apex(mtcars, aes(disp, wt), type = "scatter") %>% + ax_facet_wrap(vars(cyl, carb)) + + facet <- build_facets(ax) + expect_is(facet, "list") + expect_null(facet$xaxis_title) + expect_null(facet$yaxis_title) + + ax <- ax %>% + ax_labs( + x = "x axis title", + y = "y axis title" + ) + + facet <- build_facets(ax) + expect_is(facet, "list") + expect_is(facet$xaxis_title, "list") + expect_is(facet$yaxis_title, "list") + + TAG <- build_facet_tag(ax) + TAG <- htmltools::doRenderTags(TAG) + expect_true(grepl(pattern = "apexcharter-facet-yaxis-title", x = TAG)) + expect_true(grepl(pattern = "apexcharter-facet-xaxis-title", x = TAG)) +}) + test_that("complete_mapdata works", { diff --git a/tests/testthat/test-format.R b/tests/testthat/test-format.R new file mode 100644 index 0000000..2277690 --- /dev/null +++ b/tests/testthat/test-format.R @@ -0,0 +1,5 @@ +test_that("multiplication works", { + fmt <- format_num("~s") + expect_is(fmt, "JS_EVAL") + expect_error(format_num(".0f", locale = "DONT_EXIST")) +}) diff --git a/tests/testthat/test-mixed-charts.R b/tests/testthat/test-mixed-charts.R index e93b487..239ad5f 100644 --- a/tests/testthat/test-mixed-charts.R +++ b/tests/testthat/test-mixed-charts.R @@ -1,3 +1,13 @@ +test_that("add_line only works with apex and certain type", { + + expect_error(apexchart() %>% add_line(aes(month, temperature))) + expect_error(apexchart() %>% add_smooth_line()) + + expect_error(apex(mtcars, aes(x = cyl), type = "pie") %>% add_line(aes(month, temperature))) + expect_error(apex(mtcars, aes(x = cyl), type = "pie") %>% add_smooth_line()) +}) + + test_that("add_line works with column chart", { ax <- apex(climate_paris, aes(month, precipitation), type = "column") %>% @@ -28,5 +38,12 @@ test_that("add_smooth_line works with scatter chart", { expect_is(ax, "apex") expect_is(ax$x$ax_opts$series, "list") expect_length(ax$x$ax_opts$series, 2) + + ax <- apex(cars, aes(speed, dist), type = "scatter") %>% + add_smooth_line(model = "lm") + + 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-proxy.R b/tests/testthat/test-proxy.R index 67d9449..d01725b 100644 --- a/tests/testthat/test-proxy.R +++ b/tests/testthat/test-proxy.R @@ -7,6 +7,35 @@ test_that("apexchartProxy works", { }) +test_that(".ax_proxy & .ax_proxy2 works", { + + session <- as.environment(list( + ns = identity, + sendCustomMessage = function(type, message) { + session$lastCustomMessage = list(type = type, message = message) + } + )) + + proxy <- apexchartProxy("chart", session = session) %>% + .ax_proxy(name = "TEST", a = 1) + + expect_is(session$lastCustomMessage, "list") + expect_identical(session$lastCustomMessage$type, "update-apexchart-TEST") + expect_identical(session$lastCustomMessage$message$id, "chart") + + expect_error(.ax_proxy(apexchart(), name = "TEST", a = 1)) + + proxy <- apexchartProxy("chart", session = session) %>% + .ax_proxy2(name = "TEST", list(a = 1)) + + expect_is(session$lastCustomMessage, "list") + expect_identical(session$lastCustomMessage$type, "update-apexchart-TEST") + expect_identical(session$lastCustomMessage$message$id, "chart") + + expect_error(.ax_proxy2(apexchart(), name = "TEST", a = 1)) +}) + + test_that("ax_proxy_series works", { session <- as.environment(list( diff --git a/tests/testthat/test-tooltip.R b/tests/testthat/test-tooltip.R new file mode 100644 index 0000000..ca0a8c9 --- /dev/null +++ b/tests/testthat/test-tooltip.R @@ -0,0 +1,12 @@ +test_that("set_tooltip_fixed works", { + ax <- apex( + data = mtcars, + mapping = aes(x = cyl), + type = "column" + ) %>% + set_tooltip_fixed() + + expect_is(ax, "apex") + expect_is(ax$x$ax_opts$tooltip$fixed, "list") + expect_true(ax$x$ax_opts$tooltip$fixed$enabled) +})