Skip to content

Commit

Permalink
feat: add tests for CommandBar
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Sobolewski committed Sep 12, 2023
1 parent e0d9baa commit ce0a740
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Imports:
shiny,
shiny.react (>= 0.3.0)
Suggests:
chromote,
covr,
dplyr,
DT,
Expand All @@ -46,6 +47,7 @@ Suggests:
shiny.i18n (>= 0.3.0),
shiny.router (>= 0.3.1),
shinyjs,
shinytest2,
sortable,
stringi,
testthat (>= 3.0.0),
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/setup-disable-crashpad.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ❯ checking for detritus in the temp directory ... NOTE
# Found the following files/directories:
# ‘Crashpad’
#
# 0 errors ✔ | 0 warnings ✔ | 1 note ✖
# Error: Error: R CMD check found NOTEs
# Flavors: ubuntu-22.04 (devel), ubuntu-22.04 (release), ubuntu-22.04 (oldrel)

# References (shinytest2 github):
# 1. https://github.com/rstudio/shinytest2/blob/main/cran-comments.md
# 2. https://github.com/rstudio/shinytest2/blob/main/tests/testthat/setup-disable-crashpad.R

# Disable crash reporting on CRAN machines. (Can't get the report anyways)
chromote::set_chrome_args(c(
# https://peter.sh/experiments/chromium-command-line-switches/#disable-crash-reporter
#> Disable crash reporter for headless. It is enabled by default in official builds
"--disable-crash-reporter",
chromote::default_chrome_args()
))

# Make sure the temp folder is removed when testing is complete
withr::defer({

# Clean up chromote sessions
gc() # Run R6 finalizer methods
Sys.sleep(2) # Wait for any supervisors to exit

# Delete the Crashpad folder if it exists
unlink(file.path(tempdir(), "Crashpad"), recursive = TRUE)
}, envir = testthat::teardown_env())
193 changes: 193 additions & 0 deletions tests/testthat/test-CommandBar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
init_driver <- function(app) {
shinytest2::AppDriver$new(app, variant = shinytest2::platform_variant())
}

items <- function() {
list(
CommandBarItem(
id = "new_item",
key = "new_item_value",
text = "New",
cacheKey = "myCacheKey",
split = TRUE,
iconProps = list(iconName = "Add"),
subMenuProps = list(
items = list(
CommandBarItem(
key = "email_message_value",
id = "email_message",
text = "Email message",
iconProps = list(iconName = "Mail")
)
)
)
),
CommandBarItem(
key = "download_value",
id = "download",
text = "Download",
iconProps = list(iconName = "Download")
)
)
}

far_items <- function() {
list(
CommandBarItem(
key = "tile_value",
id = "tile",
text = "Grid view",
ariaLabel = "Grid view",
iconOnly = TRUE,
iconProps = list(iconName = "Tiles")
)
)
}

describe("CommandBar", {
it("should set input of `key` of clicked CommandBarItem", {
skip_on_cran()

# Arrange
app <- init_driver(shiny::shinyApp(
ui = shiny::tagList(
CommandBar(
items = items(),
farItems = far_items()
)
),
server = function(input, output) { }
))
withr::defer(app$stop())

# Act
app$click(selector = "#download")
value <- app$get_value(input = "download_value")

# Assert
expect_equal(value, 0)
})
})


describe("CommandBar.shinyInput", {
test_app <- function() {
shiny::shinyApp(
ui = shiny::tagList(
CommandBar.shinyInput(
inputId = "commandBar",
items = items(),
farItems = far_items()
)
),
server = function(input, output) { }
)
}

it("should yield NULL input value on startup", {
skip_on_cran()

# Arrange
app <- init_driver(test_app())
withr::defer(app$stop())

# Act
value <- app$get_value(input = "commandBar")

# Assert
expect_null(value)
})

it("should set input on id of CommandBar with `key` value of clicked CommandBarItem", {
skip_on_cran()

# Arrange
app <- init_driver(test_app())
withr::defer(app$stop())

# Act
app$click(selector = "#download")
value <- app$get_value(input = "commandBar")

# Assert
expect_equal(value, "download_value")
})

it("should set input on id of CommandBar with `key` value of clicked nested CommandBarItem", {
skip_on_cran()

# Arrange
app <- init_driver(test_app())
withr::defer(app$stop())

# Act
# Click dropdown button which is a sibling of #new_item when using split = TRUE
app$click(selector = "#new_item + button")
app$click(selector = "#email_message")
value <- app$get_value(input = "commandBar")

# Assert
expect_equal(value, "email_message_value")
})

it("should set input on id of CommandBar with `key` value of a clicked farItem", {
skip_on_cran()

# Arrange
app <- init_driver(test_app())
withr::defer(app$stop())

# Act
app$click(selector = "#tile")
value <- app$get_value(input = "commandBar")

# Assert
expect_equal(value, "tile_value")
})

it("should work with unset `farItems`", {
skip_on_cran()

# Arrange
app <- init_driver(shiny::shinyApp(
ui = shiny::tagList(
CommandBar.shinyInput(
inputId = "commandBar",
items = items()
)
),
server = function(input, output) { }
))
withr::defer(app$stop())

# Act
app$click(selector = "#download")
value <- app$get_value(input = "commandBar")

# Assert
expect_equal(value, "download_value")
})

it("should work with unset `items`", {
skip_on_cran()

# Arrange
app <- init_driver(shiny::shinyApp(
ui = shiny::tagList(
CommandBar.shinyInput(
inputId = "commandBar",
farItems = far_items()
)
),
server = function(input, output) { }
))
withr::defer(app$stop())

# Act
app$click(selector = "#tile")
value <- app$get_value(input = "commandBar")

# Assert
expect_equal(value, "tile_value")
})
})

0 comments on commit ce0a740

Please sign in to comment.