tests/testthat/test-app-duplicate-ids.R

require("shiny", quietly = TRUE, character.only = TRUE)

test_that("Duplicate input ids are found", {

  shiny_app <- shinyApp(
    ui = fluidPage(
      # Duplicate input IDs; Warning!
      textInput("text", "Text 1"),
      textInput("text", "Text 2")
    ),
    server = function(input, output) {
      # empty
    }
  )

  expect_warning(
    app <- AppDriver$new(shiny_app, check_names = TRUE),
    "text"
  )

  expect_failure(
    app$expect_unique_names(),
    "text"
  )
})

test_that("Duplicate input/output ids are found", {

  shiny_app <- shinyApp(
    ui = fluidPage(
      # Duplicate input/output ID; Warning!
      selectInput("select", "Selector", c("A", "B")),
      verbatimTextOutput("select")
    ),
    server = function(input, output) {
      # empty
    }
  )

  expect_warning(
    app <- AppDriver$new(shiny_app, check_names = TRUE),
    "select"
  )

  expect_failure(
    app$expect_unique_names(),
    "select"
  )
})



test_that("Duplicate custom output ids are found", {

  shiny_app <- shinyApp(
    ui = fluidPage(
      div(id = "custom", class = "shiny-bound-output", "Div 1 content"),
      div(id = "custom", class = "shiny-bound-output", "Div 2 content")
    ),
    server = function(input, output) {
      # empty
    }
  )

  expect_warning(
    app <- AppDriver$new(shiny_app, check_names = TRUE),
    "custom"
  )

  expect_failure(
    app$expect_unique_names(),
    "custom"
  )
})


test_that("Duplicate shiny output ids are found", {
  shiny_app <- shinyApp(
    ui = fluidPage(
      # Duplicate output IDs causes failure to load application
      htmlOutput("html"),
      textOutput("html")
    ),

    server = function(input, output) {
      # empty
    }
  )

  expect_warning(
    app <- AppDriver$new(shiny_app, check_names = TRUE),
    "html"
  )

  expect_failure(
    app$expect_unique_names(),
    "html"
  )
})

Try the shinytest2 package in your browser

Any scripts or data that you put into this service are public.

shinytest2 documentation built on June 24, 2024, 5:16 p.m.