-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: switch to testing html instead of screenshots
- Loading branch information
Jakub Sobolewski
committed
Sep 1, 2023
1 parent
7574818
commit 9a83fd2
Showing
5 changed files
with
41 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,6 @@ Suggests: | |
rmarkdown, | ||
styler, | ||
testthat, | ||
shinytest2 | ||
shinytest2, | ||
chromote, | ||
withr |
Binary file removed
BIN
-20.1 KB
tests/testthat/_snaps/mac-4.3/rendering/doesnt_render_html_from_ui.png
Binary file not shown.
Binary file removed
BIN
-13.1 KB
tests/testthat/_snaps/mac-4.3/rendering/render_html_from_server.png
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"><span id="test">Hello<span style="font-weight: bold;"> World</span></span></div>' #nolint | ||
) | ||
}) | ||
}) |