tests/testthat/test-turbo.R

test_that("Turbo Plugin initializes", {

  turbo <- expect_r6(Turbo$new(), "Turbo")

})

test_that("Turbo assets can be generated", {

  turbo <- Turbo$new()
  expect_class(turbo$assets(TRUE), "shiny.tag.list")
  expect_null(turbo$assets(FALSE))
  modified <- expect_class(
    turbo$assets(list(pb_delay = 1000)),
    "shiny.tag.list"
  )
  expect_string(as.character(modified), "Turbo.setProgressBarDelay(1000);")

})

test_that("Turbo routers can be added", {

  # Router
  app <- App$new(HTMLPlugin$new(), Turbo$new(), TestPlugin$new())
  app$turbo$router("/", function(request, response, keys, ...) {
    response$body <- html_tags(h1("Hello World!"))
  })
  res <- app$test$request("/")
  expect_equal(res$status, 200L)
  expect_equal(res$headers$`Content-Type`, "text/html")
  expect_string(res$body, fixed = "assets/turbo")

  # Router Stream
  app$turbo$router_stream("/stream", function(request, response, keys, ...) {
    response$body <- turbo_stream(
      action = "remove",
      target = "to_remove"
    )
  })
  res <- app$test$request("/stream")
  expect_equal(res$status, 200L)
  expect_equal(res$headers$`Content-Type`, "text/vnd.turbo-stream.html")
  expect_string(res$body, fixed = "to_remove")

})

test_that("Turbo senders work", {

  app <- App$new(HTMLPlugin$new(), Turbo$new(), TestPlugin$new())
  expect_error(app$turbo$send("message", list(hello = "0")), NA)
  expect_error(app$turbo$log("info", "info message"), NA)
  expect_error(app$turbo$stream("append", "hello"), NA)
  expect_error(app$turbo$listen("before-visit", "hello"), NA)
  expect_error(app$turbo$clear(), NA)
  expect_error(app$turbo$visit("/", "advance"), NA)
  expect_character(app$turbo$clients, len = 0)

})
tjpalanca/hotwire.R documentation built on Dec. 23, 2021, 10:59 a.m.