Skip to content

Commit

Permalink
Move test to apps test folder
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke committed Aug 5, 2024
1 parent 585e585 commit d68a275
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 90 deletions.
68 changes: 68 additions & 0 deletions tests/testthat/apps/task-button/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
library(shiny)
yoink <- function(pkg, fn_name) {
getFromNamespace(fn_name, pkg)
}
promise <- yoink("promises", "promise")
later <- yoink("later", "later")
input_task_button <- yoink("bslib", "input_task_button")
bind_task_button <- yoink("bslib", "bind_task_button")



ui <- fluidPage(
actionButton("run_normal", "Run normal"),
textOutput("normal_result"),
input_task_button("run_async", "Run async"),
textOutput("async_result")
)

server <- function(input, output, session) {
returned_value <- reactiveValues()

slow_function <- function() {
return(10)
}

task <- ExtendedTask$new(function() {
promise(function(resolve, reject) {
# Use later to simulate future promise calls
later(function() {
resolve(slow_function())
# Add extra time for extra checks
}, delay = 0.01)
})
}) |> bind_task_button("run_async")


# Normal
observeEvent(input$run_normal, {
returned_value$normal <- slow_function()
})

output$normal_result <- renderText({
req(returned_value$normal)
returned_value$normal
})

# Async
observeEvent(input$run_async, {
task$invoke()
})

observe({
returned_value$async <- task$result()
})

output$async_result <- renderText({
req(returned_value$async)
returned_value$async
})

# Export for testing
exportTestValues(
async = returned_value$async,
normal = returned_value$normal
)
}

shinyApp(ui = ui, server = server)
1 change: 1 addition & 0 deletions tests/testthat/apps/task-button/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shinytest2::test_app()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Load application support files into testing environment
shinytest2::load_app_env()
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Related https://github.com/rstudio/shinytest2/issues/388
# Reprex altered from https://stackoverflow.com/questions/78517385/testing-async-extendedtask-with-shinytest2
# 2 issues from reprex:
# * $click() did not work as task button preprocessor wasn't registered
# * Asserting the value of the resolved async output needs to happen after the output value updates due to timeing issues
test_that("bslib task button", {
skip_on_cran()
skip_on_os("windows")
skip_if_not_installed("bslib")
skip_if_not_installed("promises")
skip_if_not_installed("later")


app <- AppDriver$new()

app$click("run_normal")
expect_equal(app$get_value(export = "normal"), 10)

# Get current value
cur_async_value <- app$get_value(export = "async")
app$click("run_async")
# Wait until expected output changes
new_async_value <- app$wait_for_value(export = "async", ignore = list(cur_async_value))
# Assert new value
expect_equal(new_async_value, 10)
})
90 changes: 0 additions & 90 deletions tests/testthat/test-bslib-task-btn.R

This file was deleted.

0 comments on commit d68a275

Please sign in to comment.