Skip to content

Commit

Permalink
feat: Added support for query parameters to AnchorTester. (#1812)
Browse files Browse the repository at this point in the history
Co-authored-by: Marco Collovati <[email protected]>
  • Loading branch information
joelpop and mcollovati authored Aug 12, 2024
1 parent c295f45 commit 5f404eb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2000-2022 Vaadin Ltd
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
Expand Down Expand Up @@ -39,7 +39,19 @@ void anchorClick_navigatesCorrectly() {
UI.getCurrent().add(anchor);

Assertions.assertEquals("anchor", test(anchor).getHref());
Assertions.assertTrue(test(anchor).click() instanceof AnchorView,
Assertions.assertInstanceOf(AnchorView.class, test(anchor).click(),
"Click anchor did not navigate to AnchorView");
}

@Test
void anchorClick_navigatesCorrectlyWithParameters() {
Anchor anchor = new Anchor("anchor?name=value", "Home");
UI.getCurrent().add(anchor);

Assertions.assertEquals("anchor?name=value", test(anchor).getHref());
Assertions.assertEquals("anchor", test(anchor).getPath());
Assertions.assertEquals("name=value", test(anchor).getQueryParameters().getQueryString());
Assertions.assertInstanceOf(AnchorView.class, test(anchor).click(),
"Click anchor did not navigate to AnchorView");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2000-2022 Vaadin Ltd
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
Expand All @@ -18,6 +18,7 @@
import com.vaadin.flow.component.HasElement;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.router.QueryParameters;
import com.vaadin.flow.router.RouteConfiguration;
import com.vaadin.flow.server.AbstractStreamResource;
import com.vaadin.flow.server.StreamResource;
Expand Down Expand Up @@ -47,6 +48,25 @@ public String getHref() {
return getComponent().getHref();
}

/**
* Gets the path for the router-link.
* Returns an empty {@link String} if there is no corresponding navigation target.
*
* @return a {@link String} containing the navigation target path or empty if not present
*/
public String getPath() {
return URI.create(getHref()).getPath();
}

/**
* Gets the query parameters for the router-link.
*
* @return a {@link QueryParameters} containing the navigation target's query parameters
*/
public QueryParameters getQueryParameters() {
return QueryParameters.fromString(URI.create(getHref()).getQuery());
}

/**
* Click the anchor for navigation if target is a registered route in the
* application.
Expand All @@ -60,9 +80,8 @@ public HasElement click() {
final Field href = getField(Anchor.class, "href");
try {
if (href.get(getComponent()) instanceof String) {
if (RouteConfiguration.forSessionScope()
.getRoute(getComponent().getHref()).isPresent()) {
UI.getCurrent().navigate(getComponent().getHref());
if (RouteConfiguration.forSessionScope().getRoute(getPath()).isPresent()) {
UI.getCurrent().navigate(getPath(), getQueryParameters());
return UI.getCurrent().getInternals()
.getActiveRouterTargetsChain().get(0);
} else {
Expand Down

0 comments on commit 5f404eb

Please sign in to comment.