| 1 |
test_that("Nested modules", { |
| 2 |
child <- function(id) { |
| 3 |
moduleServer(id, function(input, output, session) { |
| 4 |
output$txt <- renderText("bar") |
| 5 |
}) |
| 6 |
} |
| 7 |
|
| 8 |
parent <- function(id) { |
| 9 |
moduleServer(id, function(input, output, session) { |
| 10 |
output$txt <- renderText("foo") |
| 11 |
child("child-id") |
| 12 |
}) |
| 13 |
} |
| 14 |
|
| 15 |
testServer(parent, args = list(id = "parent-id"), { |
| 16 |
expect_equal(output$txt, "foo") |
| 17 |
}) |
| 18 |
|
| 19 |
}) |
| 20 |
|
| 21 |
test_that("Lack of ID", { |
| 22 |
module <- function(id) { |
| 23 |
moduleServer(id, function(input, output, session) { |
| 24 |
output$txt <- renderText(session$ns("x")) |
| 25 |
}) |
| 26 |
} |
| 27 |
|
| 28 |
testServer(module, args = list(id = "foo"), { |
| 29 |
expect_equal(output$txt, "foo-x") |
| 30 |
}) |
| 31 |
}) |
| 32 |
|
| 33 |
test_that("testServer works with nested module servers", { |
| 34 |
outerModule <- function(id) { |
| 35 |
moduleServer(id, function(input, output, session) { |
| 36 |
r1 <- reactive({ input$x + 1}) |
| 37 |
r2 <- innerModule("inner", r1) |
| 38 |
output$someVar <- renderText(r2()) |
| 39 |
}) |
| 40 |
} |
| 41 |
|
| 42 |
innerModule <- function(id, r) { |
| 43 |
moduleServer(id, function(input, output, session) { |
| 44 |
reactive(paste("a value:", r())) |
| 45 |
}) |
| 46 |
} |
| 47 |
|
| 48 |
testServer(outerModule, args = list(id = "foo"), { |
| 49 |
session$setInputs(x = 1) |
| 50 |
expect_equal(output$someVar, "a value: 2") |
| 51 |
}) |
| 52 |
}) |
| 53 |
|
| 54 |
test_that("testServer calls do not nest in module functions", { |
| 55 |
module <- function(id) { |
| 56 |
moduleServer(id, function(input, output, session) { |
| 57 |
x <- 1 |
| 58 |
testServer(function(id) { |
| 59 |
moduleServer(id, function(input, output, session) { |
| 60 |
y <- x + 1 |
| 61 |
}) |
| 62 |
}) |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
expect_error(testServer(module, {})) |
| 67 |
}) |
| 68 |
|
| 69 |
test_that("testServer calls do not nest in test exprs", { |
| 70 |
module <- function(id) { |
| 71 |
x <- 1 |
| 72 |
moduleServer(id, function(input, output, session) { |
| 73 |
inner <- function(id) { |
| 74 |
moduleServer(id, function(input, output, session) { |
| 75 |
y <- x + 1 |
| 76 |
}) |
| 77 |
} |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
expect_error(testServer(module, { testServer(inner, {}) })) |
| 82 |
}) |