Skip to content

Commit

Permalink
fix: resolve route parameters when rerouting and forwardin with query…
Browse files Browse the repository at this point in the history
… parameters (#20210)

Fixes #20205
  • Loading branch information
mcollovati authored and vaadin-bot committed Oct 10, 2024
1 parent 13fb546 commit f4a6ffc
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,11 @@ public <T> void forwardTo(String location, List<T> locationParams) {
*/
public void forwardTo(String locationString,
QueryParameters queryParameters) {
final Optional<Class<? extends Component>> target = getSource()
.getRegistry().getNavigationTarget(locationString);
final Optional<NavigationState> navigationState = getSource()
.resolveNavigationTarget(new Location(locationString));
this.redirectQueryParameters = queryParameters;
if (target.isPresent()) {
forwardTo(getNavigationState(locationString, List.of()));
if (navigationState.isPresent()) {
forwardTo(navigationState.get());
} else {
// Inform that forward target location is not known.
unknownForward = PathUtil.trimPath(locationString);
Expand Down Expand Up @@ -910,12 +910,11 @@ public <T> void rerouteTo(String route, List<T> routeParams) {
* query parameters for the target
*/
public void rerouteTo(String route, QueryParameters queryParameters) {
final Optional<Class<? extends Component>> target = getSource()
.getRegistry().getNavigationTarget(route);

final Optional<NavigationState> navigationState = getSource()
.resolveNavigationTarget(new Location(route));
this.redirectQueryParameters = queryParameters;
if (target.isPresent()) {
rerouteTo(getNavigationState(route, List.of()));
if (navigationState.isPresent()) {
rerouteTo(navigationState.get());
} else {
// Inform that reroute target location is not known.
unknownReroute = PathUtil.trimPath(route);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.vaadin.flow.uitest.ui;

import java.util.List;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.AfterNavigationObserver;
import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.OptionalParameter;
import com.vaadin.flow.router.QueryParameters;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.uitest.servlet.ViewTestLayout;

Expand All @@ -14,27 +17,50 @@ public class SetParameterForwardToView extends Div
implements HasUrlParameter<String>, AfterNavigationObserver {

static final String LOCATION_ID = "location";
static final String PARAMETER_ID = "parameter";

private final Div location;
private final Div param;

public SetParameterForwardToView() {
location = new Div();
location.setId(LOCATION_ID);
param = new Div();
param.setId(PARAMETER_ID);
}

@Override
public void setParameter(BeforeEvent event,
@OptionalParameter String parameter) {
if (parameter != null && parameter.equals("one")) {
event.forwardTo(
"com.vaadin.flow.uitest.ui.SetParameterForwardToView",
"two");
if (parameter != null) {
switch (parameter) {
case "location":
event.forwardTo(
"com.vaadin.flow.uitest.ui.SetParameterForwardToView/locationTwo");
break;
case "locationRouteParameter":
event.forwardTo(
"com.vaadin.flow.uitest.ui.SetParameterForwardToView",
"locationRouteParameterTwo");
break;
case "locationRouteParameterList":
event.forwardTo(
"com.vaadin.flow.uitest.ui.SetParameterForwardToView",
List.of("locationRouteParameterListTwo"));
break;
case "locationQueryParams":
event.forwardTo(
"com.vaadin.flow.uitest.ui.SetParameterForwardToView/locationQueryParamsTwo",
QueryParameters.empty());
break;
}
}
param.setText(parameter);
}

@Override
public void afterNavigation(AfterNavigationEvent event) {
location.setText(event.getLocation().getPath());
add(location);
add(location, param);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.vaadin.flow.uitest.ui;

import java.util.List;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.AfterNavigationObserver;
import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.OptionalParameter;
import com.vaadin.flow.router.QueryParameters;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.uitest.servlet.ViewTestLayout;

@Route(value = "com.vaadin.flow.uitest.ui.SetParameterRerouteToView", layout = ViewTestLayout.class)
public class SetParameterRerouteToView extends Div
implements HasUrlParameter<String>, AfterNavigationObserver {

static final String LOCATION_ID = "location";
static final String PARAMETER_ID = "parameter";

private final Div location;
private final Div param;

public SetParameterRerouteToView() {
location = new Div();
location.setId(LOCATION_ID);
param = new Div();
param.setId(PARAMETER_ID);
}

@Override
public void setParameter(BeforeEvent event,
@OptionalParameter String parameter) {
if (parameter != null) {
switch (parameter) {
case "location":
event.rerouteTo(
"com.vaadin.flow.uitest.ui.SetParameterRerouteToView/locationTwo");
break;
case "locationRouteParameter":
event.rerouteTo(
"com.vaadin.flow.uitest.ui.SetParameterRerouteToView",
"locationRouteParameterTwo");
break;
case "locationRouteParameterList":
event.rerouteTo(
"com.vaadin.flow.uitest.ui.SetParameterRerouteToView",
List.of("locationRouteParameterListTwo"));
break;
case "locationQueryParams":
event.rerouteTo(
"com.vaadin.flow.uitest.ui.SetParameterRerouteToView/locationQueryParamsTwo",
QueryParameters.empty());
break;
}
}
param.setText(parameter);
}

@Override
public void afterNavigation(AfterNavigationEvent event) {
location.setText(event.getLocation().getPath());
add(location, param);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,45 @@
public class SetParameterForwardToIT extends ChromeBrowserTest {

@Test
public void testForwardingToViewInSetParameter() {
public void testForwardingToViewWithLocation_setParameter() {
testForwardingToView("location");
}

@Test
public void testForwardingToViewWithRouteParameter_setParameter() {
testForwardingToView("locationRouteParameter");
}

@Test
public void testForwardingToViewWithRouteParameterList_setParameter() {
testForwardingToView("locationRouteParameterList");
}

@Test
public void testForwardingToViewWithQueryParameter_setParameter() {
testForwardingToView("locationQueryParams");
}

private void testForwardingToView(String location) {
final String baseLoc = "/view/com.vaadin.flow.uitest.ui.SetParameterForwardToView";
getDriver().get(getRootURL() + baseLoc + "/one");
final String expectedParameter = location + "Two";
final String expectedLoc = "/" + expectedParameter;
getDriver().get(getRootURL() + baseLoc + "/" + location);
waitForDevServer();

waitForElementPresent(By.id(SetParameterForwardToView.LOCATION_ID));
final String locationId = findElement(
By.id(SetParameterForwardToView.LOCATION_ID)).getText();
Assert.assertTrue("should redirect to " + baseLoc + "/two",
locationId.endsWith("/two"));
waitForElementPresent(By.id(SetParameterForwardToView.PARAMETER_ID));
final String parameterValue = findElement(
By.id(SetParameterForwardToView.PARAMETER_ID)).getText();

Assert.assertTrue("should redirect to " + baseLoc + expectedLoc,
locationId.endsWith(expectedLoc));
Assert.assertTrue("should update the URL",
getDriver().getCurrentUrl().endsWith(baseLoc + "/two"));
getDriver().getCurrentUrl().endsWith(baseLoc + expectedLoc));
Assert.assertEquals("forwarded parameter", expectedParameter,
parameterValue);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.vaadin.flow.uitest.ui;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;

import com.vaadin.flow.testutil.ChromeBrowserTest;

public class SetParameterRerouteToIT extends ChromeBrowserTest {

@Test
public void testReroutingToViewWithLocation_setParameter() {
testReroutingToView("location");
}

@Test
public void testReroutingToViewWithRouteParameter_setParameter() {
testReroutingToView("locationRouteParameter");
}

@Test
public void testReroutingToViewWithRouteParameterList_setParameter() {
testReroutingToView("locationRouteParameterList");
}

@Test
public void testReroutingToViewWithQueryParameter_setParameter() {
testReroutingToView("locationQueryParams");
}

private void testReroutingToView(String location) {
final String baseLoc = "/view/com.vaadin.flow.uitest.ui.SetParameterRerouteToView";
final String expectedParameter = location + "Two";
final String expectedLoc = "/" + expectedParameter;
final String originalLoc = baseLoc + "/" + location;
getDriver().get(getRootURL() + originalLoc);
waitForDevServer();

waitForElementPresent(By.id(SetParameterForwardToView.LOCATION_ID));
final String locationId = findElement(
By.id(SetParameterForwardToView.LOCATION_ID)).getText();
waitForElementPresent(By.id(SetParameterForwardToView.PARAMETER_ID));
final String parameterValue = findElement(
By.id(SetParameterForwardToView.PARAMETER_ID)).getText();

Assert.assertTrue("should redirect to " + baseLoc + expectedLoc,
locationId.endsWith(expectedLoc));
Assert.assertTrue("should not update the URL",
getDriver().getCurrentUrl().endsWith(originalLoc));
Assert.assertEquals("rerouted parameter", expectedParameter,
parameterValue);
}

}

0 comments on commit f4a6ffc

Please sign in to comment.