Skip to content

Commit 3d95be6

Browse files
committed
feat: with_test_id() adds a data-testid attribute to a shiny tag.
This is useful for testing with `getByTestId` in Playwright.
1 parent b0f8b7c commit 3d95be6

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: pw
22
Title: Test your 'golem' Apps with 'Playwright'
3-
Version: 0.0.0.9007
3+
Version: 0.0.0.9008
44
Authors@R:
55
person("Colin", "Fay", , "contact@colinfay.me", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0001-7343-1846"))

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export(pw_init)
66
export(pw_show_report)
77
export(pw_test)
88
export(stop_if_npx_not_available)
9+
export(with_test_id)
910
importFrom(cli,cat_line)
1011
importFrom(cli,cli_abort)
1112
importFrom(cli,cli_alert_success)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# pw (development version)
22

3+
* `with_test_id()` adds a `data-testid` attribute to a shiny tag. This is useful for testing with `getByTestId` in Playwright.
4+
35
* `pw_show_report()` can be called to open the test report
46

57
* `pw_codegen()` can be called to launch the code generator

R/with_test_id.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#' Add a data-testid attribute to a tag
2+
#'
3+
#' This function adds a data-testid attribute to a shiny tag. This is useful for testing with `getByTestId` in Playwright.
4+
#' @param tag A shiny tag
5+
#' @param test_id A string to use as the value of the data-testid attribute
6+
#' @return A shiny tag with the data-testid attribute added
7+
#' @export
8+
with_test_id <- function(tag, test_id){
9+
shiny::tagAppendAttributes(
10+
tag,
11+
`data-testid` = test_id
12+
)
13+
}

man/with_test_id.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-with_test_id.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test_that("with_test_id works", {
2+
tag <- shiny::div()
3+
test_id <- "test-id"
4+
tag_with_test_id <- with_test_id(tag, test_id)
5+
expect_equal(
6+
tag_with_test_id$attribs$`data-testid`,
7+
test_id
8+
)
9+
})

0 commit comments

Comments
 (0)