Skip to content

Commit

Permalink
Merge pull request #258 from hotwired/prevent-pop-from-start
Browse files Browse the repository at this point in the history
Prevent a POP presentation from the start destination
  • Loading branch information
mbarta authored Feb 10, 2023
2 parents f20886c + b9d73d9 commit 90de2c6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
8 changes: 8 additions & 0 deletions turbo/src/main/assets/json/test-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
"title": "Image Viewer"
}
},
{
"patterns": [
"/custom/recede"
],
"properties": {
"presentation": "pop"
}
},
{
"patterns": [
"/custom/refresh"
Expand Down
10 changes: 8 additions & 2 deletions turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ internal class TurboNavRule(
}

private fun newPresentation(): TurboNavPresentation {
// Use the custom presentation provided in the path configuration
// Check if we should use the custom presentation provided in the path configuration
if (newProperties.presentation != TurboNavPresentation.DEFAULT) {
return newProperties.presentation
return if (isAtStartDestination && newProperties.presentation == TurboNavPresentation.POP) {
// You cannot pop from the start destination, prevent visit
TurboNavPresentation.NONE
} else {
// Use the custom presentation
newProperties.presentation
}
}

val locationIsCurrent = locationsAreSame(newLocation, currentLocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TurboPathConfigurationRepositoryTest : BaseRepositoryTest() {
assertThat(json).isNotNull()

val config = load(json)
assertThat(config?.rules?.size).isEqualTo(8)
assertThat(config?.rules?.size).isEqualTo(9)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TurboPathConfigurationTest : BaseRepositoryTest() {

@Test
fun assetConfigurationIsLoaded() {
assertThat(pathConfiguration.rules.size).isEqualTo(8)
assertThat(pathConfiguration.rules.size).isEqualTo(9)
}

@Test
Expand Down
23 changes: 23 additions & 0 deletions turbo/src/test/kotlin/dev/hotwire/turbo/nav/TurboNavRuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TurboNavRuleTest {
private val featureUrl = "https://hotwired.dev/feature"
private val newUrl = "https://hotwired.dev/feature/new"
private val editUrl = "https://hotwired.dev/feature/edit"
private val recedeUrl = "https://hotwired.dev/custom/recede"
private val refreshUrl = "https://hotwired.dev/custom/refresh"
private val resumeUrl = "https://hotwired.dev/custom/resume"
private val modalRootUrl = "https://hotwired.dev/custom/modal"
Expand Down Expand Up @@ -330,6 +331,28 @@ class TurboNavRuleTest {
assertThat(rule.newNavOptions).isEqualTo(navOptions)
}

@Test
fun `prevent pop presentation from start destination`() {
val rule = getNavigatorRule(recedeUrl)

// Current destination
assertThat(rule.previousLocation).isNull()
assertThat(rule.currentLocation).isEqualTo(homeUrl)
assertThat(rule.currentPresentationContext).isEqualTo(TurboNavPresentationContext.DEFAULT)
assertThat(rule.isAtStartDestination).isTrue()

// New destination
assertThat(rule.newLocation).isEqualTo(recedeUrl)
assertThat(rule.newPresentationContext).isEqualTo(TurboNavPresentationContext.DEFAULT)
assertThat(rule.newPresentation).isEqualTo(TurboNavPresentation.NONE)
assertThat(rule.newQueryStringPresentation).isEqualTo(TurboNavQueryStringPresentation.DEFAULT)
assertThat(rule.newNavigationMode).isEqualTo(TurboNavMode.NONE)
assertThat(rule.newModalResult).isNull()
assertThat(rule.newDestinationUri).isEqualTo(webUri)
assertThat(rule.newDestination).isNotNull()
assertThat(rule.newNavOptions).isEqualTo(navOptions)
}

private fun getNavigatorRule(
location: String,
visitOptions: TurboVisitOptions = TurboVisitOptions(),
Expand Down

0 comments on commit 90de2c6

Please sign in to comment.