Skip to content

Commit

Permalink
Revert "simplification" of book module (which broke things).
Browse files Browse the repository at this point in the history
  • Loading branch information
jonthegeek committed Nov 12, 2023
1 parent e8c1aef commit 6ddb98c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
51 changes: 31 additions & 20 deletions R/book.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,38 @@
.book_server <- function(id = "book") {
book_choices <- .book_get_choices()
moduleServer(id, function(input, output, session) {
# Use the initial non-reactive query string from the request.
query <- parseQueryString(session$request$QUERY_STRING)
query_book <- book_choices[book_choices == query$bookname]
# I can't find a way to get a non-reactive version of the query string.
query_book <- reactive({
query <- getQueryString()
query_book <- book_choices[book_choices == query$bookname]
})

if (length(query_book) && query_book != "") {
updateSelectInput( # nocov start (can't find a way to automate)
session,
"selected_book",
label = "Book Selected",
choices = book_choices,
selected = query_book
) # nocov end
} else {
updateSelectInput(
session,
"selected_book",
label = "Book Selected",
choices = book_choices,
selected = NULL
)
}
# Only use the query_string to update the input when the app initially
# loads. After that, the input is the source of truth.
observeEvent(
query_book(),
{
if (length(query_book()) && query_book() != input$selected_book) {
updateSelectInput( # nocov start (can't find a way to automate)
session,
"selected_book",
label = "Book Selected",
choices = book_choices,
selected = query_book()
) # nocov end
} else {
updateSelectInput(
session,
"selected_book",
label = "Book Selected",
choices = book_choices,
selected = NULL
)
}
},
ignoreNULL = FALSE,
once = TRUE
)

observeEvent(
input$selected_book != "",
Expand Down
19 changes: 8 additions & 11 deletions tests/testthat/test-book.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ test_that("Book UI has expectd form", {
})

test_that("Book server returns selected book", {
expect_warning(
testServer(.book_server, {
session$setInputs(selected_book = "R for Data Science")
book <- session$getReturned()
expect_identical(
book(),
"R for Data Science"
)
}),
"MockShinySession"
)
testServer(.book_server, {
session$setInputs(selected_book = "R for Data Science")
book <- session$getReturned()
expect_identical(
book(),
"R for Data Science"
)
})
})

# As far as I can find through manual testing, shinytest2 can't "see" query
Expand Down

0 comments on commit 6ddb98c

Please sign in to comment.