2025-06-11

🔗 Rencontres R 2023…

myShinyApp/
├── DESCRIPTION
├── NAMESPACE
├── app.R
├── R/
│   ├── app_ui.R
│   ├── app_server.R
│   └── run_app.R
├── tests/
│   ├── testthat/
│   │   ├── test-calculate_average.R
│   │   ├── test-golem-recommended.R
│   │   └── ...
│   └── testthat.R

  • golem::use_recommended_tests()

  • usethis::use_test("calculate_average")

calculate_average <- function(values) {
  if (!is.numeric(values)) {
    stop("values must be numeric")
  }
  if (length(values) == 0) {
    return(0)
  }
  sum(values) / length(values)
}
test_that("calculate_average works", {
  # Test with numeric values
  expect_equal(
    object = calculate_average(c(10, 20, 30)), 
    expected = 20
  )
  
  # Test with empty vector
  expect_equal(
    object = calculate_average(0), 
    expected = 0
  )
  
  # Test with non-numeric input
  expect_error(
    object = calculate_average(c("a", "b")), 
    "values must be numeric"
  )
})

 

 

app_ui <- function(request) {
  fluidPage(
    numericInput(
      inputId = "num1",
      label = "First value",
      value = 10
    ),
    numericInput(
      inputId = "num2",
      label = "Second value",
      value = 10
    ),
    ...
    actionButton(
      inputId = "go",
      label = "Calculate!"
    )
  )
}

app_ui <- function(request) {
  fluidPage(
    numericInput(
      inputId = "num1",
      label = "First value",
      value = 10
    ),
    numericInput(
      inputId = "num2",
      label = "Second value",
      value = 10
    ),
    ...
    actionButton(
      inputId = "go",
      label = "Calculate!"
    )
  )
}
app_server <- function(input, output, session) {

  rv <- reactiveValues()

  observeEvent(input$go, {
    rv$avg <- calculate_average(
      values = c(
        input$num1,
        input$num2,
        input$num3,
        input$num4
      )
    )
  })
}

testServer(app_server, {
  session$setInputs(num1 = 5)
  session$setInputs(num2 = 5)
  session$setInputs(num3 = 5)
  session$setInputs(num4 = 5)
  session$setInputs(go = 1)

  expect_equal(
    object = rv$avg, 
    expected = 5
  )

  session$setInputs(num1 = 10)
  session$setInputs(num2 = 20)
  session$setInputs(num3 = 30)
  session$setInputs(num4 = 12)
  session$setInputs(go = 2)

  expect_equal(
    object = rv$avg,
    expected = 18
  )
})
app_server <- function(input, output, session) {

  rv <- reactiveValues()

  observeEvent(input$go, {
    rv$avg <- calculate_average(
      values = c(
        input$num1,
        input$num2,
        input$num3,
        input$num4
      )
    )
  })
}

import { test, expect } from '@playwright/test';

test('Fill signature is working', async ({ page }) => {
  await page.goto('http://localhost:3000/');
  await expect(page.getByTestId("appName")).toBeVisible();

  await expect(page.getByTestId('firstname')).toBeVisible();
  await expect(page.getByPlaceholder('John')).toBeEmpty();
  await page.getByPlaceholder('John').click();
  await page.getByPlaceholder('John').fill('Arthur');
  await expect(page.getByTestId('signature-names')).toMatchAriaSnapshot(`- 'cell "Arthur {{lastname}}"'`);

  await page.getByPlaceholder('Doe').fill('Bréant');
  await expect(page.getByTestId('signature-names')).toMatchAriaSnapshot(`- 'cell "Arthur Bréant"'`);

  await page.getByRole('button', { name: 'copy fa-solid icon Copy to' }).click();
  await expect(page.getByText('Ă—Paste the signature in your')).toBeVisible();
});