Skip to content

Commit

Permalink
tests(316): Small tweaks to attempt to reduce flakiness (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie authored Oct 24, 2023
1 parent 2ad37f1 commit 3dd13fe
Showing 1 changed file with 72 additions and 43 deletions.
115 changes: 72 additions & 43 deletions inst/apps/316-bslib-popovers/tests/testthat/test-316-bslib-popovers.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ DO_SCREENSHOT <- is_testing_on_ci && is_mac_release

source(system.file("helpers", "keyboard.R", package = "shinycoreci"))

expect_js <- function(app, js, label = NULL) {
expect_true(
app$wait_for_js(!!js)$get_js(!!js),
label = label
)
invisible(app)
}

expect_focus <- function(app, selector) {
js <- sprintf(
"document.activeElement === document.querySelector('%s')",
selector
)
app$wait_for_js(js)
invisible(app)
expect_js(app, js, label = paste("Focus is on:", selector))
}

# Setup App --------------------------------------------------
Expand Down Expand Up @@ -51,35 +58,50 @@ key_press <- key_press_factory(app)

# lastShown should contain the trigger element, which we can use to find the
# actual tooltip (we just make sure it's visible).
expect_visible_tip <- function(app, selector) {
app$wait_for_js(
expect_visible_tip <- function(app, selector, expect_tabbable = FALSE) {
expect_js(
app,
sprintf("window.lastShown === document.querySelector('%s')", selector)
)
app$wait_for_js(

expect_js(
app,
"var tipId = window.lastShown.getAttribute('aria-describedby');
$(`#${tipId}:visible`).length > 0;"
)

if (expect_tabbable) {
expect_js(app, sprintf(
"document.querySelector('%s').tabIndex === 0",
selector
))
}
}

expect_no_tip <- function() {
app$wait_for_js("$('.popover:visible').length === 0;")
expect_no_tip <- function(app) {
expect_js(app, "$('.popover:visible').length === 0;")
}

click_close_button <- function() {
click_close_button <- function(app) {
app$click(selector = ".popover .btn-close")
}

expect_popover_content <- function(app, body = NULL, header = NULL) {
body_js <- sprintf(
"document.querySelector('.popover-body').innerText === '%s'",
body
)
header_js <- sprintf(
"document.querySelector('.popover-header').innerText === '%s'",
header
)
if (!is.null(body)) app$wait_for_js(body_js)
if (!is.null(header)) app$wait_for_js(header_js)
if (!is.null(body)) {
body_actual <- app$
wait_for_js("document.querySelector('.popover-body') !== null")$
get_text(".popover-body")

expect_equal(trimws(body_actual), body)
}

if (!is.null(header)) {
header_actual <- app$
wait_for_js("document.querySelector('.popover-header') !== null")$
get_text(".popover-header")

expect_equal(trimws(header_actual), header)
}
}


Expand All @@ -98,7 +120,7 @@ test_that("Can tab focus various cases/options", {
expect_focus(app, "#pop-hello span")
expect_visible_tip(app, "#pop-hello span")
key_press("Enter")
expect_no_tip()
expect_no_tip(app)
expect_focus(app, "#pop-hello span")

# Make sure the popover is focusable via keyboard
Expand All @@ -118,62 +140,65 @@ test_that("Can tab focus various cases/options", {
expect_focus(app, "#pop-hello span")
expect_visible_tip(app, "#pop-hello span")

click_close_button()
click_close_button(app)
expect_focus(app, "#pop-hello span")
expect_no_tip()
expect_no_tip(app)

key_press("Enter")
expect_focus(app, "#pop-hello span")
expect_visible_tip(app, "#pop-hello span")
key_press("Escape")
expect_focus(app, "#pop-hello span")
expect_no_tip()
expect_no_tip(app)
key_press("Enter")
expect_focus(app, "#pop-hello span")
expect_visible_tip(app, "#pop-hello span")
key_press("Tab")
key_press("Tab")
key_press("Escape")
expect_focus(app, "#pop-hello span")
expect_no_tip()
expect_no_tip(app)

key_press("Tab")
key_press("Enter")
expect_focus(app, "#pop-inline span")
expect_visible_tip(app, "#pop-inline span")
key_press("Enter")
expect_no_tip()
expect_no_tip(app)

key_press("Tab")
expect_focus(app, "#pop-hyperlink a")
expect_visible_tip(app, "#pop-hyperlink a")

key_press("Tab")
expect_no_tip()
expect_no_tip(app)
key_press("Enter")
expect_focus(app, "#btn_link")
expect_visible_tip(app, "#btn_link")
key_press("Enter")
expect_no_tip()
expect_no_tip(app)
expect_true(app$get_value(input = "btn_link") == 2)

# For some odd reason it seems a key_press("Enter") on a <button> doesn't
# simulate a click event? This seems to be a chromote specific issue.
expect_no_tip()
expect_no_tip(app)
app$click(selector = "#btn")
expect_visible_tip(app, "#btn")
click_close_button()
expect_no_tip()
click_close_button(app)
expect_no_tip(app)
expect_focus(app, "#btn")

app$click(selector = "#btn3")
expect_visible_tip(app, "#btn3")
click_close_button()
expect_no_tip()
click_close_button(app)
expect_no_tip(app)
expect_focus(app, "#btn3")

app$click(selector = "#pop-offset")
expect_visible_tip(app, "#pop-offset")

click_close_button(app)
expect_no_tip(app)
})


Expand All @@ -190,15 +215,15 @@ test_that("Can programmatically update/show/hide tooltip", {
expect_popover_content(app, "new")

app$click("hide_popover")
expect_no_tip()
expect_no_tip(app)

app$set_inputs("popover_msg" = "newer")

app$click("show_popover")
expect_popover_content(app, "newer")

app$set_inputs("navbar" = "Popover cases")
expect_no_tip()
expect_no_tip(app)
app$set_inputs("navbar" = "Popover updates")

app$set_inputs("show_title" = TRUE)
Expand All @@ -208,6 +233,8 @@ test_that("Can programmatically update/show/hide tooltip", {
app$set_inputs("show_title" = FALSE)
expect_popover_content(app, "newer", "")

click_close_button(app)
expect_no_tip(app)
})


Expand All @@ -216,10 +243,12 @@ test_that("Can put input controls in the popover", {

app$set_inputs("navbar" = "Popover inputs")

key_press("Tab")
key_press("Tab")
app$run_js("$('#inc').focus()")
expect_focus(app, "#inc")

app$click(selector = "#btn4")
expect_visible_tip(app, "#btn4")
expect_visible_tip(app, "#btn4", expect_tabbable = TRUE)

key_press("Tab")
expect_focus(app, ".popover")
key_press("Tab")
Expand All @@ -236,17 +265,17 @@ test_that("Can put input controls in the popover", {
expect_equal(app$wait_for_value(input = "num", ignore = 5), 4)
key_press("Escape")
expect_focus(app, "#btn4")
expect_no_tip()
expect_no_tip(app)

# The UI is hidden, but we can still update the numeric input
app$click("inc")
expect_equal(app$wait_for_value(input = "num", ignore = 4), 5)
app$click("inc")
expect_equal(app$wait_for_value(input = "num", ignore = 5), 6)

app$click(selector = "#btn4")
expect_visible_tip(app, "#btn4")
# Even though the tip is visible, it seems it's not always ready to be
# tabbed into, so wait a bit before doing so
{Sys.sleep(0.5); key_press("Tab")}
expect_visible_tip(app, "#btn4", expect_tabbable = TRUE)
key_press("Tab")
expect_focus(app, '.popover')
key_press("Tab")
expect_focus(app, 'input#num')
Expand All @@ -257,6 +286,6 @@ test_that("Can put input controls in the popover", {

key_press("Escape")
expect_visible_tip(app, "#btn4")
click_close_button()
expect_no_tip()
click_close_button(app)
expect_no_tip(app)
})

0 comments on commit 3dd13fe

Please sign in to comment.