diff --git a/inst/draggable-buckets/draggable-buckets.js b/inst/draggable-buckets/draggable-buckets.js index 66926a95..4e8a9451 100644 --- a/inst/draggable-buckets/draggable-buckets.js +++ b/inst/draggable-buckets/draggable-buckets.js @@ -89,7 +89,9 @@ $.extend(draggableBuckets, { subscribe: function (el, callback) { if (this.observers === undefined) this.observers = {}; - this.observers[el] = new MutationObserver(callback); + this.observers[el] = new MutationObserver(function () { + callback({ priority: "event" }); + }); this.observers[el].observe(el, { subtree: true, childList: true, diff --git a/tests/testthat/test-draggable_buckets.R b/tests/testthat/test-draggable_buckets.R new file mode 100644 index 00000000..41ccbeb4 --- /dev/null +++ b/tests/testthat/test-draggable_buckets.R @@ -0,0 +1,104 @@ +app_driver_db <- function(input_id, label, elements = character(), buckets) { + ui <- bslib::page_fluid( + draggable_buckets( + input_id = input_id, + label = label, + elements = elements, + buckets = buckets + ) + ) + + shinytest2::AppDriver$new( + shiny::shinyApp(ui, function(input, output) {}) + ) +} + +testthat::test_that( + "e2e: teal.widgets::draggable_buckets: initializes without input", + { + skip_if_too_deep(5) + app_driver <- app_driver_db( + input_id = "id", + label = "Choices", + elements = c("a", "b"), + buckets = c("bucket1", "bucket2") + ) + app_driver$wait_for_idle(timeout = default_idle_timeout) + testthat::expect_true(is_visible("#id.draggableBuckets.shiny-bound-input", app_driver)) + testthat::expect_identical( + app_driver$get_value(input = "id"), + list( + "bucket1" = list(), + "bucket2" = list() + ) + ) + app_driver$stop() + } +) + +testthat::test_that( + "e2e: teal.widgets::draggable_buckets: initializes with default inputs", + { + skip_if_too_deep(5) + app_driver <- app_driver_db( + input_id = "id", + label = "Choices", + elements = character(), + buckets = list( + "Ref" = "B: Placebo", + "Comp" = c("A: Drug X", "C: Combination") + ) + ) + app_driver$wait_for_idle(timeout = default_idle_timeout) + values <- app_driver$get_value(input = "id") + testthat::expect_identical( + app_driver$get_value(input = "id"), + list( + "Ref" = list("B: Placebo"), + "Comp" = list("A: Drug X", "C: Combination") + ) + ) + app_driver$stop() + } +) + +testthat::test_that( + "e2e: teal.widgets::draggable_buckets: moving elements between buckets updates input", + { + skip_if_too_deep(5) + app_driver <- app_driver_db( + input_id = "id", + label = "Choices", + elements = character(), + buckets = list( + "Ref" = "B: Placebo", + "Comp" = c("A: Drug X", "C: Combination") + ) + ) + app_driver$wait_for_idle(timeout = default_idle_timeout) + app_driver$run_js( + " + // Find the buckets + var widget = document.getElementById('id'); + var buckets = widget.querySelectorAll('.bucket'); + var refBucket = Array.from(buckets).find(b => b.dataset.label === 'Ref'); + var compBucket = Array.from(buckets).find(b => b.dataset.label === 'Comp'); + // Find element 'A: Drug X' in compBucket + var element = Array.from(compBucket.querySelectorAll('.element')).find(e => e.textContent.trim() === 'A: Drug X'); + // Move it to refBucket + refBucket.appendChild(element); + " + ) + app_driver$wait_for_idle(timeout = default_idle_timeout) + testthat::expect_identical( + app_driver$get_value(input = "id")$Ref, + list("B: Placebo", "A: Drug X") + ) + + testthat::expect_identical( + app_driver$get_value(input = "id")$Comp, + list("C: Combination") + ) + app_driver$stop() + } +) diff --git a/tests/testthat/test-draggable_buckets_ui.R b/tests/testthat/test-draggable_buckets_ui.R deleted file mode 100644 index 33ce2763..00000000 --- a/tests/testthat/test-draggable_buckets_ui.R +++ /dev/null @@ -1,23 +0,0 @@ -app_driver_db <- function() { - ui <- bslib::page_fluid( - draggable_buckets("id", "Choices #1", c("a", "b"), c("bucket1", "bucket2")) - ) - - shiny::shinyApp(ui, function(input, output) {}) -} - -testthat::test_that( - "e2e: teal.widgets::draggable_buckets: initializes", - { - skip_if_too_deep(5) - app_driver <- shinytest2::AppDriver$new( - app_driver_db(), - name = "db", - variant = "app_driver_db_ui" - ) - app_driver$wait_for_idle(timeout = default_idle_timeout) - testthat::expect_true(is_visible("body > div", app_driver)) - testthat::expect_true(is_visible(".draggableBuckets.shiny-bound-input", app_driver)) - app_driver$stop() - } -) diff --git a/tests/testthat/test-optionalSelectInput_ui.R b/tests/testthat/test-optionalSelectInput_ui.R index c56b88f1..8b7c91d9 100644 --- a/tests/testthat/test-optionalSelectInput_ui.R +++ b/tests/testthat/test-optionalSelectInput_ui.R @@ -115,23 +115,9 @@ testthat::test_that( testthat::expect_equal(gv$output[[i]], output[[i]]) } - # app_driver$click( - # selector = "body > div.container-fluid > bslib-layout-columns > div:nth-child(2) span.bs-select-clear-selected" - # ) - - # app_driver$get_value(output = "c2_out") %>% - # testthat::expect_equal("NULL") - - # app_driver$get_value(input = "c2") %>% - # testthat::expect_equal(NULL) - - # app_driver$get_text("#c2_input .filter-option-inner-inner") |> - # testthat::expect_equal("- Nothing selected -") - # Check allow clear behavior on c2 input where multiple is FALSE allow_clear_selector <- "#c2_input span.bs-select-clear-selected" app_driver$click(selector = allow_clear_selector) - app_driver$wait_for_idle(duration = default_idle_timeout) testthat::expect_equal(app_driver$get_value(output = "c2_out"), "NULL") testthat::expect_equal(app_driver$get_value(input = "c2"), NULL) testthat::expect_equal( diff --git a/tests/testthat/test-verbatim_popup_ui.R b/tests/testthat/test-verbatim_popup_ui.R index 1c5f7bd5..068c3c83 100644 --- a/tests/testthat/test-verbatim_popup_ui.R +++ b/tests/testthat/test-verbatim_popup_ui.R @@ -68,7 +68,7 @@ testthat::test_that( "Copy to Clipboard" ) testthat::expect_equal( - app_driver$get_text(".modal-footer button:nth-of-type(2)"), + app_driver$get_text(".modal-footer button:nth-of-type(1)"), "Dismiss" )