tests/testthat/test-devtools.R

test_that("inspect_widget reports type, state, style and rules", {
  a <- app(vertical(button("Save", id = "save", classes = "primary")),
           stylesheet = "Button.primary { border: double; }")
  pilot <- test_app(a, 30, 5)
  info <- inspect_widget(a, "#save")
  expect_s3_class(info, "termr_inspection")
  expect_identical(info$type, "Button")
  expect_identical(info$classes, "primary")
  expect_true("focus" %in% info$states)
  expect_identical(info$style$border, "double")
  expect_identical(info$stylesheet_rules, "Button.primary")
  expect_identical(info$state$label, "Save")
  expect_match(info$parent, "Vertical")
  out <- capture.output(print(info))
  expect_match(out[[1]], "<Button #save>")
  expect_true(any(grepl("rules:", out)))
  expect_error(inspect_widget(42), "must be a widget")
})

test_that("the event log records dispatched events only when enabled", {
  a <- app(input(id = "in"))
  pilot <- test_app(a, 20, 3)
  pilot$type("a")
  expect_null(a$event_log())
  a$log_events(size = 50)
  pilot$type("bc")
  log <- a$event_log()
  expect_true(all(c("key", "input.changed") %in% log$type))
  a$log_events(size = 5)
  pilot$type("defghij")
  expect_identical(nrow(a$event_log()), 5L)
  a$log_events(FALSE)
  expect_null(a$event_log())
})

test_that("F12 toggles the debug overlay", {
  a <- app(vertical(button("Go", id = "go")), debug = TRUE)
  pilot <- test_app(a, 60, 15)
  pilot$press("f12")
  text <- pilot$screen_text()
  expect_true(any(grepl("termr debug", text)))
  expect_true(any(grepl("Focused", text)))
  pilot$press("tab")
  pilot$advance(0.6)
  expect_true(any(grepl("Last event +key tab", pilot$screen_text())))
  # The overlay does not take keys or focus.
  expect_identical(a$focused$id, "go")
  pilot$press("f12")
  expect_false(any(grepl("termr debug", pilot$screen_text())))
  expect_false(any(vapply(app(label("x"))$bindings(), function(b) "f12" %in% b$keys, logical(1))))
})

Try the termr package in your browser

Any scripts or data that you put into this service are public.

termr documentation built on Oct. 21, 2026, 5:13 p.m.