Skip to content

Commit

Permalink
add test for toast
Browse files Browse the repository at this point in the history
  • Loading branch information
hypebright committed Mar 22, 2024
1 parent f324abf commit f669c08
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
27 changes: 3 additions & 24 deletions R/f7Toast.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,9 @@
#' @param ... Other options. See \url{https://framework7.io/docs/toast.html#toast-parameters}.
#' @param session Shiny session.
#'
#' @export
#' @example inst/examples/toast/app.R
#'
#' @examples
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#' shinyApp(
#' ui = f7Page(
#' title = "Toast",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "f7Toast"),
#' f7Button(inputId = "toast", label = "Open Toast")
#' )
#' ),
#' server = function(input, output, session) {
#' observeEvent(input$toast, {
#' f7Toast(
#' position = "top",
#' text = "I am a toast. Eat me!"
#' )
#' })
#' }
#' )
#' }
#' @export
f7Toast <- function(text, position = c("bottom", "top", "center"),
closeButton = TRUE, closeButtonText = "close",
closeButtonColor = "red", closeTimeout = 3000, icon = NULL,
Expand All @@ -59,7 +38,7 @@ f7Toast <- function(text, position = c("bottom", "top", "center"),
...
)
)
# see my-app.js function
# see components/init.js
session$sendCustomMessage(
type = "toast",
message = jsonlite::toJSON(
Expand Down
23 changes: 23 additions & 0 deletions inst/examples/toast/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
library(shiny)
library(shinyMobile)

app <- shinyApp(
ui = f7Page(
title = "Toast",
f7SingleLayout(
navbar = f7Navbar(title = "f7Toast"),
f7Button(inputId = "toast", label = "Open Toast")
)
),
server = function(input, output, session) {
observeEvent(input$toast, {
f7Toast(
position = "top",
text = "I am a toast. Eat me!"
)
})
}
)

if (interactive() || identical(Sys.getenv("TESTTHAT"), "true")) app

21 changes: 21 additions & 0 deletions tests/testthat/test-f7Toast.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("Toast works", {
session <- as.environment(list(
ns = identity,
sendCustomMessage = function(type, message) {
session$lastCustomMessage <- list(type = type, message = message)
}
))

f7Toast(
position = "top",
text = "I am a toast. Eat me!",
session = session
)

res <- session$lastCustomMessage
res$message <- jsonlite::fromJSON(res$message)
expect_length(res, 2)
expect_equal(res$type, "toast")
expect_length(res$message, length(formals(f7Toast)) - 3)
expect_equal(res$message$closeTimeout, formals(f7Toast)$closeTimeout)
})

0 comments on commit f669c08

Please sign in to comment.