com.github.mvysny.kaributesting.v10.Navigator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of karibu-testing-v10 Show documentation
Show all versions of karibu-testing-v10 Show documentation
Karibu Testing, support for browserless Vaadin testing in Kotlin
package com.github.mvysny.kaributesting.v10
import com.vaadin.flow.component.Component
import com.vaadin.flow.component.UI
import com.vaadin.flow.server.RouteRegistry
import kotlin.test.expect
/**
* Returns the path of the current view as shown in browser's URL bar. Includes query parameters and all.
* For example, for `http://localhost:8080/my/view?foo=bar` returns `my/view?foo=bar`.
* Returns null if there is no current UI.
*
* Doesn't take [com.vaadin.flow.component.page.History.replaceState] or others into account.
* The reason is that changing browser's history only manipulates the URL but doesn't
* cause the browser to navigate and change a view.
*/
public val currentPath: String? get() =
UI.getCurrent()?.internals?.activeViewLocation?.pathWithQueryParameters
/**
* Resolves route for given [path].
* @param path e.g. `my/view?foo=bar`
*/
public fun resolveRoute(path: String): Class? {
// trim & remove any query parameters
val trimmedPath = path.trim('/').substringBefore('?')
val registry: RouteRegistry = currentUI.internals.router.registry
val segments: List = trimmedPath.split('/')
for (prefix: Int in segments.size downTo 1) {
val p: String = segments.subList(0, prefix).joinToString("/")
val s: List = segments.subList(prefix, segments.size)
val clazz: Class? = registry.getNavigationTarget(p, s).orElse(null)
if (clazz != null) {
return clazz
}
}
return null
}
/**
* Returns the current view class.
*/
public val currentView: Class
get() = currentUI.internals.activeRouterTargetsChain.first().javaClass.asSubclass(Component::class.java)
/**
* Expects that given [view] is the currently displayed view.
*/
public fun expectView(view: Class) {
@Suppress("UNCHECKED_CAST")
expect(view, "current path: '$currentPath'") { currentView }
}
/**
* Expects that given view is the currently displayed view.
*/
public inline fun expectView() {
expectView(V::class.java)
}