Skip to content

Commit

Permalink
Integration tests for the 'persistent progress' feature (#254)
Browse files Browse the repository at this point in the history
* Integration tests for the 'persistent progress' feature

* Fix incorrect test

* Add test folder
  • Loading branch information
jcheng5 authored Mar 19, 2024
1 parent c417a34 commit 7544461
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
68 changes: 68 additions & 0 deletions inst/apps/227-persistent-progress/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
library(shiny)
library(bslib)
library(shinyjster)

ui <- function(req) {
page_sidebar(
sidebar = sidebar(
actionButton("calc1", "Recalc one"),
hr(),
actionButton("calc2", "Recalc two"),
actionButton("calc2p", "Progress two"),
actionButton("calc2e", "Error two"),
actionButton("calc2a", "Abort two"),
actionButton("calc2c", "Cancel output two"),
),
shinyjster_js(readLines("test.js")),
card(
card_header("One"),
plotOutput("plot1"),
),
card(
card_header("Two"),
plotOutput("plot2")
)
)
}

server <- function(input, output, session) {
shinyjster_server(input, output, session)

output$plot1 <- renderPlot({
input$calc1
plot(runif(10), runif(10))
})

plot2state = reactiveVal("value")
observeEvent(input$calc2, {
plot2state("value")
})
observeEvent(input$calc2p, {
plot2state("progress")
})
observeEvent(input$calc2e, {
plot2state("error")
})
observeEvent(input$calc2a, {
plot2state("abort")
})
observeEvent(input$calc2c, {
plot2state("cancel")
})

output$plot2 <- renderPlot({
input$calc2; input$calc2p; input$calc2e; input$calc2a; input$calc2c

switch(plot2state(),
value = NULL,
progress = req(FALSE, cancelOutput="progress"),
error = stop("boom"),
cancel = req(FALSE, cancelOutput=TRUE),
abort = req(FALSE),
)

plot(runif(10), runif(10))
})
}

shinyApp(ui, server)
96 changes: 96 additions & 0 deletions inst/apps/227-persistent-progress/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
function click(button_id) {
return () => {
$(`#${button_id}`).click();
};
}

recalc = click("calc2");
progress = click("calc2p");
error = click("calc2e");
abort = click("calc2a");
cancel = click("calc2c");

function recalculating(id = "plot2") {
return document.getElementById(id).classList.contains("recalculating");
}

function assertWithTimeout(assertion, timeout = 5000, interval = 50) {
return new Promise((resolve, reject) => {
const start = Date.now();
function attempt() {
try {
Jster.assert.isTrue(assertion());
resolve();
} catch (e) {
if (Date.now() - start > timeout) {
reject(e);
} else {
setTimeout(attempt, interval);
}
}
}
attempt();
});
}

function testcase(jst, name, fns, selector) {
const plot2 = document.getElementById("plot2");

jst.add(() => console.log(`Running test: ${name}`));
if (Array.isArray(fns)) {
for (const fn of fns) {
jst.add(fn);
}
} else {
jst.add(fns);
}
jst.add(Jster.shiny.waitUntilIdle);
jst.add(() => assertWithTimeout(() => plot2.matches(selector)));
}

var jst = jster();
jst.add(Jster.shiny.waitUntilIdle);

jst.add(click("calc1"));
testcase(jst, "Recalculation is persistent", progress, ".recalculating");
jst.add(() => Jster.assert.isTrue(!recalculating("plot1")));

testcase(
jst,
"Value stops recalculation",
[progress, recalc],
":not(.recalculating)"
);

testcase(
jst,
"Error stops recalculation",
[progress, error],
":not(.recalculating)"
);

testcase(
jst,
"Abort stops recalculation",
[progress, abort],
":not(.recalculating)"
);

testcase(
jst,
"Can stack recalculation",
[recalc, progress, (done) => setTimeout(done, 100), progress],
".recalculating"
);

const oldValue = document.querySelector("#plot1").innerHTML;
testcase(
jst,
"plot1 isn't blocked by plot2; plot2 doesn't stop recalculating because of plot1",
[progress, click("calc1")],
".recalculating"
);
jst.add(() =>
Jster.assert.isTrue(document.querySelector("#plot1").innerHTML !== oldValue)
);
jst.test();
1 change: 1 addition & 0 deletions inst/apps/227-persistent-progress/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shinytest2::test_app()
2 changes: 2 additions & 0 deletions inst/apps/227-persistent-progress/tests/testthat/setup.R
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 @@
shinyjster::testthat_shinyjster()

0 comments on commit 7544461

Please sign in to comment.