Skip to content

Commit

Permalink
refactor: switch to testing html instead of screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Sobolewski committed Sep 1, 2023
1 parent 7574818 commit 9a83fd2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ Suggests:
rmarkdown,
styler,
testthat,
shinytest2
shinytest2,
chromote,
withr
Binary file not shown.
Binary file not shown.
Binary file not shown.
54 changes: 38 additions & 16 deletions tests/testthat/test-rendering.R
Original file line number Diff line number Diff line change
@@ -1,57 +1,79 @@
init_driver <- function(app) {
initDriver <- function(app) {
shinytest2::AppDriver$new(app, variant = shinytest2::platform_variant())
}

describe("rendering with htmltools::HTML", {
it("renders HTML strings in React context from ui function when wrapped with `htmltools::HTML`", {
skip_on_cran()

app <- init_driver(shiny::shinyApp(
# Arrange
app <- initDriver(shiny::shinyApp(
ui = ReactContext(htmltools::HTML(
"<span style='font-weight: bold;'>Hello <span style='font-weight: normal;'>from ReactContext in UI</span></span>"
'<span id="test">Hello<span style="font-weight: bold;"> World</span></span>'
)),
server = function(input, output) {}
))
withr::defer(app$stop())

app$expect_screenshot(
name = "render_html_from_ui",
cran = FALSE
# Act
html <- app$get_html("#test")

# Assert
expect_equal(
html,
'<span id="test">Hello<span style="font-weight: bold;"> World</span></span>'
)
})

it("renders HTML strings from renderReact when wrapped with `htmltools::HTML`", {
skip_on_cran()

app <- init_driver(shiny::shinyApp(
# Arrange
app <- initDriver(shiny::shinyApp(
ui = reactOutput("react_output"),
server = function(input, output) {
output$react_output <- renderReact({
htmltools::HTML(
"<span style='font-weight: bold;'>Hello <span style='font-weight: normal;'>from ReactContext in renderReact</span></span>"
'<span id="test">Hello<span style="font-weight: bold;"> World</span></span>'
)
})
}
))
withr::defer(app$stop())

# Act
html <- app$get_html("#test")

app$expect_screenshot(
name = "render_html_from_server",
cran = FALSE
# Assert
expect_equal(
html,
'<span id="test">Hello<span style="font-weight: bold;"> World</span></span>'
)
})

it("doesn't render HTML strings in React context without `htmltools::HTML`", {
skip_on_cran()

app <- init_driver(shiny::shinyApp(
# Arrange
app <- initDriver(shiny::shinyApp(
ui = ReactContext(
"<span style='font-weight: bold;'>Hello <span style='font-weight: normal;'>from ReactContext in UI without htmltools::HTML</span></span>"
'<span id="test">Hello<span style="font-weight: bold;"> World</span></span>'
),
server = function(input, output) {}
))
withr::defer(app$stop())

# Act
html <- app$get_html("#test")
htmlContainer <- app$get_html(".react-container")

app$expect_screenshot(
name = "doesnt_render_html_from_ui",
cran = FALSE
# Assert
# Span hasn't been rendered as HTML, so it's null
expect_null(html)
# The container div has been rendered as HTML, but the span is escaped
expect_equal(
htmlContainer,
'<div class="react-container">&lt;span id="test"&gt;Hello&lt;span style="font-weight: bold;"&gt; World&lt;/span&gt;&lt;/span&gt;</div>' #nolint
)
})
})

0 comments on commit 9a83fd2

Please sign in to comment.