-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added tester for RouterLink. #1810
Open
joelpop
wants to merge
2
commits into
main
Choose a base branch
from
feature/#1805-RouterLinkTester
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...rc/test/java/com/vaadin/flow/component/routerlink/RouterLinkQueryParameterTargetView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2000-2024 Vaadin Ltd | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See <https://vaadin.com/commercial-license-and-service-terms> for the full | ||
* license. | ||
*/ | ||
package com.vaadin.flow.component.routerlink; | ||
|
||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.component.HasComponents; | ||
import com.vaadin.flow.component.Tag; | ||
import com.vaadin.flow.component.html.Span; | ||
import com.vaadin.flow.router.BeforeEnterEvent; | ||
import com.vaadin.flow.router.BeforeEnterObserver; | ||
import com.vaadin.flow.router.Route; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
@Tag(Tag.DIV) | ||
@Route(value = RouterLinkQueryParameterTargetView.ROUTE, registerAtStartup = false) | ||
public class RouterLinkQueryParameterTargetView extends Component | ||
implements HasComponents, BeforeEnterObserver { | ||
|
||
public static final String ROUTE = "router-link-query-parameter-target"; | ||
|
||
@Override | ||
public void beforeEnter(BeforeEnterEvent event) { | ||
var queryParameters = event.getLocation().getQueryParameters(); | ||
|
||
add(new Span("Query Parameter Target View: { " + | ||
queryParameters.getParameters().entrySet().stream() | ||
.map(entry -> entry.getKey() + " = [" + | ||
entry.getValue().stream() | ||
.sorted() | ||
.collect(Collectors.joining(", ")) + | ||
"]") | ||
.sorted() | ||
.collect(Collectors.joining("; ")) + | ||
" }")); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...rc/test/java/com/vaadin/flow/component/routerlink/RouterLinkRouteParameterTargetView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (C) 2000-2024 Vaadin Ltd | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See <https://vaadin.com/commercial-license-and-service-terms> for the full | ||
* license. | ||
*/ | ||
package com.vaadin.flow.component.routerlink; | ||
|
||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.component.HasComponents; | ||
import com.vaadin.flow.component.Tag; | ||
import com.vaadin.flow.component.html.Span; | ||
import com.vaadin.flow.router.BeforeEnterEvent; | ||
import com.vaadin.flow.router.BeforeEnterObserver; | ||
import com.vaadin.flow.router.Route; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
@Tag(Tag.DIV) | ||
@Route(value = RouterLinkRouteParameterTargetView.ROUTE + RouterLinkRouteParameterTargetView.ROUTE_PARAMETERS, | ||
registerAtStartup = false) | ||
public class RouterLinkRouteParameterTargetView extends Component | ||
implements HasComponents, BeforeEnterObserver { | ||
|
||
public static final String ROUTE = "router-link-route-parameter-target"; | ||
public static final String ROUTE_PARAMETERS = "/:segment1?/static/:segment2/:segment3*"; | ||
|
||
@Override | ||
public void beforeEnter(BeforeEnterEvent event) { | ||
var routeParameters = event.getRouteParameters(); | ||
|
||
add(new Span("Route Parameter Target View: { " + | ||
routeParameters.getParameterNames().stream() | ||
.map(name -> name + " = " + routeParameters.get(name).orElse("")) | ||
.sorted() | ||
.collect(Collectors.joining("; ")) + | ||
" }")); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...junit5/src/test/java/com/vaadin/flow/component/routerlink/RouterLinkStaticTargetView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (C) 2000-2024 Vaadin Ltd | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See <https://vaadin.com/commercial-license-and-service-terms> for the full | ||
* license. | ||
*/ | ||
package com.vaadin.flow.component.routerlink; | ||
|
||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.component.HasComponents; | ||
import com.vaadin.flow.component.Tag; | ||
import com.vaadin.flow.component.html.Span; | ||
import com.vaadin.flow.router.Route; | ||
|
||
@Tag(Tag.DIV) | ||
@Route(value = RouterLinkStaticTargetView.ROUTE, registerAtStartup = false) | ||
public class RouterLinkStaticTargetView extends Component | ||
implements HasComponents { | ||
|
||
public static final String ROUTE = "router-link-static-target"; | ||
|
||
public RouterLinkStaticTargetView() { | ||
add(new Span("Static Target View")); | ||
} | ||
} |
183 changes: 183 additions & 0 deletions
183
...-unit-junit5/src/test/java/com/vaadin/flow/component/routerlink/RouterLinkTesterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* Copyright (C) 2000-2024 Vaadin Ltd | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See <https://vaadin.com/commercial-license-and-service-terms> for the full | ||
* license. | ||
*/ | ||
package com.vaadin.flow.component.routerlink; | ||
|
||
import com.vaadin.flow.component.html.Span; | ||
import com.vaadin.flow.router.RouteConfiguration; | ||
import com.vaadin.flow.router.RouterLink; | ||
import com.vaadin.testbench.unit.ComponentTester; | ||
import com.vaadin.testbench.unit.UIUnitTest; | ||
import com.vaadin.testbench.unit.ViewPackages; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@ViewPackages | ||
class RouterLinkTesterTest extends UIUnitTest { | ||
|
||
private ComponentTester<RouterLinkView> $routerLinkView; | ||
|
||
@BeforeEach | ||
void init() { | ||
RouteConfiguration.forApplicationScope() | ||
.setAnnotatedRoute(RouterLinkView.class); | ||
RouteConfiguration.forApplicationScope() | ||
.setAnnotatedRoute(RouterLinkStaticTargetView.class); | ||
RouteConfiguration.forApplicationScope() | ||
.setAnnotatedRoute(RouterLinkUrlParameterTargetView.class); | ||
RouteConfiguration.forApplicationScope() | ||
.setAnnotatedRoute(RouterLinkQueryParameterTargetView.class); | ||
RouteConfiguration.forApplicationScope() | ||
.setAnnotatedRoute(RouterLinkRouteParameterTargetView.class); | ||
|
||
var routerLinkView = navigate(RouterLinkView.class); | ||
$routerLinkView = test(routerLinkView); | ||
} | ||
|
||
@Test | ||
void routerLink_targetless() { | ||
// get router link | ||
var targetlessRouterLink = $routerLinkView.find(RouterLink.class) | ||
.withText("No Target") | ||
.single(); | ||
var $targetlessRouterLink = test(targetlessRouterLink); | ||
Assertions.assertNotNull($targetlessRouterLink, | ||
"Tester for targetless RouterLink not initialized."); | ||
|
||
// verify its href | ||
Assertions.assertEquals("", | ||
$targetlessRouterLink.getHref()); | ||
|
||
// verify its click action fails due to no navigation target | ||
Assertions.assertThrows(IllegalStateException.class, $targetlessRouterLink::click); | ||
} | ||
|
||
@Test | ||
void routerLink_static() { | ||
// get router link | ||
var staticRouterLink = $routerLinkView.find(RouterLink.class) | ||
.withText("Static Target") | ||
.single(); | ||
var $staticRouterLink = test(staticRouterLink); | ||
Assertions.assertNotNull($staticRouterLink, | ||
"Tester for static RouterLink not initialized."); | ||
|
||
// verify its href | ||
Assertions.assertEquals(RouterLinkStaticTargetView.ROUTE, | ||
$staticRouterLink.getHref()); | ||
|
||
// verify its click action returns correct target | ||
var targetView = $staticRouterLink.click(); | ||
Assertions.assertInstanceOf(RouterLinkStaticTargetView.class, targetView); | ||
|
||
// verify navigation target is correct | ||
var $targetView = test(targetView); | ||
Assertions.assertDoesNotThrow(() -> $targetView.find(Span.class) | ||
.withText("Static Target View") | ||
.single()); | ||
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as for |
||
} | ||
|
||
@Test | ||
void routerLink_emptyUrlParameter() { | ||
var emptyUrlParameterRouterLink = $routerLinkView.find(RouterLink.class) | ||
.withText("Empty URL Parameter Target") | ||
.single(); | ||
var $emptyUrlParameterRouterLink = test(emptyUrlParameterRouterLink); | ||
Assertions.assertNotNull($emptyUrlParameterRouterLink, | ||
"Tester for empty URL parameter RouterLink not initialized."); | ||
|
||
// verify its href | ||
Assertions.assertEquals(RouterLinkUrlParameterTargetView.ROUTE, | ||
$emptyUrlParameterRouterLink.getHref()); | ||
|
||
// verify its click action returns correct target | ||
var targetView = $emptyUrlParameterRouterLink.click(); | ||
Assertions.assertInstanceOf(RouterLinkUrlParameterTargetView.class, targetView); | ||
|
||
// verify navigation target is correct | ||
var $targetView = test(targetView); | ||
Assertions.assertDoesNotThrow(() -> $targetView.find(Span.class) | ||
.withText("URL Parameter Target View: { }") | ||
.single()); | ||
} | ||
|
||
@Test | ||
void routerLink_urlParameter() { | ||
var urlParameterRouterLink = $routerLinkView.find(RouterLink.class) | ||
.withText("URL Parameter Target") | ||
.single(); | ||
var $urlParameterRouterLink = test(urlParameterRouterLink); | ||
Assertions.assertNotNull($urlParameterRouterLink, | ||
"Tester for URL parameter RouterLink not initialized."); | ||
|
||
// verify its href | ||
Assertions.assertEquals(RouterLinkUrlParameterTargetView.ROUTE + "/parameter-value", | ||
$urlParameterRouterLink.getHref()); | ||
|
||
// verify its click action returns correct target | ||
var targetView = $urlParameterRouterLink.click(); | ||
Assertions.assertInstanceOf(RouterLinkUrlParameterTargetView.class, targetView); | ||
|
||
// verify navigation target is correct | ||
var $targetView = test(targetView); | ||
Assertions.assertDoesNotThrow(() -> $targetView.find(Span.class) | ||
.withText("URL Parameter Target View: { parameter-value }") | ||
.single()); | ||
} | ||
|
||
@Test | ||
void routerLink_queryParameter() { | ||
var queryParameterRouterLink = $routerLinkView.find(RouterLink.class) | ||
.withText("Query Parameter Target") | ||
.single(); | ||
var $queryParameterRouterLink = test(queryParameterRouterLink); | ||
Assertions.assertNotNull($queryParameterRouterLink, | ||
"Tester for QueryParameter RouterLink not initialized."); | ||
|
||
// verify its href | ||
Assertions.assertEquals(RouterLinkQueryParameterTargetView.ROUTE + | ||
"?parameter2=parameter2-value1¶meter2=parameter2-value2¶meter1=parameter1-value", | ||
$queryParameterRouterLink.getHref()); | ||
|
||
// verify its click action returns correct target | ||
var targetView = $queryParameterRouterLink.click(); | ||
Assertions.assertInstanceOf(RouterLinkQueryParameterTargetView.class, targetView); | ||
|
||
// verify navigation target is correct | ||
var $targetView = test(targetView); | ||
Assertions.assertDoesNotThrow(() -> $targetView.find(Span.class) | ||
.withText("Query Parameter Target View: { parameter1 = [parameter1-value]; parameter2 = [parameter2-value1, parameter2-value2] }") | ||
.single()); | ||
} | ||
|
||
@Test | ||
void routerLink_routeParameter() { | ||
var routeParameterRouterLink = $routerLinkView.find(RouterLink.class) | ||
.withText("Route Parameter Target") | ||
.single(); | ||
var $routeParameterRouterLink = test(routeParameterRouterLink); | ||
Assertions.assertNotNull($routeParameterRouterLink, | ||
"Tester for RouteParameter RouterLink not initialized."); | ||
|
||
// verify its href | ||
Assertions.assertEquals(RouterLinkRouteParameterTargetView.ROUTE + | ||
"/static/segment2-value/segment3-value1/segment3-value2", | ||
$routeParameterRouterLink.getHref()); | ||
|
||
// verify its click action returns correct target | ||
var targetView = $routeParameterRouterLink.click(); | ||
Assertions.assertInstanceOf(RouterLinkRouteParameterTargetView.class, targetView); | ||
|
||
// verify navigation target is correct | ||
var $targetView = test(targetView); | ||
Assertions.assertDoesNotThrow(() -> $targetView.find(Span.class) | ||
.withText("Route Parameter Target View: { segment2 = segment2-value; segment3 = segment3-value1/segment3-value2 }") | ||
.single()); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../src/test/java/com/vaadin/flow/component/routerlink/RouterLinkUrlParameterTargetView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (C) 2000-2024 Vaadin Ltd | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See <https://vaadin.com/commercial-license-and-service-terms> for the full | ||
* license. | ||
*/ | ||
package com.vaadin.flow.component.routerlink; | ||
|
||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.component.HasComponents; | ||
import com.vaadin.flow.component.Tag; | ||
import com.vaadin.flow.component.html.Span; | ||
import com.vaadin.flow.router.BeforeEvent; | ||
import com.vaadin.flow.router.HasUrlParameter; | ||
import com.vaadin.flow.router.OptionalParameter; | ||
import com.vaadin.flow.router.Route; | ||
|
||
@Tag(Tag.DIV) | ||
@Route(value = RouterLinkUrlParameterTargetView.ROUTE, registerAtStartup = false) | ||
public class RouterLinkUrlParameterTargetView extends Component | ||
implements HasComponents, HasUrlParameter<String> { | ||
|
||
public static final String ROUTE = "router-link-url-parameter-target"; | ||
|
||
@Override | ||
public void setParameter(BeforeEvent event, @OptionalParameter String parameter) { | ||
add(new Span("URL Parameter Target View: { " + (parameter != null ? parameter : "") + " }")); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...tbench-unit-junit5/src/test/java/com/vaadin/flow/component/routerlink/RouterLinkView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (C) 2000-2024 Vaadin Ltd | ||
* | ||
* This program is available under Vaadin Commercial License and Service Terms. | ||
* | ||
* See <https://vaadin.com/commercial-license-and-service-terms> for the full | ||
* license. | ||
*/ | ||
package com.vaadin.flow.component.routerlink; | ||
|
||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.component.HasComponents; | ||
import com.vaadin.flow.component.Tag; | ||
import com.vaadin.flow.router.QueryParameters; | ||
import com.vaadin.flow.router.Route; | ||
import com.vaadin.flow.router.RouteParameters; | ||
import com.vaadin.flow.router.RouterLink; | ||
|
||
import java.util.Map; | ||
|
||
@Tag(Tag.DIV) | ||
@Route(value = RouterLinkView.ROUTE, registerAtStartup = false) | ||
public class RouterLinkView extends Component implements HasComponents { | ||
|
||
public static final String ROUTE = "router-link-test"; | ||
|
||
public RouterLinkView() { | ||
// targetless router link | ||
var targetlessRouterLink = new RouterLink(); | ||
targetlessRouterLink.setText("No Target"); | ||
|
||
// static router link | ||
var staticRouterLink = new RouterLink("Static Target", | ||
RouterLinkStaticTargetView.class); | ||
|
||
// url parameter router link - empty | ||
var emptyUrlParameterRouterLink = new RouterLink("Empty URL Parameter Target", | ||
RouterLinkUrlParameterTargetView.class); | ||
|
||
// url parameter router link - non-empty | ||
var urlParameterRouterLink = new RouterLink("URL Parameter Target", | ||
RouterLinkUrlParameterTargetView.class, | ||
"parameter-value"); | ||
|
||
// query parameter router link | ||
var queryParameterRouterLink = new RouterLink("Query Parameter Target", | ||
RouterLinkQueryParameterTargetView.class); | ||
queryParameterRouterLink.setQueryParameters( | ||
QueryParameters.empty() | ||
.merging("parameter1", "parameter1-value") | ||
.merging("parameter2", "parameter2-value1", "parameter2-value2")); | ||
|
||
// route parameter router link | ||
var routeParameterRouterLink = new RouterLink("Route Parameter Target", | ||
RouterLinkRouteParameterTargetView.class, | ||
new RouteParameters(Map.ofEntries( | ||
Map.entry("segment2", "segment2-value"), | ||
Map.entry("segment3", "segment3-value1/segment3-value2")))); | ||
|
||
add(targetlessRouterLink); | ||
add(staticRouterLink); | ||
add(emptyUrlParameterRouterLink); | ||
add(urlParameterRouterLink); | ||
add(queryParameterRouterLink); | ||
add(routeParameterRouterLink); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest making all RouterLink components as package protected class fields so that the test can directly wrap the instance, without relying on
find
.This allows to prevent false positives in tests because of potential bugs in the component locator.
IMO it also simplifies the test code