All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mvysny.kaributesting.v10.Navigator.kt Maven / Gradle / Ivy

There is a newer version: 2.1.8
Show newest version
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)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy