-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
97 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
shinytest2::test_app() |
2 changes: 2 additions & 0 deletions
2
tests/testthat/apps/task-button/tests/testthat/setup-shinytest2.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
26 changes: 26 additions & 0 deletions
26
tests/testthat/apps/task-button/tests/testthat/test-bslib-task-btn.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file was deleted.
Oops, something went wrong.