diff --git a/inst/apps/316-bslib-popovers/tests/testthat/test-316-bslib-popovers.R b/inst/apps/316-bslib-popovers/tests/testthat/test-316-bslib-popovers.R index 9b3f537234..9c5811ece1 100644 --- a/inst/apps/316-bslib-popovers/tests/testthat/test-316-bslib-popovers.R +++ b/inst/apps/316-bslib-popovers/tests/testthat/test-316-bslib-popovers.R @@ -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 -------------------------------------------------- @@ -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')")$ + get_text(".popover-header") + + expect_equal(trimws(header_actual), header) + } } @@ -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 @@ -118,16 +140,16 @@ 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") @@ -135,45 +157,48 @@ test_that("Can tab focus various cases/options", { 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