tests/testthat/test-plugin-html.R

test_that("HTML App initializes", {

  app <- expect_r6(App$new(HTMLPlugin$new()), "App")
  app <- expect_r6(App$new(HTMLPlugin$new(autoreload = TRUE)), "App")
  expect_list(app$html)
  assert_true(app$plugins$html$config$html$autoreload)

})

test_that("HTML App starts and stops", {

  app <- expect_r6(App$new(HTMLPlugin$new()), "App")
  expect_error(app$lifecycle_start(), NA)
  expect_true(app$lifecycle_is_running())
  expect_error(app$lifecycle_stop(), NA)

})

test_that("router_html() works", {

  app <- App$new(TestPlugin$new(), HTMLPlugin$new())

  app$html$router("/hello_world", function(request, response, keys, ...) {
    response$body <- tags$h1("Hello World")
    return(FALSE)
  })

  app$html$router(
    "/hello_world_fragment",
    function(request, response, keys, ...) {
      response$body <- tags$h1("Hello World")
      return(FALSE)
    },
    fragment = TRUE
  )

  app$html$router("/autoreload", function(request, response, keys, ...) {
    response$body <- tagList(
      tags$head(autoreload_assets()),
      tags$h1("Hello World")
    )
    return(TRUE)
  })

  res <- app$test$request("/hello_world")
  expect_equal(res$status, 200L)
  expect_equal(res$headers$`Content-Type`, "text/html")
  expect_string(res$body, fixed = "<h1>Hello World</h1>")

  res <- app$test$request("/hello_world_fragment")
  expect_equal(res$status, 200L)
  expect_equal(res$headers$`Content-Type`, "text/html")
  expect_equal(res$body, "<h1>Hello World</h1>")

  res <- app$test$request("/autoreload")
  expect_equal(res$status, 200L)
  expect_equal(res$headers$`Content-Type`, "text/html")
  expect_string(res$body, fixed = "autoreload.js")

})
tjpalanca/webtools documentation built on Dec. 23, 2021, 11 a.m.